简体   繁体   English

如何使用Node.Js解密从Android(Java)发送的数据

[英]How to decrypt data sent from Android (Java) using Node.Js

I have run into a problem - an existing program uses the code below to encrypt data 我遇到了问题-现有程序使用下面的代码来加密数据

public static String encryptData(String data, final String key) {
    try {
        byte[] kb=key.getBytes("utf-8");
        byte[] dig= MessageDigest.getInstance("SHA-1").digest(kb);
        byte[] kdig= Arrays.copyOf(dig, 24);
        final SecretKeySpec secretKeySpec = new SecretKeySpec(kdig, "DESede");

        final Cipher instance = Cipher.getInstance("DESede");
        instance.init(ENCRYPT_MODE, secretKeySpec);

        byte[] ecb=instance.doFinal(data.getBytes("utf-8"));
        byte[] enb64=Base64.encode(ecb, Base64.DEFAULT);
        return new String(enb64);
    } catch (Exception ex) {
        ErmsLogger.e(TAG, ex.getMessage());
        return null;
    }
}

I need to write code on Node.Js that decrypts this encrypted data. 我需要在Node.Js上编写解密此加密数据的代码。 So far - I have come up with 到目前为止-我想出了

function decryptData(encrpted_data,fn){
    var hashedKey = crypto.createHash('sha1').update(config.dataPassword).digest('hex');

    if(hashedKey.length < 48){
        var num=48 - hashedKey.length;
        for(var i=0;i < num; i++){
            hashedKey +='0';
        }
    }

    var key=Buffer.from(hashedKey, 'hex');

    var decipher   = crypto.createDecipher('des-ede', key);
    decoded        = decipher.update(encrpted_data, 'base64', 'utf8');
    decoded       += decipher.final('utf8');
    log.debug(JSON.stringify(decoded));

    return fn(decoded);
}

I keep on running into 我继续遇到

Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
    at Decipher.final (internal/crypto/cipher.js:104:26)

whenever I try to decrypt data sent from the android app. 每当我尝试解密从android应用发送的数据时。 the working decryption code on Android (Java) is Android(Java)上的有效解密代码为

public static String decryptData(final String data, final String password) throws Exception {
        MessageDigest instance=MessageDigest.getInstance("SHA-1");
        byte[] passworddigest=instance.digest(password.getBytes("utf-8"));
        byte[] key=Arrays.copyOf(passworddigest, 24);
        final SecretKeySpec secretKeySpec = new SecretKeySpec(key, "DESede");

        final byte[] decodeddata = Base64.decode(data.getBytes("utf-8"), Base64.DEFAULT);

        final Cipher ciperInstance = Cipher.getInstance("DESede");
        ciperInstance.init(DECRYPT_MODE, secretKeySpec);
        byte[] res= ciperInstance.doFinal(decodeddata);

        return new String(res, "UTF-8");
    }

Could you assist me translate this in Node? 您能帮我在Node中翻译吗?

After a bit of research and hitting my head against it - I finally came up with something that works 经过一番研究之后,我终于找到了可行的方法

function decryptData(encrpted_data,fn){

    var encodeKey   = crypto.createHash('sha1').update(config.dataPassword).digest('hex');
    var cryptkey=Buffer.alloc(24);
    encodeKey.copy(cryptkey);

    var decipher = crypto.createDecipheriv('des-ede3', cryptkey,'');
    decipher.setAutoPadding(true);
    decoded  = decipher.update(encrpted_data, 'base64', 'utf8');
    decoded += decipher.final('utf8');

    log.debug(JSON.stringify(decoded));

    return fn(decoded);
}

the explanation - crypto.createDecipher() - internally uses the MD5 hash for the key passed to it and so it really doesn't matter what key value you send in - it will be changed. 解释-crypto.createDecipher()-内部使用MD5哈希作为传递给它的密钥,因此您发送的密钥值实际上并不重要-它将被更改。 So the best option is to use createDecipheriv which accepts a raw key. 所以最好的选择是使用createDecipheriv,它接受一个原始密钥。 This allows you to hash your key outside before passing it in. Since I was not using any IV - I passed the value of ''. 这允许您在传入密钥之前将其散列到外部。由于我没有使用任何IV-我传递了''的值。 In Java (Android) DESede is really TripleDES and the equivalent on on crypto is 'des-ede3' and not 'des-ede'. 在Java(Android)中,DESede实际上是TripleDES,在加密货币上的等效项是“ des-ede3”而不是“ des-ede”。 I hope this saves someone a couple of hours. 我希望这可以节省一个人几个小时。

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

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