nodeJS:无法获得加密模块给我正确的AES加密结果 [英] nodeJS: can't get crypto module to give me the right AES cipher outcome

查看:362
本文介绍了nodeJS:无法获得加密模块给我正确的AES加密结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用nodeJS加密模块使用AES 128的ECB模式加密一些十六进制字符串。



为此,我使用以下代码:

  cryptoAES = function(sInput,sKey,bEncrypt){
return crypto('AES- 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'
pre>

但我得到:

  result ='ea1f940da8e269b9e075c936bff6a1f7 '

任何想法我可能做错了什么?

解决方案

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

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

生成所需结果。


I'm trying to use the nodeJS crypto module to encrypt some hex strings using the ECB mode of AES 128.

To do so, I'm using the following code:

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

When calling cryptoAES with:

sKey = '12345678900987654321123456789001'

sInput = '060123456789ABCDEF00000000000000'

I should get

result = 'FBECD5D02C5B7CD1055AAF86238D1E2F'

but I'm getting:

result = 'ea1f940da8e269b9e075c936bff6a1f7'

Any idea what I could be doing wrong?

解决方案

Reading https://github.com/joyent/node/issues/1318#issuecomment-1562766, you do need crypto.createCipheriv():

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

That generates the required result.

这篇关于nodeJS:无法获得加密模块给我正确的AES加密结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆