AES算法实现Java和JS互通加解密

在项目中使用RSA加密解密,但是对于稍微有点长度的明文加密时,RSA就会提示加密内容过长,导致加密失败。所以在项目中对于请求参数过多的接口使用AES加密解密。

1、Java后台代码

01
02
03
04
05
06
07
08
09
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
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
  
import sun.misc.BASE64Decoder;
 
public class AESUtil {
static String key = "0123456789abcdef"//16位
    static String iv = "0123456789abcdef"//16位
  
    public static void main(String args[]) throws Exception {
       System.out.println(encryptAES("*****"));
       System.out.println(decryptAES(encryptAES("123456")));
    }
  
    public static String encryptAES(String data) throws Exception {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding");   //参数分别代表 算法名称/加密模式/数据填充方式
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            return new sun.misc.BASE64Encoder().encode(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
  
    public static String decryptAES(String data) throws Exception {
        try {
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data);
            Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("UTF-8"));
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

2、js实现代码

插件下载:

01
02
03
04
05
06
07
08
09
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
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>AES TEST</title>
</head>
<body>
<p>待加密值:123456RWEQR</p>
<p id="p1"></p>
<p id="p2"></p>
  
<script type="text/javascript" src="js/jquery-1.12.0.min.js"></script>
<script type="text/javascript" src="js/crypto-js.js"></script>
<script type="text/javascript">
    $(function () {
        var data = "123456RWEQR";
        var key = CryptoJS.enc.Latin1.parse('abcdef0123456789');
        var iv = CryptoJS.enc.Latin1.parse('0123456789abcdef');
  
        //加密
        var encrypted = CryptoJS.AES.encrypt(data, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.ZeroPadding
        });
        $("#p1").text("加密结果:"+encrypted.toString());
        console.log(encrypted.toString())
        //解密
        var decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv: iv, padding: CryptoJS.pad.ZeroPadding});
        console.log(decrypted.toString(CryptoJS.enc.Utf8));
        $("#p2").text("解密结果:"+decrypted.toString(CryptoJS.enc.Utf8));
    })
</script>
</body>
</html>

发表评论

欢迎阅读『AES算法实现Java和JS互通加解密|Java、JavaScript、Web设计、加密解密、算法|Nick Tan-梓潼Blog』