I have created an encryption function in PHP using openssl_encrypt()
with aes-256-cbc
as algorithm. I want the encrypted data to be decrypted in NodeJS when sent in an API
call.
This is the encryption in my PHP:
$form_data_str = json_encode($form_data);
// Encrypt data to submit
define('AES_256_CBC', 'aes-256-cbc');
// Encryption Key from Merchant sKey
$sKey = 'uTEW2U0s90mtzF5nGX2BBKkuYcUsQEEK';
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(AES_256_CBC));
$encrypted_form_data = openssl_encrypt($form_data_str, AES_256_CBC, $encryption_key, 0, $iv);
then I pass the encrypted data as body in an API call to my NodeJS server. Now, I want to decrypt the data in my NodeJS. I have this current code in my NodeJS:
var encrypter = require('crypto'),
algorithm = 'aes-256-cbc',
password = 'uTEW2U0s90mtzF5nGX2BBKkuYcUsQEEK';
function decrypt(data){
let iv = new Buffer.alloc(16);
var decipher = encrypter.createDecipheriv(algorithm,password,iv)
var decrypted = decipher.update(data,'hex','utf8')
decrypted += decipher.final('utf8');
return decrypted;
}
var decrypted = decrypt('encrypted data from request body');
console.log(decrypted)
In console
I have this error:
crypto.js:183
var ret = this._handle.final();
^
Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length at Decipheriv.final (crypto.js:183:26)
What should be the correct approach in my decrypt
function in NodeJS?