繁体   English   中英

PHP 加密和 Coldfusion 解密

[英]Encryption with PHP and Decryption with Coldfusion

我们目前正在使用 PHP 加密来自我们网站的值,然后再将它们写入 MySQL 表。 我们可以使用 PHP 成功解密这些值,但是,我们已经开发出一种也能够使用 Coldfusion 解密数据的需求。 不幸的是,我们无法让它发挥作用。 我正在寻找一种解决方案,可以做这两件事之一:

  1. 成功使用我们现有的用 PHP 编写的加密代码/数据,并能够使用 Coldfusion 成功解密(首选)。
  2. 对我们现有的用 PHP 编写的加密代码的修改,可以在 Coldfusion 中成功解密。

这是我们当前的 PHP 加密代码:

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;
  }

这是我们当前的 PHP 解密代码:

                // 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;
  }

我使用以下代码在 Coldfusion 中尝试了解密测试:

<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>

但是,这失败了,并出现错误:

"Error","ajp-nio-8018-exec-7","09/25/20","11:26:48","","尝试加密或解密输入字符串时出错:' ' 无法解码字符串 ""7w!z%C&F)J@NcRfUjXn2r5u8x/A?D(G-KaPdSgVkYp3s6v9y$B&E)H@MbQeThWmZ"".. 包含或处理的文件的具体顺序为:C:\\inetpub\\wwwroot \\scheduled\\Decrypt_Test.cfm,第 3 行

注意正确的解密值为: Arlene

有任何想法吗? 任何帮助,将不胜感激。 谢谢你。

PHP 代码使用 AES-128,即一个 16 字节的大密钥。 但是应用的密钥 ( 7w!z%C&F)J@NcRfUjXn2r5u8x/A?D(G-KaPdSgVkYp3s6v9y$B&E)H@MbQeThWmZ ) 大得多。 PHP 根据指定的 AES 变体截断过长的密钥,即对于 AES-128,仅考虑前 16 个字节,其余的将被忽略。 在 ColdFusion 脚本中,必须使用相同的 16 字节密钥。

另外,PHP代码IV中,HMac和密文在Base64编码前加密时连接,Base64解码后解密时再次分离。
解密需要IV和密文。 HMac 通常用于身份验证,但这里没有实现(为此,必须针对接收到的密文确定 HMac,并与接收到的 HMac 进行比较)。
在 ColdFusion 脚本中,没有发生分离,而是直接解密连接的数据,这失败了。 即必须实现缺失分离,然后才能使用IV和密文进行解密。

以下 ColdFusion 脚本说明了必须实现的逻辑。 该脚本可以在这里执行:

<!--- Hex encode encrypted data --->
<cfset EncryptedData=binaryDecode("ltlXr0hdv8kE9v+VVJ8GmLYVfgPSZPr9QCL89QoMupFJrXNdYmFi7sp0wbx8CbzJycmzblLDB/zF5pXrmRasMoW8PGV7tfjnEsxv8LUt7xj/56tAMHRJxIRk01TdpNvJ","base64")>
<cfset EncryptedDataHex=binaryEncode(EncryptedData,"hex")>

<!--- Separate IV (first 32 chars / 16 bytes), HMac(SHA512) (next 128 chars / 64 bytes), ciphertext --->
<cfset IVHex=Mid(EncryptedDataHex,1,32)>
<cfoutput>#IVHex#</cfoutput><br>
<cfset HMacHex=Mid(EncryptedDataHex,1+32,128)> <!--- Will not be used any further, see PHP code --->
<cfoutput>#HMacHex#</cfoutput><br>
<cfset CiphertextHex=Mid(EncryptedDataHex,1+32+128,len(EncryptedDataHex)-160)>
<cfoutput>#CiphertextHex#</cfoutput><br>

<!--- Truncate key to 16 bytes --->
<cfset Key=Mid("7w!z%C&F)J@NcRfUjXn2r5u8x/A?D(G-KaPdSgVkYp3s6v9y$B&E)H@MbQeThWmZ",1,16)>

<!--- Decrypt --->
<cfset KeyB64=toBase64(Key)>
<cfset IV=binaryDecode(IVHex,"hex")>
<cfset Plaintext=Decrypt(CiphertextHex,KeyB64,"AES/CBC/PKCS5Padding","hex",IV)>
<cfoutput>#Plaintext#</cfoutput>

随着输出:

96D957AF485DBFC904F6FF95549F0698 
B6157E03D264FAFD4022FCF50A0CBA9149AD735D626162EECA74C1BC7C09BCC9C9C9B36E52C307FCC5E695EB9916AC3285BC3C657BB5F8E712CC6FF0B52DEF18 
FFE7AB40307449C48464D354DDA4DBC9 
Arlene

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM