Java DES 加密工具类

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.util.HashMap;
import java.util.Map;

/**
* 加密
*
* @author
* @since 2021-08-30 14:14
**/
public class DesPlus {

private static final String strDefaultKey = "dhyPksiF";

private static final Logger logger = LoggerFactory.getLogger(DesPlus.class);

private final Map<String, Cipher> encryptMap = new HashMap<String, Cipher>();

private final Map<String, Cipher> decryptMap = new HashMap<String, Cipher>();

private static final ThreadLocal<DesPlus> localDesPlus = new ThreadLocal<DesPlus>() {

@Override
protected DesPlus initialValue() {
return new DesPlus();
}
};

private DesPlus() {
}

public static DesPlus getInstance() {

return localDesPlus.get();
}

public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";

/**
* DES算法,加密
*
* @param data 待加密字符串
* @return 加密后的字节数组,一般结合Base64编码使用
* @throws Exception 异常
*/
public String encode(String data) throws Exception {
return encode(strDefaultKey, data.getBytes());
}

/**
* DES算法,加密
*
* @param data 待加密字符串
* @param key 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
* @throws Exception 异常
*/
public String encode(String key, String data) throws Exception {
logger.info("key:{}|data:{}|start", key, data);
String encode = encode(key, data.getBytes());
logger.info("key:{}|data:{}|end。{}", key, data, encode);
return encode;
}

/**
* DES算法,加密
*
* @param data 待加密字符串
* @param key 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
* @throws Exception 异常
*/
public String encode(String key, byte[] data) throws Exception {
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
//key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
IvParameterSpec iv = new IvParameterSpec(key.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

byte[] bytes = cipher.doFinal(data);

return byteArrayToHexString(bytes);
} catch (Exception e) {
logger.error("key:{}|data:{}|encode error", key, new String(data), e);
}
return "";
}

public String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++)
resultSb.append(byteToHexString(b[i]));

return resultSb.toString();
}

private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}

/**
* DES算法,解密
*
* @param data 待解密字符串
* @param key 解密私钥,长度不能够小于8位
* @return 解密后的字节数组
*/
public byte[] decode(String key, byte[] data) {
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
//key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
IvParameterSpec iv = new IvParameterSpec(key.getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
return cipher.doFinal(data);
} catch (Exception e) {
logger.error("key:{}|data:{}|decode error", key, new String(data), e);
}
return new byte[0];
}

/**
* 获取编码后的值
*
* @param data 解码数据
* @return 解码后的数据
*/
public String decodeValue(String data) {
return decodeValue(strDefaultKey, data);
}

/**
* 获取编码后的值
*
* @param key
* @param data
* @return
* @throws Exception
*/
public String decodeValue(String key, String data) {
byte[] datas;
String value = null;
try {
datas = decode(key, hexStr2ByteArr(data));
value = new String(datas);
} catch (Exception e) {
value = "";
}
return value;
}

public byte[] hexStr2ByteArr(String strIn) {

byte[] arrB = strIn.getBytes();

int iLen = arrB.length;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];

for (int i = 0; i < iLen; i = i + 2) {

String strTmp = new String(arrB, i, 2);

arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);

}

return arrOut;

}

public static void main(String[] args) throws Exception {
DesPlus instance = getInstance();
String encode = instance.encode("18000000000");
System.out.println(encode);
System.out.println(instance.decodeValue(encode));
}
}