【问题标题】:nodeJS: can't get crypto module to give me the right AES cipher outcomenodeJS:无法获得加密模块来给我正确的 AES 密码结果
【发布时间】:2013-02-21 14:37:59
【问题描述】:

我正在尝试使用 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'

知道我做错了什么吗?

【问题讨论】:

  • 你能从方程中去掉 IV 吗?无论如何,欧洲央行不使用 IV。
  • 确实,已删除 iv。但结果并没有改变,因为没有使用 IV。
  • 对于其他读者,我验证了预期的结果...
  • 感谢您的检查。这已经是一个有趣的确认了。

标签: javascript node.js cryptography aes


【解决方案1】:

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

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

生成所需的结果。

【讨论】:

  • 就是这样。问题解决了。这太不合逻辑了,文档应该提到它。
  • 貌似有两个参数的ceateCipher用的是算法和密码,而不是算法和key
  • 确实,而且,即使文档没有错,我也没有立即明白。
  • 此解决方案已弃用。如果你给空IV,它会说'invalid iv length'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-07
  • 2015-05-22
  • 2019-12-20
  • 2021-05-16
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多