add src files decompiled via jad
[rsa2client.git] / src / Acme / Crypto / DesCipher.java
1 // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.kpdus.com/jad.html
3 // Decompiler options: packimports(3) fieldsfirst nonlb space 
4
5 package Acme.Crypto;
6
7
8 public class DesCipher {
9
10     private int encryptKeys[];
11     private int decryptKeys[];
12     private int tempInts[];
13     private static byte bytebit[] = {
14         1, 2, 4, 8, 16, 32, 64, -128
15     };
16     private static int bigbyte[] = {
17         0x800000, 0x400000, 0x200000, 0x100000, 0x80000, 0x40000, 0x20000, 0x10000, 32768, 16384, 
18         8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 
19         8, 4, 2, 1
20     };
21     private static byte pc1[] = {
22         56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 
23         41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 
24         26, 18, 10, 2, 59, 51, 43, 35, 62, 54, 
25         46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 
26         29, 21, 13, 5, 60, 52, 44, 36, 28, 20, 
27         12, 4, 27, 19, 11, 3
28     };
29     private static int totrot[] = {
30         1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 
31         19, 21, 23, 25, 27, 28
32     };
33     private static byte pc2[] = {
34         13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 
35         20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 
36         26, 19, 12, 1, 40, 51, 30, 36, 46, 54, 
37         29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 
38         33, 52, 45, 41, 49, 35, 28, 31
39     };
40     private static int SP1[] = {
41         0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 4, 0x10000, 1024, 0x1010400, 
42         0x1010404, 1024, 0x1000404, 0x1010004, 0x1000000, 4, 1028, 0x1000400, 0x1000400, 0x10400, 
43         0x10400, 0x1010000, 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 1028, 
44         0x10404, 0x1000000, 0x10000, 0x1010404, 4, 0x1010000, 0x1010400, 0x1000000, 0x1000000, 1024, 
45         0x1010004, 0x10000, 0x10400, 0x1000004, 1024, 4, 0x1000404, 0x10404, 0x1010404, 0x10004, 
46         0x1010000, 0x1000404, 0x1000004, 1028, 0x10404, 0x1010400, 1028, 0x1000400, 0x1000400, 0, 
47         0x10004, 0x10400, 0, 0x1010004
48     };
49     private static int SP2[] = {
50         0x80108020, 0x80008000, 32768, 0x108020, 0x100000, 32, 0x80100020, 0x80008020, 0x80000020, 0x80108020, 
51         0x80108000, 0x80000000, 0x80008000, 0x100000, 32, 0x80100020, 0x108000, 0x100020, 0x80008020, 0, 
52         0x80000000, 32768, 0x108020, 0x80100000, 0x100020, 0x80000020, 0, 0x108000, 32800, 0x80108000, 
53         0x80100000, 32800, 0, 0x108020, 0x80100020, 0x100000, 0x80008020, 0x80100000, 0x80108000, 32768, 
54         0x80100000, 0x80008000, 32, 0x80108020, 0x108020, 32, 32768, 0x80000000, 32800, 0x80108000, 
55         0x100000, 0x80000020, 0x100020, 0x80008020, 0x80000020, 0x100020, 0x108000, 0, 0x80008000, 32800, 
56         0x80000000, 0x80100020, 0x80108020, 0x108000
57     };
58     private static int SP3[] = {
59         520, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008, 
60         0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 520, 0x8000000, 8, 0x8020200, 512, 
61         0x20200, 0x8020000, 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 8, 0x8020208, 
62         512, 0x8000000, 0x8020200, 0x8000000, 0x20008, 520, 0x20000, 0x8020200, 0x8000200, 0, 
63         512, 0x20008, 0x8020208, 0x8000200, 0x8000008, 512, 0, 0x8020008, 0x8000208, 0x20000, 
64         0x8000000, 0x8020208, 8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 520, 0x8020000, 
65         0x20208, 8, 0x8020008, 0x20200
66     };
67     private static int SP4[] = {
68         0x802001, 8321, 8321, 128, 0x802080, 0x800081, 0x800001, 8193, 0, 0x802000, 
69         0x802000, 0x802081, 129, 0, 0x800080, 0x800001, 1, 8192, 0x800000, 0x802001, 
70         128, 0x800000, 8193, 8320, 0x800081, 1, 8320, 0x800080, 8192, 0x802080, 
71         0x802081, 129, 0x800080, 0x800001, 0x802000, 0x802081, 129, 0, 0, 0x802000, 
72         8320, 0x800080, 0x800081, 1, 0x802001, 8321, 8321, 128, 0x802081, 129, 
73         1, 8192, 0x800001, 8193, 0x802080, 0x800081, 8193, 8320, 0x800000, 0x802001, 
74         128, 0x800000, 8192, 0x802080
75     };
76     private static int SP5[] = {
77         256, 0x2080100, 0x2080000, 0x42000100, 0x80000, 256, 0x40000000, 0x2080000, 0x40080100, 0x80000, 
78         0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000, 0x40080000, 0, 
79         0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000, 
80         0x42000000, 0x80100, 0x80000, 0x42000100, 256, 0x2000000, 0x40000000, 0x2080000, 0x42000100, 0x40080100, 
81         0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 256, 0x2000000, 0x42080000, 0x42080100, 0x80100, 
82         0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 
83         0, 0x40080000, 0x2080100, 0x40000100
84     };
85     private static int SP6[] = {
86         0x20000010, 0x20400000, 16384, 0x20404010, 0x20400000, 16, 0x20404010, 0x400000, 0x20004000, 0x404010, 
87         0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 16400, 0, 0x400010, 0x20004010, 16384, 
88         0x404000, 0x20004010, 16, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 16400, 0x404000, 
89         0x20404000, 0x20000000, 0x20004000, 16, 0x20400010, 0x404000, 0x20404010, 0x400000, 16400, 0x20000010, 
90         0x400000, 0x20004000, 0x20000000, 16400, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000, 
91         0, 0x20400010, 16, 16384, 0x20400000, 0x404010, 16384, 0x400010, 0x20004010, 0, 
92         0x20404000, 0x20000000, 0x400010, 0x20004010
93     };
94     private static int SP7[] = {
95         0x200000, 0x4200002, 0x4000802, 0, 2048, 0x4000802, 0x200802, 0x4200800, 0x4200802, 0x200000, 
96         0, 0x4000002, 2, 0x4000000, 0x4200002, 2050, 0x4000800, 0x200802, 0x200002, 0x4000800, 
97         0x4000002, 0x4200000, 0x4200800, 0x200002, 0x4200000, 2048, 2050, 0x4200802, 0x200800, 2, 
98         0x4000000, 0x200800, 0x4000000, 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 2, 
99         0x200002, 0x4000000, 0x4000800, 0x200000, 0x4200800, 2050, 0x200802, 0x4200800, 2050, 0x4000002, 
100         0x4200802, 0x4200000, 0x200800, 0, 2, 0x4200802, 0, 0x200802, 0x4200000, 2048, 
101         0x4000002, 0x4000800, 2048, 0x200002
102     };
103     private static int SP8[] = {
104         0x10001040, 4096, 0x40000, 0x10041040, 0x10000000, 0x10001040, 64, 0x10000000, 0x40040, 0x10040000, 
105         0x10041040, 0x41000, 0x10041000, 0x41040, 4096, 64, 0x10040000, 0x10000040, 0x10001000, 4160, 
106         0x41000, 0x40040, 0x10040040, 0x10041000, 4160, 0, 0, 0x10040040, 0x10000040, 0x10001000, 
107         0x41040, 0x40000, 0x41040, 0x40000, 0x10041000, 4096, 64, 0x10040040, 4096, 0x41040, 
108         0x10001000, 64, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040, 
109         0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 4160, 
110         4160, 0x40040, 0x10000000, 0x10041000
111     };
112
113     public DesCipher(byte abyte0[]) {
114         encryptKeys = new int[32];
115         decryptKeys = new int[32];
116         tempInts = new int[2];
117         setKey(abyte0);
118     }
119
120     public void setKey(byte abyte0[]) {
121         deskey(abyte0, true, encryptKeys);
122         deskey(abyte0, false, decryptKeys);
123     }
124
125     private void deskey(byte abyte0[], boolean flag, int ai[]) {
126         int ai1[] = new int[56];
127         int ai2[] = new int[56];
128         int ai3[] = new int[32];
129         for (int j = 0; j < 56; j++) {
130             byte byte0 = pc1[j];
131             int l1 = byte0 & 7;
132             ai1[j] = (abyte0[byte0 >>> 3] & bytebit[l1]) == 0 ? 0 : 1;
133         }
134
135         for (int i = 0; i < 16; i++) {
136             int i2;
137             if (flag)
138                 i2 = i << 1;
139             else
140                 i2 = 15 - i << 1;
141             int j2 = i2 + 1;
142             ai3[i2] = ai3[j2] = 0;
143             for (int k = 0; k < 28; k++) {
144                 int j1 = k + totrot[i];
145                 if (j1 < 28)
146                     ai2[k] = ai1[j1];
147                 else
148                     ai2[k] = ai1[j1 - 28];
149             }
150
151             for (int l = 28; l < 56; l++) {
152                 int k1 = l + totrot[i];
153                 if (k1 < 56)
154                     ai2[l] = ai1[k1];
155                 else
156                     ai2[l] = ai1[k1 - 28];
157             }
158
159             for (int i1 = 0; i1 < 24; i1++) {
160                 if (ai2[pc2[i1]] != 0)
161                     ai3[i2] |= bigbyte[i1];
162                 if (ai2[pc2[i1 + 24]] != 0)
163                     ai3[j2] |= bigbyte[i1];
164             }
165
166         }
167
168         cookey(ai3, ai);
169     }
170
171     private void cookey(int ai[], int ai1[]) {
172         int i1 = 0;
173         int k = 0;
174         int l = 0;
175         for (; i1 < 16; i1++) {
176             int i = ai[k++];
177             int j = ai[k++];
178             ai1[l] = (i & 0xfc0000) << 6;
179             ai1[l] |= (i & 0xfc0) << 10;
180             ai1[l] |= (j & 0xfc0000) >>> 10;
181             ai1[l] |= (j & 0xfc0) >>> 6;
182             l++;
183             ai1[l] = (i & 0x3f000) << 12;
184             ai1[l] |= (i & 0x3f) << 16;
185             ai1[l] |= (j & 0x3f000) >>> 4;
186             ai1[l] |= j & 0x3f;
187             l++;
188         }
189
190     }
191
192     public void encrypt(byte abyte0[], int i, byte abyte1[], int j) {
193         squashBytesToInts(abyte0, i, tempInts, 0, 2);
194         des(tempInts, tempInts, encryptKeys);
195         spreadIntsToBytes(tempInts, 0, abyte1, j, 2);
196     }
197
198     public void decrypt(byte abyte0[], int i, byte abyte1[], int j) {
199         squashBytesToInts(abyte0, i, tempInts, 0, 2);
200         des(tempInts, tempInts, decryptKeys);
201         spreadIntsToBytes(tempInts, 0, abyte1, j, 2);
202     }
203
204     private void des(int ai[], int ai1[], int ai2[]) {
205         int j1 = 0;
206         int l = ai[0];
207         int k = ai[1];
208         int j = (l >>> 4 ^ k) & 0xf0f0f0f;
209         k ^= j;
210         l ^= j << 4;
211         j = (l >>> 16 ^ k) & 0xffff;
212         k ^= j;
213         l ^= j << 16;
214         j = (k >>> 2 ^ l) & 0x33333333;
215         l ^= j;
216         k ^= j << 2;
217         j = (k >>> 8 ^ l) & 0xff00ff;
218         l ^= j;
219         k ^= j << 8;
220         k = k << 1 | k >>> 31 & 1;
221         j = (l ^ k) & 0xaaaaaaaa;
222         l ^= j;
223         k ^= j;
224         l = l << 1 | l >>> 31 & 1;
225         for (int i1 = 0; i1 < 8; i1++) {
226             j = k << 28 | k >>> 4;
227             j ^= ai2[j1++];
228             int i = SP7[j & 0x3f];
229             i |= SP5[j >>> 8 & 0x3f];
230             i |= SP3[j >>> 16 & 0x3f];
231             i |= SP1[j >>> 24 & 0x3f];
232             j = k ^ ai2[j1++];
233             i |= SP8[j & 0x3f];
234             i |= SP6[j >>> 8 & 0x3f];
235             i |= SP4[j >>> 16 & 0x3f];
236             i |= SP2[j >>> 24 & 0x3f];
237             l ^= i;
238             j = l << 28 | l >>> 4;
239             j ^= ai2[j1++];
240             i = SP7[j & 0x3f];
241             i |= SP5[j >>> 8 & 0x3f];
242             i |= SP3[j >>> 16 & 0x3f];
243             i |= SP1[j >>> 24 & 0x3f];
244             j = l ^ ai2[j1++];
245             i |= SP8[j & 0x3f];
246             i |= SP6[j >>> 8 & 0x3f];
247             i |= SP4[j >>> 16 & 0x3f];
248             i |= SP2[j >>> 24 & 0x3f];
249             k ^= i;
250         }
251
252         k = k << 31 | k >>> 1;
253         j = (l ^ k) & 0xaaaaaaaa;
254         l ^= j;
255         k ^= j;
256         l = l << 31 | l >>> 1;
257         j = (l >>> 8 ^ k) & 0xff00ff;
258         k ^= j;
259         l ^= j << 8;
260         j = (l >>> 2 ^ k) & 0x33333333;
261         k ^= j;
262         l ^= j << 2;
263         j = (k >>> 16 ^ l) & 0xffff;
264         l ^= j;
265         k ^= j << 16;
266         j = (k >>> 4 ^ l) & 0xf0f0f0f;
267         l ^= j;
268         k ^= j << 4;
269         ai1[0] = k;
270         ai1[1] = l;
271     }
272
273     public static void squashBytesToInts(byte abyte0[], int i, int ai[], int j, int k) {
274         for (int l = 0; l < k; l++)
275             ai[j + l] = (abyte0[i + l * 4] & 0xff) << 24 | (abyte0[i + l * 4 + 1] & 0xff) << 16 | (abyte0[i + l * 4 + 2] & 0xff) << 8 | abyte0[i + l * 4 + 3] & 0xff;
276
277     }
278
279     public static void spreadIntsToBytes(int ai[], int i, byte abyte0[], int j, int k) {
280         for (int l = 0; l < k; l++) {
281             abyte0[j + l * 4] = (byte)(ai[i + l] >>> 24);
282             abyte0[j + l * 4 + 1] = (byte)(ai[i + l] >>> 16);
283             abyte0[j + l * 4 + 2] = (byte)(ai[i + l] >>> 8);
284             abyte0[j + l * 4 + 3] = (byte)ai[i + l];
285         }
286
287     }
288
289 }