• 如何使用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));
  • 相关阅读:
    JS 循环遍历json
    客户端获取ip
    jquery 常用获取值得方法汇总
    C# MATLAB混合编程
    java设计模式之抽象工厂模式学习
    java设计模式之工厂模式学习
    java设计模式之装饰者模式学习
    本周任务
    模仿jquery的data
    js中random的应用
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/7694195.html
Copyright © 2020-2023  润新知