简体   繁体   English

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

[英]how to decrypt in nodejs which is encrypted using JAVA

im trying to convert java lambda into javascript lamda. 即时通讯试图将Java lambda转换为javascript lamda。 want to convert these encrypt and decrypt method which is written in java to node js or javascript. 想要将这些用Java编写的加密和解密方法转换为节点js或javascript。 I have tried to implement using crpto in node 我试图在节点中使用crpto实现

keys are like this 钥匙是这样的

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

encrypt method 加密方式

 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;
    }

decrypt method 解密方法

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;
}

I have used this is the solution i made but seems it not working cause i encrypted this using a java code and using node's javascript code to decrypt it. 我曾经使用过这是我做的解决方案,但似乎无法正常工作,因为我使用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();
}

find the alogorithm i use this and adding padding if the key is not long enough but this give error that saying length is not enough. 找到我要使用的算法,如果密钥不够长,请添加填充,但这会提示错误,即长度不够。

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);
}

after struggling for a while i have implemented encrypted method and decrypt method which will be identical to java. 经过一段时间的努力,我已经实现了与Java相同的加密方法和解密方法。

decrypt method 解密方法

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

}

encrypt method 加密方式

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();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM