2

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?

Axel Cateland
  • 125
  • 1
  • 8

1 Answers1

5

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.

robertklep
  • 198,204
  • 35
  • 394
  • 381