KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > crypto > Tea


1 package com.quadcap.crypto;
2
3 /* Copyright 2002 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.Random JavaDoc;
42
43 import java.nio.ByteBuffer JavaDoc;
44 import java.nio.IntBuffer JavaDoc;
45
46 import com.quadcap.util.text.Text;
47 import com.quadcap.util.Util;
48
49 /**
50  * Implementation of Tiny Encryption Algorithm
51  *
52  * @author Stan Bailes
53  */

54 public class Tea extends AbstractSymmetricKey implements SymmetricKey {
55     static final int delta = 0x9E3779B9;
56     int a, b, c, d;
57     int[] v = new int[2];
58
59     /**
60      * Initialize key from serialized representation
61      */

62     public void init(String JavaDoc s) {
63         String JavaDoc[] vx = Text.extractN(s, "*:*:*:*:*");
64         a = Integer.parseInt(vx[1]);
65         b = Integer.parseInt(vx[2]);
66         c = Integer.parseInt(vx[3]);
67         d = Integer.parseInt(vx[4]);
68     }
69
70     public void init(byte[] k) {
71         a = Util.integer(k, 0);
72         b = Util.integer(k, 4);
73         c = Util.integer(k, 8);
74         d = Util.integer(k, 12);
75     }
76
77     /**
78      * Initialize: Create a random key
79      */

80     public void init(Random JavaDoc r) {
81         a = r.nextInt();
82         b = r.nextInt();
83         c = r.nextInt();
84         d = r.nextInt();
85     }
86
87     /**
88      * Return the serialized form of the key
89      */

90     public String JavaDoc toString() {
91         return "TEA:" + a + ":" + b + ":" + c + ":" + d;
92     }
93
94     /**
95      * Encrypt a buffer (must be multiple of 8 bytes)
96      */

97     public void encrypt(ByteBuffer JavaDoc m, ByteBuffer JavaDoc c) {
98         while (m.position() < m.limit()) {
99             v[0] = m.getInt();
100             v[1] = m.getInt();
101             encrypt(v);
102             c.putInt(v[0]);
103             c.putInt(v[1]);
104         }
105     }
106
107     /**
108      * Decrypt a buffer (must be a multiple of 8 bytes)
109      */

110     public void decrypt(ByteBuffer JavaDoc c, ByteBuffer JavaDoc m) {
111         while ( c.position() < c.limit()) {
112             v[0] = c.getInt();
113             v[1] = c.getInt();
114             decrypt(v);
115             m.putInt(v[0]);
116             m.putInt(v[1]);
117         }
118     }
119
120     public int getBlockSize() { return 8; }
121     
122     final void encrypt(int[] v) {
123         int y = v[0];
124         int z = v[1];
125         int sum = 0;
126         
127         for (int n = 32; n-- > 0; ) {
128             sum += delta;
129             y += (z << 4) + a ^ z + sum ^ (z >>> 5) + b;
130             z += (y << 4) + c ^ y + sum ^ (y >>> 5) + d;
131         }
132         v[0] = y;
133         v[1] = z;
134     }
135
136     final void decrypt(int[] v) {
137         int y = v[0];
138         int z = v[1];
139         int sum = 0xC6EF3720;
140         for (int n = 32; n-- > 0; ) {
141             z -= (y << 4) + c ^ y + sum ^ (y >>> 5) + d;
142             y -= (z << 4) + a ^ z + sum ^ (z >>> 5) + b;
143             sum -= delta;
144         }
145         v[0] = y;
146         v[1] = z;
147     }
148 }
149
Free Books   Free Magazines  
Popular Tags