I need to be able to create an encryption in NodeJS that is the same with PHP openssl_encrypt. For urgent reasons as a workaround, we created a PHP API and our Node server uses that just to do the encryption. We cannot alter the encryption in PHP as it is a legacy system.
The key used is a 15 char string. The iv used is a 16 char string.
PHP Encrypt API
$string = $_GET["string"];
$key = $_GET["key"];
$iv = $_GET["iv"];
$encrypt_method = 'AES-256-CBC';
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
echo $output;
PHP Decrypt API
$string = $_GET["string"];
$key = $_GET["key"];
$iv = $_GET["iv"];
$encrypt_method = 'AES-256-CBC';
$string = base64_decode($string);
$output = openssl_decrypt($string, $encrypt_method, $key, 0, $iv);
echo $output;
NodeJS Crypto Encrypt
static aesEncrypt = ({ toEncrypt }) => {
const cipher = crypto.createCipheriv('aes-256-cbc', aesKey, aesIv);
let encrypted = cipher.update(toEncrypt, 'utf8', 'base64');
return encrypted + cipher.final('base64');
};
NodeJS Crypto Decrypt
static aesDecrypt = ({ toDecrypt }) => {
const decipher = crypto.createDecipheriv('aes-256-cbc', aesKey, aesIv);
let decrypted = decipher.update(toDecrypt, 'base64', 'utf8');
return decrypted + decipher.final('utf8');
};
I've used crypto and crypto-js but I can't replicate the results. I've found examples in stackoverflow but their usage of php encryption is different and thus gave different results.