javascript - Node JS : can't get crypto module to give me the right AES cipher outcome

标签 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'

知道我可能做错了什么吗?

最佳答案

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

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

这会生成所需的结果。

关于javascript - Node JS : can't get crypto module to give me the right AES cipher outcome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15004951/

相关文章:

javascript - 动态添加的子菜单不会切换 - Bootstrap

node.js - Mongoose 在特定记录后查找 n 条记录

node.js - 使用加密库在 NodeJS 中创建 PGP 兼容签名

javascript - 多个 YouTube 视频嵌入自动播放序列

javascript - 为什么节点会打印 Promise { <pending> }?我可以把它关掉吗?

node.js - 使用 redis 的 NodeJS - 安装 hiredis 还是不安装?

node.js - 无法让 mongojs(node-mongodb-native 包装器)在 Ubuntu 10.10 上工作

javascript - jsrsasign - 如何提取 x.509 证书的指纹?

java相当于php的hmac-SHA1

javascript - 如何在 HTML 输入文本中正确显示引号