nodeJS: can't get crypto module to give me the right AES cipher outcome

痴心易碎 提交于 2019-12-11 01:21:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15004951/nodejs-cant-get-crypto-module-to-give-me-the-right-aes-cipher-outcome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!