Coldfusion 3DES加密使加密结果不同于PHP`mcrypt_encrypt`

Coldfusion 3DES encrypt make the encrypted result different to PHP `mcrypt_encrypt`

首先,Coldfusion Encrypt:

<cfset message = '1447841550'>
<cfset key = 'Mk9m98IfEblmPfrpsawt7BmxObt98Jev'>

<cfset ciphertext = Encrypt(#message#, #key#, "desede", "base64")>
<cfoutput>#ciphertext#</cfoutput>

然后,PHP mcrypt:

$message = "1447841550";
$key = 'Mk9m98IfEblmPfrpsawt7BmxObt98Jev';

$key = base64_decode($key);

$bytes = array(0,0,0,0,0,0,0,0); //byte [] IV = {0, 0, 0, 0, 0, 0, 0, 0}
$iv = implode(array_map("chr", $bytes));

$ciphertext = mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv);

echo base64_encode($ciphertext);

问题。

在相同的字符串中,相同的算法和相同的编码。

仍然有一小部分输出不匹配。

下面是真实的示例输出。

// Coldfusion output.

n6lp0I1w5FwrP3yPw3s8bw== 

^^^^^^^^^^

Same part


// PHP output.

n6lp0I1w5FxLQHskKMn4sw==

^^^^^^^^^^

Same part

为什么 Coldfusion 会使结果不同?

如何在不修改 PHP 代码的情况下在 Coldfusion 中获得相同的结果。 PHP 输出对我来说是正确的输出。

是否可以用javascript得到正确的结果(PHP)?这个方案也不错

我很沮丧。

设置接近,但不完全相同。结果不同的原因是:

  1. "CBC" 模式需要 IV(初始化向量)。 PHP 代码显式提供 IV,但 CF 代码不提供。所以 encrypt() 函数随机生成一个 IV。因此,为什么结果不匹配:IV 不同,结果不同。

  2. 当您使用 "NoPadding" 模式时,必须填充输入字符串,使其长度为块大小的偶数倍(即 DESEDE => 8)。据我了解,. The CF encrypt() function does not support zero padding. However, you can simulate it using something like this udf

合并这两 (2) 个更改后,结果将匹配:

结果:

n6lp0I1w5FxLQHskKMn4sw== 

示例:

<cfset message = nullPad("1447841550", 8)>
<cfset key = "Mk9m98IfEblmPfrpsawt7BmxObt98Jev">
<!--- Important: IV values should be random, and NOT reused --->
<!--- https://en.wikipedia.org/wiki/Initialization_vector --->
<cfset iv = binaryDecode("0000000000000000", "hex")>
<cfset ciphertext = Encrypt(message, key, "DESede/CBC/NoPadding", "base64", iv)>
<cfoutput>#ciphertext#</cfoutput>