解决cryptoJS.AES默认参数加密,java无法解密的问题

前端直接默认参数加密的,没有传iv salt等

const cryptoJS = require("crypto-js");

const encryptedValue = cryptoJS.AES.encrypt(value, secret)

后端代码

前端默认参数加密,后端解密

java 复制代码
 public static byte[][] GenerateKeyAndIV(int keyLength, int ivLength, int iterations, byte[] salt, byte[] password, MessageDigest md) {

        int digestLength = md.getDigestLength();

        int requiredLength = (keyLength + ivLength + digestLength - 1) / digestLength * digestLength;

        byte[] generatedData = new byte[requiredLength];

        int generatedLength = 0;

        try {

            md.reset();

// Repeat process until sufficient data has been generated

            while (generatedLength < keyLength + ivLength) {

// Digest data (last digest if available, password data, salt if available)

                if (generatedLength > 0) {
                    md.update(generatedData, generatedLength - digestLength, digestLength);
                }

                md.update(password);

                if (salt != null) {
                    md.update(salt, 0, 8);
                }

                md.digest(generatedData, generatedLength, digestLength);

// additional rounds

                for (int i = 1; i < iterations; i++) {

                    md.update(generatedData, generatedLength, digestLength);

                    md.digest(generatedData, generatedLength, digestLength);

                }

                generatedLength += digestLength;

            }

// Copy key and IV into separate byte arrays

            byte[][] result = new byte[2][];

            result[0] = Arrays.copyOfRange(generatedData, 0, keyLength);

            if (ivLength > 0) {
                result[1] = Arrays.copyOfRange(generatedData, keyLength, keyLength + ivLength);
            }

            return result;

        } catch (DigestException e) {

            throw new RuntimeException(e);

        } finally {

// Clean out temporary data

            Arrays.fill(generatedData, (byte) 0);

        }

    }




//解密方法
 public static byte[] aesDecrypt(String encryptedText, String secret) {
        try {
            byte[] cipherData = cn.hutool.core.codec.Base64.decode(encryptedText);

            byte[] saltData = Arrays.copyOfRange(cipherData, 8, 16);

            MessageDigest md5 = MessageDigest.getInstance("MD5");

            final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes(StandardCharsets.UTF_8), md5);

            SecretKeySpec key = new SecretKeySpec(keyAndIV[0], "AES");

            IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);

            byte[] encrypted = Arrays.copyOfRange(cipherData, 16, cipherData.length);

            Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding");

            aesCBC.init(Cipher.DECRYPT_MODE, key, iv);
            return aesCBC.doFinal(encrypted);
        } catch (Exception e) {
            log.error("aesDecrypt errorencryptedText" + e.getMessage(), e);
            return null;
        }
    }

后端加密

java 复制代码
 public static byte[] aesEncrypt(String datastr, String secret) {
        try {
            byte[] input = datastr.getBytes();
            // 以下两个变量不可修改,否则java加密的,node无法解密
            byte[] saltData = "DFބf$t:".getBytes();
            byte[] preData = "Salted__".getBytes();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, secret.getBytes(StandardCharsets.UTF_8), md5);

            SecretKeySpec key = new SecretKeySpec(keyAndIV[0], "AES");

            IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);
            Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding");

            aesCBC.init(Cipher.ENCRYPT_MODE, key, iv);

            byte[] encrypt = aesCBC.doFinal(input);
            byte[] encryptedData = addBytes(saltData, encrypt);
            return Base64.getEncoder().encode((addBytes(preData, encryptedData)));
        } catch (Exception e) {
            log.error("aesEncrypt error.param={}", datastr, e);
            return null;
        }
    }


    public static byte[] addBytes(byte[] data1, byte[] data2) {

        byte[] data3 = new byte[data1.length + data2.length];

        System.arraycopy(data1, 0, data3, 0, data1.length);

        System.arraycopy(data2, 0, data3, data1.length, data2.length);

        return data3;

    }
相关推荐
俏布斯3 分钟前
算法日常记录
java·算法·leetcode
独好紫罗兰6 分钟前
洛谷题单3-P5719 【深基4.例3】分类平均-python-流程图重构
开发语言·python·算法
27669582927 分钟前
美团民宿 mtgsig 小程序 mtgsig1.2 分析
java·python·小程序·美团·mtgsig·mtgsig1.2·美团民宿
爱的叹息8 分钟前
Java 连接 Redis 的驱动(Jedis、Lettuce、Redisson、Spring Data Redis)分类及对比
java·redis·spring
程序猿chen18 分钟前
《JVM考古现场(十五):熵火燎原——从量子递归到热寂晶壁的代码涅槃》
java·jvm·git·后端·java-ee·区块链·量子计算
篝火悟者20 分钟前
自学-C语言-基础-数组、函数、指针、结构体和共同体、文件
c语言·开发语言
genispan40 分钟前
QT/C++ 多线程并发下载实践
开发语言·c++·qt
松韬1 小时前
Spring + Redisson:从 0 到 1 搭建高可用分布式缓存系统
java·redis·分布式·spring·缓存
-代号95271 小时前
【JavaScript】十三、事件监听与事件类型
开发语言·javascript·ecmascript
绝顶少年1 小时前
Spring Boot 注解:深度解析与应用场景
java·spring boot·后端