-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDesTest.java
More file actions
executable file
·92 lines (76 loc) · 2.92 KB
/
Copy pathDesTest.java
File metadata and controls
executable file
·92 lines (76 loc) · 2.92 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
package array;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DesTest {
public static void main(String[] args) {
//test();
// 待加密内容
String str = "88990000";
// 密码,长度要是8的倍数
String password = "88888888";
byte[] result = desCrypto(str.getBytes(), password);
System.out.println("加密后内容为:" + new String(result));
// 直接将如上内容解密
String charSet = "gbk";
byte[] array = new String(result,Charset.forName(charSet)).getBytes(Charset.forName(charSet));
System.out.println(result.length + " " + array.length);
try {
byte[] decryResult = decrypt(result, password);//换成result
System.out.println("解密内容为:" + new String(decryResult));
} catch (Exception e1) {
e1.printStackTrace();
}
}
static void test() {
String str = "yahier";
byte[] data = str.getBytes();
System.out.println("新str:" + new String(data));
byte[] data2 = new String(data).getBytes();
System.out.println("" + new String(data2));
String test = "tests1weipdosgpdos";
byte[] bytes = test.getBytes(Charset.forName("UTF-8"));
String newStr = new String(bytes, Charset.forName("UTF-8"));
}
public static byte[] desCrypto(byte[] datasource, String password) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(password.getBytes());
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
//Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
// 现在,获取数据并加密
// 正式执行加密操作
return cipher.doFinal(datasource);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
private static byte[] decrypt(byte[] src, String password) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom random = new SecureRandom();
// 创建一个DESKeySpec对象
DESKeySpec desKey = new DESKeySpec(password.getBytes());
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES");
//Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
// 真正开始解密操作
return cipher.doFinal(src);
}
}