使用前端的AES进行数据加密,其实加密解密的结果在 pcks7 和 后端java 中的 pkcs5 是相一致的
前端代码如下:
var CryptoJS = require("crypto-js");
var key = CryptoJS.enc.Utf8.parse("2550e1be21ba49a1");
var word = CryptoJS.enc.Utf8.parse("123456");
var keyStr = "LmRayww3y/dB2CZxnfdr4w==";
var enKenStr = CryptoJS.enc.Utf8.parse(keyStr);
var en = CryptoJS.AES.encrypt(word, key,{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
var de = CryptoJS.AES.decrypt(keyStr, key,{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
})
// en LmRayww3y/dB2CZxnfdr4w==
console.log("en" , en.toString());
// de 123456
console.log("de" , CryptoJS.enc.Utf8.stringify(de).toString());
// true
console.log(en.toString() == keyStr)
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
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
后端代码如下:
// 加密
public static String aesEncrypt(String str, String key) throws Exception {
if (str == null || key == null) return null;
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
return new BASE64Encoder().encode(bytes);
}
// 解密
public static String aesDecrypt(String str, String key) throws Exception {
if (str == null || key == null) return null;
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
byte[] bytes = new BASE64Decoder().decodeBuffer(str);
bytes = cipher.doFinal(bytes);
return new String(bytes, "utf-8");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20