【问题标题】:NodeJs Decrypt AES256 Encryption From JAVANodeJs 从 JAVA 解密 AES256 加密
【发布时间】:2019-10-13 18:42:25
【问题描述】:

我正在与另一个系统集成,给定的数据在 AES-256-CBC(Java) 中加密,需要在 NodeJs 中解密才能继续。

我从互联网上尝试了很多方法并陷入了错误。下面是 Java(解密)和 NodeJs(我的解密代码)的示例代码

private static final int ITERATION_COUNT = 65536;
private static final int KEY_LENGTH = 256;
private static final byte[] DEFAULT_IV = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

public static byte[] decryptToBytes(String src, String secret, String salt, byte[] iv) {
        try{
            IvParameterSpec ivspec = new IvParameterSpec(iv);

            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
            KeySpec spec = new PBEKeySpec(secret.toCharArray(), salt.getBytes(), ITERATION_COUNT, KEY_LENGTH);
            SecretKey tmp = factory.generateSecret(spec);
            SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
            return cipher.doFinal(Base64.getDecoder().decode(src));
        }catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

public static String decrypt(String src, String secret, String salt, byte[] iv) {
        try{
            return new String(decryptToBytes(src, secret, salt, iv));
        }catch (Exception ex) {
            return null;
        }
    }

public static void main(String[] args) {
        String secret  = "abcd123456";
        String salt = "123abc";
        String plainText ="This is AES256 encryption test";
        String cipherText = "gbYgtu5EWxOYRSUmMsEtdn8oQLxBjejfwUBSRhhls08=";

        byte[] IV = new byte[16];

        String originalText = decrypt(cipherText,secret, salt, IV);

    }
import crypto from "crypto";
public aesCdcDecrypt(input: string) {
        let iterationCount = 65536;
        let keyLength = 256;
        let iv = new Buffer(16);
        let keyHex = "abcd123456";
        let salt = "123abc";

        let decryptText: string;
        try {
            crypto.pbkdf2(new Buffer(keyHex), new Buffer(salt), iterationCount, keyLength, "sha256", function (err, key) {
                let secretKey = key.toString("hex");
                let decipher = crypto.createDecipheriv("aes-256-cbc", secretKey, iv);
                decryptText = decipher.update(input, "binary", "utf8");
                decryptText += decipher.final("utf8");

                console.log('Result: ' + decryptText);
            });
        } catch (e) {
            console.log(e);
        }
        return decryptText;
    }

得到这个错误的结果 --> 错误:无效的密钥长度 在新的 Decipheriv (crypto.js:267:16) 在 Object.createDecipheriv (crypto.js:627:10)

【问题讨论】:

    标签: java node.js encryption aes


    【解决方案1】:

    您的 TS 代码中存在一些小问题:

    • key 长度以字节为单位,而不是位
    • new Buffer() 默认不解码base64

    这是一个工作版本(JS):

    const crypto = require('crypto')
    function aesCdcDecrypt(ciphertext) {
        let iterationCount = 65536;
        let keyLength = 32;
        let iv = Buffer.alloc(16);
        let keyHex = "abcd123456";
        let salt = "123abc";
    
        let key = crypto.pbkdf2Sync(keyHex, Buffer.from(salt), iterationCount, keyLength, "sha256");
        var cipher = crypto.createDecipheriv("aes-256-cbc", key, iv);
        cipher.setAutoPadding(true);
        let ciph = cipher.update(Buffer.from(ciphertext, "base64"));
        let ciphf = cipher.final();
        return Buffer.concat([ciph, ciphf]).toString();
    }
    console.log(aesCdcDecrypt("gbYgtu5EWxOYRSUmMsEtdn8oQLxBjejfwUBSRhhls08="));
    

    打印:

    This is AES256 encryption test
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多