Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Coldfusion 3DES encrypt使加密结果不同于PHP`mcrypt_encrypt`_Php_Encryption_Coldfusion_Mcrypt - Fatal编程技术网

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

Coldfusion 3DES encrypt使加密结果不同于PHP`mcrypt_encrypt`,php,encryption,coldfusion,mcrypt,Php,Encryption,Coldfusion,Mcrypt,首先,Coldfusion加密: <cfset message = '1447841550'> <cfset key = 'Mk9m98IfEblmPfrpsawt7BmxObt98Jev'> <cfset ciphertext = Encrypt(#message#, #key#, "desede", "base64")> <cfoutput>#ciphertext#</cfoutput> $message = "14478415

首先,Coldfusion加密:

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

<cfset ciphertext = Encrypt(#message#, #key#, "desede", "base64")>
<cfoutput>#ciphertext#</cfoutput>
$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);
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>
问题。

使用相同的字符串、相同的算法和相同的编码

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

下面是真实的示例输出

// Coldfusion output.

n6lp0I1w5FwrP3yPw3s8bw== 

^^^^^^^^^^

Same part


// PHP output.

n6lp0I1w5FxLQHskKMn4sw==

^^^^^^^^^^

Same part
为什么Coldfusion会产生不同的结果

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

是否可以使用javascript获得正确的结果(PHP)?这个解决方案也是好的


我很沮丧。

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

  • “CBC”模式需要一个(初始化向量)。PHP代码显式提供了一个IV,但CF代码没有。因此,
    encrypt()
    函数随机生成一个IV。因此,为什么结果不匹配:不同的IV,不同的结果

  • 当您使用“NoPadding”模式时,必须填充输入字符串,使其长度为块大小的偶数倍(即DESEDE=>8)。据我所知。CF
    encrypt()
    函数不支持零填充。但是,您可以使用类似于此udf的东西来模拟它

  • 合并这两(2)项变更后,结果将匹配:

    结果:

    <cfset message = '1447841550'>
    <cfset key = 'Mk9m98IfEblmPfrpsawt7BmxObt98Jev'>
    
    <cfset ciphertext = Encrypt(#message#, #key#, "desede", "base64")>
    <cfoutput>#ciphertext#</cfoutput>
    
    $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);
    
    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>
    
    示例:

    <cfset message = '1447841550'>
    <cfset key = 'Mk9m98IfEblmPfrpsawt7BmxObt98Jev'>
    
    <cfset ciphertext = Encrypt(#message#, #key#, "desede", "base64")>
    <cfoutput>#ciphertext#</cfoutput>
    
    $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);
    
    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>
    
    
    #密文#
    
    在这里解决了吗?嗨,Bardware,我试过了,但结果不一样。我知道我需要应用PHP“MCRYPT_MODE_CBC”中的算法“DESede/ECB/NoPadding”,它不适用于我的“DESede/ECB/NoPadding”或“DESede/ECB/PKCS5Padding”任何想法?对不起,我的意思是DESede/CBC/NoPadding欢迎您。。谢谢你用一个独立的例子提出了一个清晰、简洁的问题。这总是让协助变得更容易:)干杯,欢迎来到S.O。