KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > io > crypt > XTEA


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.io.crypt;
22
23 /**
24  * @exclude
25  */

26 public class XTEA {
27
28     /**
29      * @exclude
30      */

31     public final static class IterationSpec {
32         int _iterations;
33         int _deltaSumInitial;
34         
35         IterationSpec(int iterations,int deltaSumInitial) {
36             _iterations=iterations;
37             _deltaSumInitial=deltaSumInitial;
38         }
39     }
40
41     public final static IterationSpec ITERATIONS8=new IterationSpec(8,0xF1BBCDC8);
42     public final static IterationSpec ITERATIONS16=new IterationSpec(16,0xE3779B90);
43     public final static IterationSpec ITERATIONS32=new IterationSpec(32,0xC6EF3720);
44     public final static IterationSpec ITERATIONS64=new IterationSpec(64,0x8DDE6E40);
45
46     private final IterationSpec _iterationSpec;
47     
48     private static final int DELTA = 0x9E3779B9;
49
50     private int[] _key;
51
52     /**
53      * creates an XTEA object from the given String key and iterations count.
54      *
55      * @param key
56      * the key, used in ecryption/decryption routine.
57      * @param iterationSpec
58      * The iteration spec containing iterations count.
59      * Possible values are 8, 16, 32 and 64.
60      *
61      */

62     public XTEA(String JavaDoc key, IterationSpec iterationSpec) {
63         this(new KeyGenerator().core(key), iterationSpec);
64     }
65
66     /**
67      * creates an XTEA object from the given String key. The default value of
68      * rounds is 32;
69      *
70      * @param key
71      * the key, used in ecryption/decryption routine.
72      *
73      *
74      */

75     public XTEA(String JavaDoc key) {
76         this(new KeyGenerator().core(key), ITERATIONS32);
77     }
78
79     /**
80      * creates an XTEA object from an int array of four
81      * @throws IllegalArgumentException
82      *
83      */

84     private XTEA(int[] key, IterationSpec iterationSpec) throws IllegalArgumentException JavaDoc {
85         if (key.length != 4) {
86             throw new IllegalArgumentException JavaDoc();
87         }
88         _key = key;
89         _iterationSpec=iterationSpec;
90     }
91
92     /**
93      * converts incoming array of eight bytes from offset to array of two
94      * integer values.<br>
95      * (An Integer is represented in memory as four bytes.)
96      *
97      * @param bytes
98      * Incoming byte array of length eight to be converted<br>
99      * @param offset
100      * Offset from which to start converting bytes<br>
101      * @param res
102      * Int array of length two which contains converted array bytes.
103      *
104      */

105     private void byte2int(byte[] bytes, int offset, int[] res) {
106         res[0] = (((bytes[offset] & 0xff) << 24)
107                 | ((bytes[offset + 1] & 0xff) << 16)
108                 | ((bytes[offset + 2] & 0xff) << 8) | (bytes[offset + 3] & 0xff));
109         res[1] = (((bytes[offset + 4] & 0xff) << 24)
110                 | ((bytes[offset + 5] & 0xff) << 16)
111                 | ((bytes[offset + 6] & 0xff) << 8) | (bytes[offset + 7] & 0xff));
112     }
113
114     /**
115      * converts incoming array of two integers from offset to array of eight
116      * bytes.<br>
117      * (An Integer is represented in memory as four bytes.)
118      *
119      * @param i
120      * Incoming integer array of two to be converted<br>
121      * @param offset
122      * Offset from which to start converting integer values<br>
123      * @param res
124      * byte array of length eight which contains converted integer
125      * array i.
126      */

127     private void int2byte(int[] i, int offset, byte[] res) {
128         res[offset] = (byte) ((i[0] & 0xff000000) >>> 24);
129         res[offset + 1] = (byte) ((i[0] & 0x00ff0000) >>> 16);
130         res[offset + 2] = (byte) ((i[0] & 0x0000ff00) >>> 8);
131         res[offset + 3] = (byte) (i[0] & 0x000000ff);
132         res[offset + 4] = (byte) ((i[1] & 0xff000000) >>> 24);
133         res[offset + 5] = (byte) ((i[1] & 0x00ff0000) >>> 16);
134         res[offset + 6] = (byte) ((i[1] & 0x0000ff00) >>> 8);
135         res[offset + 7] = (byte) (i[1] & 0x000000ff);
136
137     }
138
139     /**
140      * enciphers two int values
141      *
142      * @param block
143      * int array to be encipher according to the XTEA encryption
144      * algorithm<br>
145      */

146     private void encipher(int[] block) {
147         int n = _iterationSpec._iterations;
148         int delta_sum = 0;
149         while (n-- > 0) {
150             block[0] += ((block[1] << 4 ^ block[1] >> 5) + block[1])
151                     ^ (delta_sum + _key[delta_sum & 3]);
152             delta_sum += DELTA;
153             block[1] += ((block[0] << 4 ^ block[0] >> 5) + block[0])
154                     ^ (delta_sum + _key[delta_sum >> 11 & 3]);
155
156         }
157     }
158
159     /**
160      * deciphers two int values
161      *
162      * @param e_block
163      * int array to be decipher according to the XTEA encryption
164      * algorithm<br>
165      */

166     private void decipher(int[] e_block) {
167         int delta_sum = _iterationSpec._deltaSumInitial;
168         int n = _iterationSpec._iterations;
169         while (n-- > 0) {
170             e_block[1] -= ((e_block[0] << 4 ^ e_block[0] >> 5) + e_block[0])
171                     ^ (delta_sum + _key[delta_sum >> 11 & 3]);
172             delta_sum -= DELTA;
173             e_block[0] -= ((e_block[1] << 4 ^ e_block[1] >> 5) + e_block[1])
174                     ^ (delta_sum + _key[delta_sum & 3]);
175         }
176     }
177
178     /**
179      * encrypts incoming byte array according XTEA
180      *
181      * @param buffer
182      * incoming byte array to be encrypted
183      */

184     public void encrypt(byte[] buffer) {
185         int[] asInt = new int[2];
186         for (int i = 0; i < buffer.length; i += 8) {
187             byte2int(buffer, i, asInt);
188             encipher(asInt);
189             int2byte(asInt, i, buffer);
190         }
191     }
192
193     /**
194      * decrypts incoming byte array according XTEA
195      *
196      * @param buffer
197      * incoming byte array to be decrypted
198      *
199      */

200     public void decrypt(byte[] buffer) {
201         int[] asInt = new int[2];
202         for (int i = 0; i < buffer.length; i += 8) {
203             byte2int(buffer, i, asInt);
204             decipher(asInt);
205             int2byte(asInt, i, buffer);
206         }
207     }
208 }
209
Popular Tags