I have a PHP script used to encrypt data with openssl_encrypt() and the aes-256-cbc algo. Encryption/Decryption works well using PHP, it's an old script. I've been trying to decrypt the datas using NodeJS and the crypto module.
Information used for debug purpose:
base64 string stored in DB , that contains : encrypted_data_b64 + iv (format : encrypted_data_b64::iv)
This is generated by the php script .
T3FWbE1GRU5uUlFPNFpJOEpwR25zU1pTeWlHdU55S2FYMnFhc3c3MHVMZz06OsDI5XgOUrIoo+IAu8sYoXw=
Here is the base64 format key:
alFlTFNKNGtLYXhtNkpOa2tHUEs=
Here is the base64 format IV:
wMjleA5Ssiij4gC7yxihfA==
Here is the encrypted data , base64 format:
OqVlMFENnRQO4ZI8JpGnsSZSyiGuNyKaX2qasw70uLg=
Are you able to make a NODEJS example file that will decode the data from the string stored in BD ?
As an example, here is the PHP script used. I have been trying a lot of option using node but always get errors or not the correct description.
<?php
function Encrypt($data)
{
// Remove the base64 encoding from our key
$encryption_key = base64_decode("alFlTFNKNGtLYXhtNkpOa2tHUEs=");
// Generate an initialization vector
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
// Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
$encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
// The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
return base64_encode($encrypted . '::' . $iv);
};
function Decrypt($data)
{
// Remove the base64 encoding from our key
$encryption_key = base64_decode("alFlTFNKNGtLYXhtNkpOa2tHUEs");
// plain $encryption_key = "jQeLSJ4kKaxm6JNkkGPK";
// To decrypt, split the encrypted data from our IV - our unique separator used was "::"
list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
}
//echo "Here is the encrypted password:" . Encrypt("mysecurepassword");
//echo "<br><br>";
//echo "here is the decrypted password:" . Decrypt(Encrypt("mysecurepassword"));
$data = "T3FWbE1GRU5uUlFPNFpJOEpwR25zU1pTeWlHdU55S2FYMnFhc3c3MHVMZz06OsDI5XgOUrIoo+IAu8sYoXw=";
// Remove the base64 encoding from our key
$encryption_key = base64_decode("alFlTFNKNGtLYXhtNkpOa2tHUEs");
// plain $encryption_key = "jQeLSJ4kKaxm6JNkkGPK";
// To decrypt, split the encrypted data from our IV - our unique separator used was "::"
list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
echo "This is the IV : " .base64_encode($iv);
echo '<br>';
echo openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
echo '<br>';
echo openssl_cipher_iv_length('aes-256-cbc');
echo '<br>';
echo $encrypted_data;
?>
Thanks for your help and your time :) I hope it will help other people too.