nodeJS:无法获取加密模块给我正确的AES密码结果

时间:2013-02-21 14:37:59

标签: javascript node.js cryptography aes

我正在尝试使用nodeJS加密模块使用AES 128的ECB模式加密某些十六进制字符串。

为此,我使用以下代码:

cryptoAES = function (sInput, sKey, bEncrypt) {
    return crypto('AES-128-ECB', sInput, sKey, bEncrypt);
};

crypto = function (sAlgo, sInput, sKey, bEncrypt) {
    var result = "";
    if (bEncrypt){
        var cipher;
        var bKey = new Buffer(sKey, 'hex');
        var bInput = new Buffer(sInput, 'hex');

        cipher = crypto.createCipher(sAlgo, bKey);

        cipher.setAutoPadding(false);
        result = cipher.update(bInput, null, 'hex');
        result += cipher.final('hex');
    }
    return result;
};

使用以下方法调用cryptoAES时

sKey = '12345678900987654321123456789001'

sInput = '060123456789ABCDEF00000000000000'

我应该

result = 'FBECD5D02C5B7CD1055AAF86238D1E2F'

但我得到了:

result = 'ea1f940da8e269b9e075c936bff6a1f7'

知道我可能做错了吗?

1 个答案:

答案 0 :(得分:5)

阅读https://github.com/joyent/node/issues/1318#issuecomment-1562766需要crypto.createCipheriv()

cipher = crypto.createCipheriv(sAlgo, bKey, '');

这会产生所需的结果。