I'm trying to port this working php code to Node.js but I get Error: Invalid IV length 32
Here is the PHP code:
//--- PHP example code (works): ---
$aes_iv = 'MjY2YjljMmM0MjVjNzVlMGMyZGI2NjAwN2U5ZGMzZDQ%3D';
$payload = base64_decode($payload);
$aes_iv = base64_decode($aes_iv);
// secret key. 64 character hex string:
$shared_key = '14370ced836 ...';
// convert from hex to binary string:
$shared_key = pack('H*', $shared_key);
// AES decrypt payload
$payload = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $shared_key, $payload, MCRYPT_MODE_CBC, $aes_iv);
// AES adds null characters to the end of short strings,
// so we should strip them out
$payload = rtrim($tp_payload, "\0");
Here is the Node.js code that doesn't work. See "Error: Invalid IV length 32"
//--- Node.js equivalent ??? ---
var aes_iv = 'MjY2YjljMmM0MjVjNzVlMGMyZGI2NjAwN2U5ZGMzZDQ%3D';
var payload_s = new Buffer(payload, 'base64').toString();
var aes_iv_s = new Buffer(aes_iv, 'base64').toString();
// secret key. 64 character hex string:
var shared_key = '14370ced836 ...';
// convert from hex to binary string:
var shared_key_b = new Buffer(shared_key, 'hex').toString('binary');
// Error: Invalid IV length 32
var decipher = crypto.createDecipheriv('aes-256-cbc', shared_key_b, aes_iv_s);
var decoded = decipher.update(payload_s);
decoded += decipher.final();
console.log(decoded);