Given a PHP script that encrypts a string with openssl_encrypt, I want to decrypt the output with openssl CLI and C#.
Although I can encrypt and decrypt the string in each programming language on its own with key and IV, I cannot decrypt the PHP output with openssl CLI and C#.
This is the PHP script:
<?php
function bad_crypt($string, $action = 'e') {
if (empty($string)) {
return "";
}
$encrypt_method = "AES-256-CBC";
$key = "3d13b07f244861f2d323d1734d773ce4bd079e2c17deb5695911e1387e0b579a";
$iv = "d9ec53dadaf1e968"; // bad practice, but this is just a test
echo "key:";
echo "<br>";
echo $key;
echo "<br>";
echo "iv:";
echo "<br>";
echo $iv;
echo "<br>";
if ($action == 'e') {
$output = base64_encode(openssl_encrypt($string, $encrypt_method, $key, 0, $iv));
} else if ($action == 'd') {
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
$info = "mysecret";
$encInfo = bad_crypt($info);
echo "encrypted:";
echo "<br>";
echo $encInfo;
echo "<br>";
echo "<br>";
echo "<br>";
$t = bad_crypt($encInfo, "d");
echo "decrypted:";
echo "<br>";
echo $t;
?>
which gives the following output:
key:
3d13b07f244861f2d323d1734d773ce4bd079e2c17deb5695911e1387e0b579a
iv:
d9ec53dadaf1e968
encrypted:
QVBBaDcrdkRUdVFqZDV5cGFMNHJCdz09
key:
3d13b07f244861f2d323d1734d773ce4bd079e2c17deb5695911e1387e0b579a
iv:
d9ec53dadaf1e968
decrypted:
mysecret
I have tried numerous combinations of options for openssl; none did reproduce the PHP encryption result or decrypted it.
Example openssl encryption command:
openssl.exe enc -e -aes-256-cbc -a -K 3d13b07f244861f2d323d1734d773ce4bd079e2c17deb5695911e1387e0b579a -iv d9ec53dadaf1e968 -in mysecret.txt
with mysecret.txt containing mysecret results in plQ2hEAoywxUeFWniDszdQ==.
Any help is appreciated.
Looking forward to a working openssl cli command and a C# program.