I'm encrypting a JSON object using OpenSSL on the command line. However, when I send it using cURL and then try to decrypt it, I just get unprintable bytes. Here is my process:
Encrypting in OpenSSL
openssl aes-128-cbc -in object.json -out cipher.txt -pass pass:key.file -base64
cat object.json
{"payload":"this is a test"}cat key.file
KWA6HNqb9UydXPbh72Vej82rT7NMVQZEcat cipher.txt U2FsdGVkX1/OOX/XSRXXOCmxfak5TXbagG6ZSW6U95U+VLADuaH83zmP8hee017J
key.file is exactly 32 bytes. I have the same key.file residing on the server. I have verified that it is also 32 bytes.
Sending using cURL
curl --data "@cipher.txt" http://www.example.com/myscript.php
php script
$key = file_get_contents("key.file");
echo $key . "\n";
// get the ciphertext from the POST parameter
$ciphertext = rawurlencode(file_get_contents("php://input"));
echo $ciphertext . "\n";
echo base64_decode(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext, MCRYPT_MODE_CBC));
And here is what I get back:
KWA6HNqb9UydXPbh72Vej82rT7NMVQZE U2FsdGVkX1%2FOOX%2FXSRXXOCmxfak5TXbagG6ZSW6U95U%2BVLADuaH83zmP8hee017J
���G�/
I don't see what I am doing wrong. Can anyone please show me where I am making my mistake? Thanks!
EDIT I also tried to use this command to decrypt, but I just get an empty string as a result:
echo base64_decode(openssl_decrypt($ciphertext, "AES-128-CBC", $key, OPENSSL_RAW_DATA));