Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decrypt cipher text using NodeJs createDecipheriv. What am I doing wrong?

Tags:

node.js

I have the following code that should decrypt an encrypted text:

var crypto = require('crypto');
var Buffer = require('buffer').Buffer;

var iv = new Buffer('the-iv', 'binary'); //length=16
var key = new Buffer('the-secret-key', 'binary');//length=30

var encryptedText = new Buffer('base64-encoded-encrypted-data', 'base64');

var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv); //using the aes-128-cbc algorithm
decrypted = decipher.update(encryptedText, "binary", 'utf8');
decrypted += decipher.final('utf8');

When I execute the script node test-main.js, I get the following error:

node-crypto : Invalid key length 30

crypto.js:355
  this._binding.initiv(cipher, toBuf(key), toBuf(iv));
                ^
Error: DecipherInitIv error
    at new Decipheriv (crypto.js:355:17)
    at Object.Decipheriv (crypto.js:352:12)
    at Object.<anonymous> (path/to/file/test-main.js:9:19)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

My NodeJs version is 0.10.15

I am not sure what I am doing wrong/missing.

like image 386
Uyi Ehondor Avatar asked Jul 31 '13 15:07

Uyi Ehondor


1 Answers

Please try using a key length of 16. aes-128-cbc uses a 128 bit key.

like image 169
Chandu Avatar answered Oct 11 '22 13:10

Chandu