- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDesUtil.java
More file actions
Latest commit
122 lines (112 loc) · 3.94 KB
/
Copy pathDesUtil.java
File metadata and controls
122 lines (112 loc) · 3.94 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
packagecom.realmo.utils;
importjava.io.UnsupportedEncodingException;
importjava.security.InvalidKeyException;
importjava.security.Key;
importjava.security.NoSuchAlgorithmException;
importjava.security.SecureRandom;
importjava.security.spec.InvalidKeySpecException;
importjavax.crypto.Cipher;
importjavax.crypto.KeyGenerator;
importjavax.crypto.SecretKey;
importjavax.crypto.SecretKeyFactory;
importjavax.crypto.spec.DESKeySpec;
importjavax.crypto.spec.IvParameterSpec;
/**
* DES加密解密
*/
publicclassDesUtil {
privatestaticfinalStringALGORITHM = "DES";
privatestaticfinalStringKEY = "12345678";
publicstaticfinalStringLOCAL_KEY = "12344321";//本地加密秘钥
/**
* 初始化生成密钥
*/
privatestaticStringinitKey(Stringseed) {
try {
SecureRandomrandom = null;
if (seed != null) {
random = newSecureRandom(seed.getBytes());
} else {
random = newSecureRandom();
}
// 初始化密钥生成器
KeyGeneratorkeyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(random);
SecretKeykey = keyGenerator.generateKey();
// 获取密钥二进制字节数组
byte[] keyBytes = key.getEncoded();
// 转换为Base64编码内容
returnBase64Util.encodeByte(keyBytes);
} catch (NoSuchAlgorithmExceptione) {
e.printStackTrace();
}
returnnull;
}
/**
* 加密
*/
publicstaticStringencrypt(Stringsrc) {
returnencrypt(src, KEY);
}
/**
* 加密 传入秘钥
*/
publicstaticStringencrypt(Stringsrc, StringkeyString) {
try {
// 获取密钥 - Key应该是Base64编码后的密钥的字符串
// 把字符串转换为密钥对象
Keykey = getKey(keyString);
IvParameterSpeczeroIv = newIvParameterSpec(KEY.getBytes("UTF-8"));
Ciphercipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
byte[] bytes = cipher.doFinal(src.getBytes("UTF-8"));
returnBase64Util.encodeByte(bytes);
} catch (Exceptione) {
e.printStackTrace();
returne.getMessage();
}
}
/**
* 解密
*/
publicstaticStringdecrypt(Stringdest){
try {
returndecrypt(dest, KEY);
} catch (Exceptione) {
e.printStackTrace();
returndest.trim();
}
}
publicstaticStringdecrypt(Stringdest, StringkeyString) throwsException {
Keykey = getKey(keyString);
IvParameterSpeczeroIv = newIvParameterSpec(KEY.getBytes("UTF-8"));
Ciphercipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
// 把加密数据的Base64串进行解码,转换为字节数组
byte[] bytes = Base64Util.decodeToBytes(dest);
// 明文的字节数组
byte[] decryptBytes = cipher.doFinal(bytes);
returnnewString(decryptBytes, "UTF-8");
}
/**
* 通过密钥字符串生成 密钥Key
*/
privatestaticKeygetKey(StringkeyString) {
try {
// String key = Base64Util.decode(keyString);
DESKeySpeckeySpec = newDESKeySpec(keyString.getBytes("UTF-8"));
SecretKeyFactorykeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
KeysecretKey = keyFactory.generateSecret(keySpec);
returnsecretKey;
} catch (NoSuchAlgorithmExceptione) {
e.printStackTrace();
} catch (InvalidKeyExceptione) {
e.printStackTrace();
} catch (UnsupportedEncodingExceptione) {
e.printStackTrace();
} catch (InvalidKeySpecExceptione) {
e.printStackTrace();
}
returnnull;
}
}