This question aids as part two to my previous question.
I have come across other threads with similar questions but due to recent changes in PHP (ie. mcrypt removal), I am seeking some advice as to how I'd best go about this using OpenSSL in 2017/18.
I have devised the following function in a PHP script. It takes a plain text string and encrypts it.
<?php
function encrypt( $myString) {
$data = $myString;
$key = 'B374A26A71490437AA024E4FADD5B497FDFF1A8EA6FF12F6FB65AF2720B59CCF';
$iv = '61736466673534336173646667353433';
$encrypted = openssl_encrypt($data, 'AES-256-CBC', $key, 0, hex2bin($iv));
return $encrypted;
}
?>
Returns: 6Q7DM7VGEeJdnGf2h9k1Kg==
My question is quite simple: What is the Terminal decryption equivalent turning the above result back into its plain text?
So far I was able to use the following Terminal commands in AppleScript (for faster variable manipulation) but the do shell script
content is Terminal code:
set encKey to "B374A26A71490437AA024E4FADD5B497FDFF1A8EA6FF12F6FB65AF2720B59CCF"
set encIV to "61736466673534336173646667353433"
set stringToEnc to "my plain text string"
set encrypted to (do shell script "echo '" & stringToEnc & "' | openssl enc -aes-256-cbc -a -K " & encKey & " -iv " & encIV)
set decrypted to (do shell script "echo '" & encrypted & "' | openssl enc -aes-256-cbc -a -d -K " & encKey & " -iv " & encIV)
However, while this works as is, setting encrypted
(in the script above) to the output of the PHP function, it throws the error:
bad decrypt 140735624655752:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22/libressl/crypto/evp/evp_enc.c:529:
Having studied this, that and similar questions here on SO, I am puzzled why the error keeps occurring.
I believe it is because of the encoding of the key
and iv
(note that I have used hex2bin()
in PHP), which will yield a different result if not used.
What's missing in my Terminal command? If someone could help me work out the decryption equivalent, I would really appreciate the assistance.