AesEncryptUtils.java
4.11 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package cn.com.glsx.smart.common.utils;
import cn.com.glsx.smart.common.exception.ChejiaBusinessException;
import cn.com.glsx.smart.common.exception.ResultEnum;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
/**
* @author fengzhi
* @version V1.0
* @Description: AES加密工具类
*/
@Component
public class AesEncryptUtils {
/**
* crep的解密key
* 可配置到Constant中,并读取配置文件注入,16位
*/
@Value("${aesencrypt.key}")
private String KEY;
/**
* 参数分别代表
* 算法名称/加密模式/数据填充方式
*/
@Value("${aesencrypt.algorithmstr}")
private String ALGORITHMSTR;
/**
* secretKey的解密key
*/
@Value("${aesencrypt.secret.key}")
private String SECRET_KEY;
/**
* 加密
*
* @param content 加密的字符串
* @param encryptKey 加密的key值
*/
public String encrypt(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
// 采用base64算法进行转码,避免出现中文乱码
return Base64.encodeBase64String(b);
}
/**
* 解密
*
* @param encryptStr 解密的字符串
* @param decryptKey 解密的key值
*/
public String decrypt(String encryptStr, String decryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
// 采用base64算法进行转码,避免出现中文乱码
byte[] encryptBytes = Base64.decodeBase64(encryptStr);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
/**
* 加密crep/secretKey
* @param content 明文
*/
public String encrypt(String content) {
if(StringUtils.isBlank(content))
return "";
String encrypt_str;
try {
encrypt_str = encrypt(content, KEY);
} catch (Exception e) {
throw ChejiaBusinessException.create(ResultEnum.CAR_KEY_CREP_SECRET_ENCRYPT);
}
return encrypt_str;
}
/**
* 解密crep/secretKey
* @param encryptStr 密文
*/
public String decrypt(String encryptStr) {
if(StringUtils.isBlank(encryptStr))
return "";
String decrypt_str;
try {
decrypt_str = decrypt(encryptStr, KEY);
} catch (Exception e) {
throw ChejiaBusinessException.create(ResultEnum.CAR_KEY_CREP_SECRET_DECRYPT);
}
return decrypt_str;
}
/**
* 数据库加密secretKey
* @param content 明文
*/
public String encryptSecret(String content) {
if(StringUtils.isBlank(content))
return "";
String secretKey_encrypt;
try {
secretKey_encrypt = encrypt(content, SECRET_KEY);
} catch (Exception e) {
throw ChejiaBusinessException.create(ResultEnum.CAR_KEY_SECRET_ENCRYPT);
}
return secretKey_encrypt;
}
/**
* 数据库解密secretKey
* @param encryptStr 密文
*/
public String decryptSecret(String encryptStr) {
if(StringUtils.isBlank(encryptStr))
return "";
String secretKey;
try {
secretKey = decrypt(encryptStr, SECRET_KEY);
} catch (Exception e) {
throw ChejiaBusinessException.create(ResultEnum.CAR_KEY_SECRET_DECRYPT);
}
return secretKey;
}
}