KickJava   Java API By Example, From Geeks To Geeks.

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


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.LongBuffer JavaDoc;
45
46 import com.quadcap.util.text.Text;
47 import com.quadcap.util.Util;
48
49 /**
50  * Implementation of Tiny Encryption Algorithm, modified to use longs
51  * instead of ints. SO the key is now four longs, or 256 bits, and the
52  * encryption/decryption proceeds 16 bytes at a time instead of 8.
53  *
54  * The number of rounds has been retained at 32.
55  *
56  * @author Stan Bailes
57  */

58 public class Tea256 extends AbstractSymmetricKey implements SymmetricKey {
59     /** Computed as (sqrt(5)-1) * 2**63, following original Tea choice of
60         delta. */

61     static final long delta = 0x9E3779B97F4A7C15L;
62     static final int rounds = 32;
63     
64     long a, b, c, d;
65     long[] v = new long[2];
66
67     /**
68      * Initialize key from serialized representation
69      */

70     public void init(String JavaDoc s) {
71         String JavaDoc[] vx = Text.extractN(s, "*:*:*:*:*");
72         a = Long.parseLong(vx[1]);
73         b = Long.parseLong(vx[2]);
74         c = Long.parseLong(vx[3]);
75         d = Long.parseLong(vx[4]);
76     }
77
78     public void init(byte[] k) {
79         a = Util.bytesToLong(k, 0);
80         b = Util.bytesToLong(k, 8);
81         c = Util.bytesToLong(k, 16);
82         d = Util.bytesToLong(k, 24);
83     }
84     
85     /**
86      * Initialize: Create a random key
87      */

88     public void init(Random JavaDoc r) {
89         a = r.nextLong();
90         b = r.nextLong();
91         c = r.nextLong();
92         d = r.nextLong();
93     }
94
95     /**
96      * Return the serialized form of the key
97      */

98     public String JavaDoc toString() {
99         return "TEA256:" + a + ":" + b + ":" + c + ":" + d;
100     }
101
102     /**
103      * Encrypt a buffer (must be multiple of 16 bytes)
104      */

105     public void encrypt(ByteBuffer JavaDoc plain, ByteBuffer JavaDoc code) {
106         LongBuffer JavaDoc p = plain.asLongBuffer();
107         LongBuffer JavaDoc cb = code.asLongBuffer();
108         for (int i = 0; i < p.limit(); i += 2) {
109             v[0] = p.get(i);
110             v[1] = p.get(i+1);
111             encrypt(v);
112             cb.put(i, v[0]);
113             cb.put(i+1, v[1]);
114         }
115     }
116
117     /**
118      * Decrypt a buffer (must be a multiple of 16 bytes)
119      */

120     public void decrypt(ByteBuffer JavaDoc code, ByteBuffer JavaDoc plain) {
121         LongBuffer JavaDoc p = plain.asLongBuffer();
122         LongBuffer JavaDoc cb = code.asLongBuffer();
123         for (int i = 0; i < cb.limit(); i += 2) {
124             v[0] = cb.get(i);
125             v[1] = cb.get(i+1);
126             decrypt(v);
127             p.put(i, v[0]);
128             p.put(i+1, v[1]);
129         }
130     }
131
132     public int getBlockSize() { return 16; }
133     
134     final void encrypt(long[] v) {
135         long y = v[0];
136         long z = v[1];
137         long sum = 0;
138         
139         for (int n = rounds; n-- > 0; ) {
140             sum += delta;
141             y += (z << 4) + a ^ z + sum ^ (z >>> 5) + b;
142             z += (y << 4) + c ^ y + sum ^ (y >>> 5) + d;
143         }
144         v[0] = y;
145         v[1] = z;
146     }
147
148     final void decrypt(long[] v) {
149         long y = v[0];
150         long z = v[1];
151         long sum = delta * rounds;
152         for (int n = rounds; n-- > 0; ) {
153             z -= (y << 4) + c ^ y + sum ^ (y >>> 5) + d;
154             y -= (z << 4) + a ^ z + sum ^ (z >>> 5) + b;
155             sum -= delta;
156         }
157         v[0] = y;
158         v[1] = z;
159     }
160 }
161
Popular Tags