我正在尝试将 Java AES 加密解密转换为 NodeJs。到目前为止,这是我尝试过的

I am trying to convert Java AES encryption decryption to NodeJs. This is what I have tried so far

加密:

private static String secretKey = "boooooooooom!!!!";
private static String salt = "ssshhhhhhhhhhh!!!!";

public static String encrypt(String strToEncrypt, String secret) 
{
    try 
    {
        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);

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

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
    } 
    catch (Exception e) 
    {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

解密:

private static String secretKey = "boooooooooom!!!!";
private static String salt = "ssshhhhhhhhhhh!!!!";

public static String decrypt(String strToDecrypt, String secret) {
    try 
    {
        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
        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 new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    } 
    catch (Exception e) {
        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}

我对此进行了很多搜索。我在堆栈溢出时遇到了这些问题

Difference in key lengths between crypto.pbkdf2 (node.js) and PBEKeySpec, 我都试过了,但是 none 解决了我的问题。

到目前为止我尝试过的是,

function encrypt(plainText, secretKey,randomeString) {

    const key = crypto.pbkdf2Sync(secretKey, salt, 65536, 16, digest);
    const iv = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]);
    const cipher = crypto.createCipheriv(algorithm, key, iv);
    let encrypted = cipher.update(plainText, 'utf8', 'base64')
    encrypted += cipher.final('base64');      
    return encrypted;
};

function decrypt(strToDecrypt, secretKey, randomeString) {

    const key = crypto.pbkdf2Sync(secretKey, salt, 65536, 16, digest);

    const iv = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]);

    const decipher = crypto.createDecipheriv(algorithm, key, iv);
    let decrypted = decipher.update(strToDecrypt, 'base64');
    decrypted += decipher.final();
    return decrypted;
}

此 Node.js 代码应产生与您的 Java 代码相同的结果。我还将 secretKey 变量作为加密/解密函数的参数。

const crypto = require('crypto');

const algorithm = 'aes-256-cbc';
const salt = "ssshhhhhhhhhhh!!!!";
const digest = 'sha256';

function encrypt(plainText, secretKey) {

    const key = crypto.pbkdf2Sync(secretKey, salt, 65536, 32, digest);
    const iv = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]);

    const cipher = crypto.createCipheriv(algorithm, key, iv);
    let encrypted = cipher.update(plainText, 'utf8', 'base64')
    encrypted += cipher.final('base64');
    return encrypted;
};

function decrypt(strToDecrypt, secretKey) {

    const key = crypto.pbkdf2Sync(secretKey, salt, 65536, 32, digest);
    const iv = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]);

    const decipher = crypto.createDecipheriv(algorithm, key, iv);
    let decrypted = decipher.update(strToDecrypt, 'base64');
    decrypted += decipher.final();
    return decrypted;
}

const key = "boooooooooom!!!!";
const cipherText = encrypt("test", key);
console.log("Ciphertext:", cipherText);
const plainText = decrypt(cipherText, key);
console.log("Plaintext:", plainText);