1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
|
---|
4 | * www.mindbright.se, [email protected]
|
---|
5 | *
|
---|
6 | *****************************************************************************
|
---|
7 | * $Author: mats $
|
---|
8 | * $Date: 1999/07/19 17:13:49 $
|
---|
9 | * $Name: rel1-2-1 $
|
---|
10 | *****************************************************************************/
|
---|
11 | /*
|
---|
12 | * !!! Author's comment: See DES.java for additional copyright-info on DES
|
---|
13 | */
|
---|
14 | package mindbright.security;
|
---|
15 |
|
---|
16 | import java.math.BigInteger;
|
---|
17 |
|
---|
18 | public final class DES3 extends Cipher {
|
---|
19 | DES des1 = new DES();
|
---|
20 | DES des2 = new DES();
|
---|
21 | DES des3 = new DES();
|
---|
22 |
|
---|
23 | public synchronized void encrypt(byte[] src, int srcOff, byte[] dest, int destOff, int len) {
|
---|
24 | des1.encrypt(src, srcOff, dest, destOff, len);
|
---|
25 | des2.decrypt(dest, destOff, dest, destOff, len);
|
---|
26 | des3.encrypt(dest, destOff, dest, destOff, len);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public synchronized void decrypt(byte[] src, int srcOff, byte[] dest, int destOff, int len) {
|
---|
30 | des3.decrypt(src, srcOff, dest, destOff, len);
|
---|
31 | des2.encrypt(dest, destOff, dest, destOff, len);
|
---|
32 | des1.decrypt(dest, destOff, dest, destOff, len);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void setKey(byte[] key) {
|
---|
36 | byte[] subKey = new byte[8];
|
---|
37 | des1.setKey(key);
|
---|
38 | System.arraycopy(key, 8, subKey, 0, 8);
|
---|
39 | des2.setKey(subKey);
|
---|
40 | System.arraycopy(key, 16, subKey, 0, 8);
|
---|
41 | des3.setKey(subKey);
|
---|
42 | }
|
---|
43 |
|
---|
44 | /* !!! DEBUG
|
---|
45 | public static void main(String[] argv) {
|
---|
46 | byte[] key = {
|
---|
47 | (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78,
|
---|
48 | (byte)0x87, (byte)0x65, (byte)0x43, (byte)0x21,
|
---|
49 | (byte)0x44, (byte)0x55, (byte)0x66, (byte)0x77,
|
---|
50 | (byte)0x87, (byte)0x65, (byte)0x43, (byte)0x21,
|
---|
51 | (byte)0x87, (byte)0x65, (byte)0x43, (byte)0x21,
|
---|
52 | (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78,
|
---|
53 | };
|
---|
54 |
|
---|
55 | byte[] txt = {
|
---|
56 | (byte)0x00, (byte)0x11, (byte)0x22, (byte)0x33,
|
---|
57 | (byte)0x44, (byte)0x55, (byte)0x66, (byte)0x77,
|
---|
58 | (byte)0x00, (byte)0x11, (byte)0x22, (byte)0x33,
|
---|
59 | (byte)0x44, (byte)0x55, (byte)0x66, (byte)0x77,
|
---|
60 | (byte)0x00, (byte)0x11, (byte)0x22, (byte)0x33,
|
---|
61 | (byte)0x44, (byte)0x55, (byte)0x66, (byte)0x77
|
---|
62 | };
|
---|
63 |
|
---|
64 | byte[] enc;
|
---|
65 | byte[] dec;
|
---|
66 |
|
---|
67 | System.out.println("key: " + printHex(key));
|
---|
68 | System.out.println("txt: " + printHex(txt));
|
---|
69 |
|
---|
70 | DES3 cipher = new DES3();
|
---|
71 | cipher.setKey(key);
|
---|
72 |
|
---|
73 | enc = cipher.encrypt(txt);
|
---|
74 | System.out.println("enc: " + printHex(enc));
|
---|
75 |
|
---|
76 | cipher = new DES3();
|
---|
77 | cipher.setKey(key);
|
---|
78 |
|
---|
79 | dec = cipher.decrypt(enc);
|
---|
80 |
|
---|
81 | System.out.println("dec: " + printHex(dec));
|
---|
82 | }
|
---|
83 |
|
---|
84 | static String printHex(byte[] buf) {
|
---|
85 | byte[] out = new byte[buf.length + 1];
|
---|
86 | out[0] = 0;
|
---|
87 | System.arraycopy(buf, 0, out, 1, buf.length);
|
---|
88 | BigInteger big = new BigInteger(out);
|
---|
89 | return big.toString(16);
|
---|
90 | }
|
---|
91 | static String printHex(int i) {
|
---|
92 | BigInteger b = BigInteger.valueOf((long)i + 0x100000000L);
|
---|
93 | BigInteger c = BigInteger.valueOf(0x100000000L);
|
---|
94 | if(b.compareTo(c) != -1)
|
---|
95 | b = b.subtract(c);
|
---|
96 | return b.toString(16);
|
---|
97 | }
|
---|
98 |
|
---|
99 | */
|
---|
100 |
|
---|
101 | }
|
---|