AESUtil.java
4.06 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
package com.topdraw.util;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.Key;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
@Slf4j
public class AESUtil {
public static String encrypt(String data, String key) {
String strResult = null;
try {
SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
AlgorithmParameterSpec aps = new IvParameterSpec("0123456789ABCDEF".getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, aps);
byte[] result = cipher.doFinal(data.getBytes("UTF-8"));
StringBuilder sb_hex = new StringBuilder();
for (byte b : result) {
sb_hex.append(String.format("%02x", b));
}
strResult = sb_hex.toString();
} catch (Exception e) {
}
return strResult;
}
public static String decrypt(String encryptedData, String key) {
String strResult = null;
try {
int len = encryptedData.length() / 2;
byte[] data = new byte[len];
for (int i = 0; i < len; i++) {
String str = encryptedData.substring(i * 2, i * 2 + 2);
data[i] = Integer.valueOf(str, 16).byteValue();
}
SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
AlgorithmParameterSpec aps = new IvParameterSpec("0123456789ABCDEF".getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, aps);
byte[] result = cipher.doFinal(data);
strResult = new String(result, "UTF-8");
} catch (Exception e) {
}
return strResult;
}
private static final String AES ="AES";
private static final String AES_CBC_PKCS7 ="AES/CBC/PKCS7Padding";
public static String decryptJsUserInfo(String encryptedData,String iv,String sessionKey) {
try {
byte[] data = java.util.Base64.getDecoder().decode(encryptedData);
byte[] aseKey = java.util.Base64.getDecoder().decode(sessionKey);
byte[] ivData = java.util.Base64.getDecoder().decode(iv);
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance(AES_CBC_PKCS7);
Key sKeySpec = new SecretKeySpec(aseKey, AES);
AlgorithmParameterSpec aps = new IvParameterSpec(ivData);
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, aps);// 初始化
byte[] result = cipher.doFinal(data);
return new String(result);
} catch (Exception e) {
log.error("decryptJsUserInfo:"+ e);
return null;
}
}
public static JSONObject decryptJsUserPhone(String encryptedData, String iv, String key) {
byte[] dataByte = java.util.Base64.getDecoder().decode(encryptedData);
// 加密秘钥
byte[] keyByte = java.util.Base64.getDecoder().decode(key);
// 偏移量
byte[] ivByte = java.util.Base64.getDecoder().decode(iv);
try {
// 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
int base = 16;
if (keyByte.length % base != 0) {
int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
keyByte = temp;
}
// 初始化
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding","BC");
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
parameters.init(new IvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
byte[] resultByte = cipher.doFinal(dataByte);
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, "UTF-8");
return JSONObject.parseObject(result);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}