如何使用CryptoJS配合Java进行AES加密和解密

注意

1. PKCS5PaddingPKCS7Padding是一样的

2. 加密时使用的key和iv要转换成base64格式

 

一、前端

1.函数

    function encrypt (msg, key, iv) {
        return  CryptoJS.AES.encrypt(msg,  key, {
            iv: iv,
            padding: CryptoJS.pad.Pkcs7,
            mode: CryptoJS.mode.CBC
        });
    }


    function decrypt (cipherText, key, iv) {
        return  CryptoJS.AES.decrypt({ ciphertext: cipherText }, key, {
            iv: iv,
            padding: CryptoJS.pad.Pkcs7,
            mode: CryptoJS.mode.CBC

        });
    }

 

2. 示例

            var key =    CryptoJS.enc.Base64.parse('ZGIyMTM5NTYxYzlmZTA2OA==');
            var iv =    CryptoJS.enc.Base64.parse('ZGIyMTM5NTYxYzlmZTA2OA==');

            var encrypted = encrypt('Hello World', key, iv);
            var cipherText = encrypted.ciphertext.toString();
            //java 使用 34439a96e68b129093105b67de81c0fc
            console.log(cipherText);

            // 拿到字符串类型的密文需要先将其用Hex方法parse一下
            var cipherTextHexStr = CryptoJS.enc.Hex.parse(cipherText);

            // 将密文转为Base64的字符串
            // 只有Base64类型的字符串密文才能对其进行解密
            var cipherTextBase64Str = CryptoJS.enc.Base64.stringify(cipherTextHexStr);

            //下面三种解密都可以
            var decrypted = CryptoJS.AES.decrypt(cipherTextBase64Str, key, {
                iv: iv,
                padding: CryptoJS.pad.Pkcs7,
                mode: CryptoJS.mode.CBC
            });

            decrypted = decrypt(CryptoJS.enc.Base64.parse(cipherTextBase64Str), key, iv);
            decrypted = decrypt(cipherTextHexStr, key, iv);

            console.log(decrypted.toString(CryptoJS.enc.Utf8));
            

 

 

二、后端

1.函数

    public static byte[] AES_CBC_Decrypt(byte[] data, byte[] key, byte[] iv) throws Exception{
        Cipher cipher = getCipher(Cipher.DECRYPT_MODE, key, iv);
        return cipher.doFinal(data);
    }

    private static Cipher getCipher(int mode, byte[] key, byte[] iv) throws Exception{
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//因为AES的加密块大小是128bit(16byte), 所以key是128、192、256bit无关 //System.out.println("cipher.getBlockSize(): " + cipher.getBlockSize());
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); cipher.init(mode, secretKeySpec, new IvParameterSpec(iv)); return cipher; }

 

2.示例

        //传给crypto的key、iv要使用base64格式
        //ZGIyMTM5NTYxYzlmZTA2OA==
        byte[] bytes = "db2139561c9fe068".getBytes();
        String base64Str = Base64.encodeBase64String(bytes);
        System.out.println(base64Str);

        String crypto = "34439a96e68b129093105b67de81c0fc";
        data = Hex.decodeHex(crypto.toCharArray());
        s = AES_CBC_Decrypt(data, bytes, bytes);
        System.out.println(new String(s));

 

posted @ 2017-10-20 09:41  等风来。。  Views(24444)  Comments(0Edit  收藏  举报
------------------------------------------------------------------------------------------------------------ --------------- 欢迎联系 x.guan.ling@gmail.com--------------- ------------------------------------------------------------------------------------------------------------