DES可逆加密算法

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
package com;

import javax.crypto.Cipher;
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 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 String encrypt(String str) {

return encrypt(str, strDefaultKey);
}

public String encrypt(String str, String key) {

if (str == null) {
return null;
}
if (key == null || key.length() < 8) {
return null;
}
Cipher encryptCipher = encryptMap.get(key);
if (encryptCipher == null) {
try {
Key k = getKey(key);
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, k);

encryptMap.put(key, encryptCipher);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
try {
byte[] bs = str.getBytes("utf-8");
bs = encryptCipher.doFinal(bs);
return byteArrayToHexString(bs);
} catch (Exception e) {
return null;
}
}

public String decrypt(String str) {
return decrypt(str, strDefaultKey);
}


public String decrypt(String str, String key) {
if (str == null) {
return null;
}
if (key == null || key.length() < 8) {
return null;
}
Cipher decryptCipher = decryptMap.get(key);
if (decryptCipher == null) {
try {
Key k = getKey(key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, k);

decryptMap.put(key, decryptCipher);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
try {
byte[] bs = hexStr2ByteArr(str);
bs = decryptCipher.doFinal(bs);
return new String(bs, "utf-8");
} catch (Exception e) {
return str;
}
}


private Key getKey(String key) {
byte[] arrBTmp = key.getBytes();
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key k = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return k;
}

public static 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];
}

public static 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) {
DesPlus desPlus = new DesPlus();

String encrypt = desPlus.encrypt("1500213000000");
System.out.println(encrypt);
String decrypt = desPlus.decrypt(encrypt);
System.out.println(decrypt);
}
}