DES3.java
3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.sitech.ibnms.util;
import java.math.BigInteger;
import java.util.Scanner;
public final class DES3 extends Cipher {
DES des1 = new DES();
DES des2 = new DES();
DES des3 = new DES();
public synchronized void encrypt(byte[] src, int srcOff, byte[] dest,
int destOff, int len) {
des1.encrypt(src, srcOff, dest, destOff, len);
des2.decrypt(dest, destOff, dest, destOff, len);
des3.encrypt(dest, destOff, dest, destOff, len);
}
public synchronized void decrypt(byte[] src, int srcOff, byte[] dest,
int destOff, int len) {
des3.decrypt(src, srcOff, dest, destOff, len);
des2.encrypt(dest, destOff, dest, destOff, len);
des1.decrypt(dest, destOff, dest, destOff, len);
}
public void setKey(byte[] key) {
byte[] subKey = new byte[8];
des1.setKey(key);
System.arraycopy(key, 8, subKey, 0, 8);
des2.setKey(subKey);
System.arraycopy(key, 16, subKey, 0, 8);
des3.setKey(subKey);
}
static byte[] key = { (byte) 0x12, (byte) 0x34, (byte) 0x45, (byte) 0x78,
(byte) 0x87, (byte) 0x34, (byte) 0x43, (byte) 0x23, (byte) 0x89,
(byte) 0x55, (byte) 0x01, (byte) 0x77, (byte) 0x87, (byte) 0xef,
(byte) 0x43, (byte) 0x78, (byte) 0xcd, (byte) 0x65, (byte) 0x9a,
(byte) 0x21, (byte) 0x12, (byte) 0xab, (byte) 0x56, (byte) 0x78, };
static public String decrypt(String source) {
byte[] txt = new byte[24];
BigInteger t = new BigInteger(source, 16);
byte[] b = t.toByteArray();
if (b[0] == 0) {
System.arraycopy(b, 1, txt, 0, b.length - 1);
} else {
System.arraycopy(b, 0, txt, 0, b.length);
}
byte[] dec;
DES3 cipher = new DES3();
cipher.setKey(key);
dec = cipher.decrypt(txt);
byte[] dect = new byte[dec.length];
int j = 0;
for (int i = 0; i < dec.length; i++) {
if (dec[i] > 0) {
dect[j++] = dec[i];
}
}
String rt = (new String(dect, 0, j));
return rt;
}
static public void displayString(String source) {
byte[] aa = source.getBytes();
System.out.print("BEGIN:");
for (int i = 0; i < aa.length; i++) {
System.out.print(aa[i]);
System.out.print(" ");
}
System.out.println(":END");
}
static public String encrypt(String source) {
byte[] txt = new byte[24];
byte[] b = source.getBytes();
System.arraycopy(b, 0, txt, 0, b.length);
byte[] enc;
DES3 cipher = new DES3();
cipher.setKey(key);
enc = cipher.encrypt(txt);
return printHex(enc);
}
static String printHex(byte[] buf) {
byte[] out = new byte[buf.length + 1];
out[0] = 0;
System.arraycopy(buf, 0, out, 1, buf.length);
BigInteger big = new BigInteger(out);
return big.toString(16);
}
static String printHex(int i) {
BigInteger b = BigInteger.valueOf((long) i + 0x100000000L);
BigInteger c = BigInteger.valueOf(0x100000000L);
if (b.compareTo(c) != -1)
b = b.subtract(c);
return b.toString(16);
}
private static String read(String prompt) {
Scanner scanner = new Scanner(System.in);
System.out.print(prompt);
return scanner.nextLine();
}
public static void main(String[] args) {
System.out.println("******DES3 Util********");
String type = read("类型(1:加密, 2:解密):").trim();
String str = read("字符串:").trim();
switch (Integer.parseInt(type)) {
case 1:
System.out.println(DES3.encrypt(str));
break;
case 2:
System.out.println(DES3.decrypt(str));
break;
default:
break;
}
System.out.println("******DES3 Util********");
}
}