【问题标题】:Coldfusion 3DES encrypt make the encrypted result different to PHP `mcrypt_encrypt`Coldfusion 3DES 加密使加密结果不同于 PHP `mcrypt_encrypt`
【发布时间】:2016-02-22 22:18:06
【问题描述】:

起初,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)?这个方案也不错。

我很沮丧。

【问题讨论】:

  • 在这里解决了吗? stackoverflow.com/a/2819186/577052
  • 嗨 Bardware,我试过了,但结果不一样。我知道我需要像 PHP "MCRYPT_MODE_CBC" 一样应用算法 "DESede / ECB / NoPadding" 它不适用于我 "DESede / ECB / NoPadding" 或 "DESede / ECB / PKCS5Padding" 有什么想法吗?
  • 对不起,我的意思是 DESede/CBC/NoPadding

标签: php encryption coldfusion mcrypt


【解决方案1】:

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

  1. “CBC”模式需要IV(初始化向量)。 PHP 代码明确提供 IV,但 CF 代码不提供。所以encrypt() 函数会随机生成一个IV。因此结果不匹配的原因:不同的 IV,不同的结果。

  2. 当您使用“NoPadding”模式时,必须填充输入字符串,使其长度为块大小的偶数倍(即 DESEDE => 8)。据我了解,"...the mcrypt extension of PHP only uses ZeroPadding"。 CF encrypt() 函数不支持零填充。但是,您可以使用类似这样的 udf nullPad()

  3. 来模拟它

一旦您合并了这两 (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>

【讨论】:

  • 不客气 .. 并感谢 you 发布了一个带有独立示例的清晰、简洁的问题。这总是让帮助变得更容易:) 干杯,欢迎来到 S.O.
猜你喜欢
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多