# java AES 32位加密解密
# 1 常用加密32位原因
加密原因
网上很多解密加密是16位的,用32位密钥加密会报java.security.InvalidKeyException: Illegal key size or default parameters
异常错误,因为美国的出口限制,Sun通过权限文件(local_policy.jar、US_export_policy.jar)做了相应限制。因此存在以下一些问题:
- 密钥长度上不能满足需求(如:java.security.InvalidKeyException: Illegal key size or default parameters);
- 部分算法未能支持,如MD4、SHA-224等算法;
- API使用起来还不是很方便;
- 一些常用的进制转换辅助工具未能提供,如Base64编码转换、十六进制编码转换等工具。
# 2 解决方案
解决方案
Oracle在其官方网站上提供了无政策限制权限文件(Unlimited Strength Jurisdiction Policy Files),我们只需要将其部署在JRE环境中,就可以解决限制问题,特别注意:两个目录都要替换
%JDK_Home%\jre\lib\security
目录下,对应覆盖local_policy.jar和US_export_policy.jar两个文件
%JRE_Home%\lib\security
目录下,也需要对应覆盖这两个文件。
# 3 AES工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
/**
* AES常用解密加密工具类
* https://github.com/ourlang
* @author 小林
*/
public class AesUtil {
/**
* 默认的字符编码
*/
private static final String DEFAULT_CHARSET = "utf-8";
/**
* 算法
*/
private static String ALGORITHM = "AES";
/**
* 算法/模式/填充
**/
private static final String CipherMode = "AES/ECB/PKCS5Padding";
/**
* 记录日志
**/
private final static Logger logger = LoggerFactory.getLogger(AesUtil.class);
private AesUtil() {
}
/**
* 解密AES 32位
*
* @param sSrc 解密的内容
* @param secretKey 秘钥
* @return 解密后的明文 数据
*/
public static String decrypt(String sSrc, String secretKey) {
if (secretKey == null) {
logger.error("需要加密的秘钥为空");
return null;
}
try {
byte[] raw = secretKey.getBytes(DEFAULT_CHARSET);
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
// 先用base64解密
byte[] encryptedArr = Base64.getDecoder().decode(sSrc);
byte[] original = cipher.doFinal(encryptedArr);
return new String(original, DEFAULT_CHARSET);
} catch (Exception ex) {
logger.error("AES解密失败", ex);
return null;
}
}
/**
* 加密32位
*
* @param sSrc 需要加密的内容
* @param sKey 秘钥
* @return 加密的内容
*/
public static String encrypt(String sSrc, String sKey) {
if (sKey == null) {
logger.error("需要加密的秘钥为空");
return null;
}
try {
byte[] raw = sKey.getBytes(DEFAULT_CHARSET);
SecretKeySpec skeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(CipherMode);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(sSrc.getBytes(DEFAULT_CHARSET));
return Base64.getEncoder().encodeToString(encrypted);
} catch (Exception ex) {
logger.error("AES加密失败", ex);
return null;
}
}
}
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
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