We are currently encrypting values from our website with PHP before writing them to a MySQL table. We can successfully decrypt the values with PHP, however, we've developed a need to also be able to decrypt the data with Coldfusion. Unfortunately we haven't been able to get that to work. I'm looking for a solution that will do either of these two things:
- Successfully uses our existing encryption code/data written in PHP and being able to decrypt it successfully with Coldfusion (preferred).
- A modification to our existing encryption code written in PHP that can be successfully decrypted in Coldfusion.
Here is our current PHP encryption code:
private $cipher="AES-128-CBC";
// Encrypt a value
private function encrypt($value,$key){
$ivlen = openssl_cipher_iv_length($this->cipher);
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($value, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha512', $ciphertext_raw, $key, true);
$encryptedValue = base64_encode( $iv.$hmac.$ciphertext_raw );
return $encryptedValue;
}
Here is our current PHP decryption code:
// Decrypt a value
public function decrypt($encryptedValue,$key){
$val = base64_decode($encryptedValue);
$ivlen = openssl_cipher_iv_length($this->cipher);
$iv = substr($val, 0, $ivlen);
$hmac = substr($val, $ivlen, $this->sha2len);
$ciphertext_raw = substr($val, $ivlen+$this->sha2len);
$decryptedValue = openssl_decrypt($ciphertext_raw, $this->cipher, $key, OPENSSL_RAW_DATA, $iv);
return $decryptedValue;
}
I tried a decrypt test in Coldfusion with the following code:
<cfset TestString="ltlXr0hdv8kE9v+VVJ8GmLYVfgPSZPr9QCL89QoMupFJrXNdYmFi7sp0wbx8CbzJycmzblLDB/zF5pXrmRasMoW8PGV7tfjnEsxv8LUt7xj/56tAMHRJxIRk01TdpNvJ">
<cfset KeyString="7w!z%C&F)J@NcRfUjXn2r5u8x/A?D(G-KaPdSgVkYp3s6v9y$B&E)H@MbQeThWmZ">
<cfset NewString='#Decrypt(TestString, KeyString, "AES", "Base64")#'>
<cfoutput>#NewString#</cfoutput>
That fails, however, with the error:
"Error","ajp-nio-8018-exec-7","09/25/20","11:26:48","","An error occurred while trying to encrypt or decrypt your input string: '' Can not decode string ""7w!z%C&F)J@NcRfUjXn2r5u8x/A?D(G-KaPdSgVkYp3s6v9y$B&E)H@MbQeThWmZ"".. The specific sequence of files included or processed is: C:\inetpub\wwwroot\scheduled\Decrypt_Test.cfm, line: 3 "
Note that the correct decrypted value is: Arlene
Any ideas? Any help would be appreciated. Thank you.
openssl_cipher_iv_length("AES-128-CBC");? - rrk<?php echo(openssl_cipher_iv_length("AES-128-CBC")); ?>may be try this? - rrksha2len. - rrk