如何在使用JAVA加密的nodejs中解密

时间:2019-01-09 02:55:30

标签: javascript java node.js cryptojs

im试图将java lambda转换为javascript lamda。想要将这些用Java编写的加密和解密方法转换为节点js或javascript。 我试图在节点

中使用crpto来实现

键就是这样

private static String CIPHER_NAME = "AES/CBC/PKCS5PADDING";
private static int CIPHER_KEY_LEN = 16; //128 bits

加密方法

 private String encrypt(String key, String iv, String data) {
        try {
            if (key.length() <CIPHER_KEY_LEN) {
                int numPad = CIPHER_KEY_LEN - key.length();

                for(int i = 0; i < numPad; i++){
                    key += "0"; //0 pad to len 16 bytes
                }

            } else if (key.length() >CIPHER_KEY_LEN) {
                key = key.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes
            }


            IvParameterSpec initVector = new IvParameterSpec(iv.getBytes("ISO-8859-1"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("ISO-8859-1"), "AES");

            Cipher cipher = Cipher.getInstance(CIPHER_NAME);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initVector);

            byte[] encryptedData = cipher.doFinal((data.getBytes()));

            String base64_EncryptedData = Base64.getEncoder().encodeToString(encryptedData);
            String base64_IV = Base64.getEncoder().encodeToString(iv.getBytes("ISO-8859-1"));

            return base64_EncryptedData + ":" + base64_IV;

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

解密方法

private String decrypt(String key, String data) {
    try {
        if (key.length() < CIPHER_KEY_LEN) {
            int numPad = CIPHER_KEY_LEN - key.length();

            for(int i = 0; i < numPad; i++){
                key += "0"; //0 pad to len 16 bytes
            }

        } else if (key.length() > CIPHER_KEY_LEN) {
            key = key.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes
        }

        String[] parts = data.split(":");

        IvParameterSpec iv = new IvParameterSpec(Base64.getDecoder().decode(parts[1]));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("ISO-8859-1"), "AES");

        Cipher cipher = Cipher.getInstance(CIPHER_NAME);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

        byte[] decodedEncryptedData = Base64.getDecoder().decode(parts[0]);

        byte[] original = cipher.doFinal(decodedEncryptedData);

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

我用过 这是我提出的解决方案,但似乎无法解决问题,因为我使用Java代码并使用节点的javascript代码对其进行了加密。

function decrypt (messagebase64, keyBase64, ivBase64) {

    var key = Buffer.from(keyBase64, 'base64');
    var iv = Buffer.from(ivBase64, 'base64');

    var decipher = crypto.createDecipheriv(getAlgorithm(keyBase64), key, iv);
    decipher.setAutoPadding(false);
    decipher.update(messagebase64, 'base64');
    return decipher.final();
}

找到我要使用的算法,如果密钥不够长,请添加填充,但这会提示错误,即长度不够。

function getAlgorithm(keyBase64) {

    if(keyBase64.length<CIPHER_KEY_LEN){
        var padding = CIPHER_KEY_LEN-keyBase64.length;
        for(var i=0;i<padding;i++){
            keyBase64+="0";
        }
    }else if(keyBase64.length>CIPHER_KEY_LEN){
        keyBase64 =keyBase64.substring(0, CIPHER_KEY_LEN)
    }

    var key = Buffer.from(keyBase64, 'base64');

    switch (key.length) {
        case 16:
            return 'aes-128-cbc';
        case 32:
            return 'aes-256-cbc';

    }

    throw new Error('Invalid key length: ' + key.length);
}

1 个答案:

答案 0 :(得分:0)

经过一段时间的努力,我实现了与Java相同的加密方法和解密方法。

解密方法

function decrypt (messagebase64, keyBase64, ivBase64) {

    if(keyBase64.length<CIPHER_KEY_LEN){
        var padding = CIPHER_KEY_LEN-keyBase64.length;
        for(var i=0;i<padding;i++){
            keyBase64+="0";
        }
    }else if(keyBase64.length>CIPHER_KEY_LEN){
        keyBase64 =keyBase64.substring(0, CIPHER_KEY_LEN)
    }


    var key = Buffer.from(keyBase64, 'latin1');
    var iv = Buffer.from(ivBase64, 'base64');

    var encryptdata = new Buffer(messagebase64, 'base64');

    var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv),
        decoded = decipher.update(encryptdata, 'base64', 'utf8');

    decoded += decipher.final('utf8');


    return decoded

}

加密方法

function encrypt(plainText, keyBase64, ivBase64) {

    if(keyBase64.length<CIPHER_KEY_LEN){
        var padding = CIPHER_KEY_LEN-keyBase64.length;
        for(var i=0;i<padding;i++){
            keyBase64+="0";
        }
    }else if(keyBase64.length>CIPHER_KEY_LEN){
        keyBase64 =keyBase64.substring(0, CIPHER_KEY_LEN)
    }

    var key = Buffer.from(keyBase64, 'latin1');
    var iv = Buffer.from(ivBase64,'latin1');
    var encoded_base64_iv= iv.toString('base64');

    var cipher2 = crypto.createCipheriv('aes-128-cbc', key, iv);
    cipher2.write(plainText);
    cipher2.end();

    var cipher_text = cipher2.read();
    var encodedString = cipher_text.toString('base64');

    var final_encrypted_data = encodedString+":"+encoded_base64_iv;

    return final_encrypted_data.toString();
};