NodeJS: it is not possible to get a crypto module to give me the correct AES encryption result

I am trying to use nodeJS encryption module to encrypt some hex strings using ECB AES 128 mode.

For this, I use 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 have to get

 result = 'FBECD5D02C5B7CD1055AAF86238D1E2F' 

but I get:

 result = 'ea1f940da8e269b9e075c936bff6a1f7' 

Any idea what I can do wrong?

+2
source share
1 answer

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

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

This generates the desired result.

+5
source

All Articles