我们试图用PHP中的AES / OFB / Nopadding解密coldfusion加密字符串。但是,我们认为更糟糕的是,我们在这里尝试所有解决方案,但仍然无法使其工作。
这是CF的代码
<cfsetting enablecfoutputonly="Yes">
<!--- Set encoding --->
<cfset k_strCharset="UTF-8">
<cfcontent type="text/html; charset=#k_strCharset#">
<cfset setEncoding("URL", "#k_strCharset#")>
<cfset setEncoding("FORM", "#k_strCharset#")>
<!--- Get variables --->
<cfif IsDefined("FORM.K1")><cfset fv_strK1="#FORM.K1#"><cfelse><cfset fv_strK1=""></cfif><!--- xxx --->
<cfif IsDefined("FORM.S1")><cfset fv_strS1="#FORM.S1#"><cfelse><cfset fv_strS1=""></cfif>
<cfif IsDefined("FORM.S2")><cfset fv_strS2="#FORM.S2#"><cfelse><cfset fv_strS2=""></cfif>
<!--- Encrypt / Decrypt --->
<cfif fv_strK1 is "xxx">
<cfif fv_strS1 is not "">
<cfset fv_strS2 = Encrypt(fv_strS1, fv_strK1, "AES/OFB/NoPadding", "BASE64")>
<cfelseif fv_strS2 is not "">
<cfset fv_strS1 = Decrypt(fv_strS2, fv_strK1, "AES/OFB/NoPadding", "BASE64")>
</cfif>
<cfset fv_strS3 = "">
<cfset fv_strS4 = "">
<cfset fv_strS5 = "">
</cfif>
<cfsetting enablecfoutputonly="No">
然后我们做php
<?php
$z = "bf19zWnbPmJxOvzRuP85Bw==";
$encrypted_string="q2SYE7hWWltsBw5byuwl/IkGmOOm+94=";
$source_text = html_entity_decode(getDecrypt($encrypted_string, $z), ENT_NOQUOTES, 'UTF-8');
//echo trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_OFB));
echo "<br>" . $z . "<br>";
// echo trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, base64_decode($z), base64_decode($encrypted_string), MCRYPT_MODE_OFB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_OFB), MCRYPT_RAND)));
echo "\n\nPlain-Text:\n" . $source_text . "\n";
// Functions
function getDecrypt($str, $key) {
return ofb_decrypt(base64_decode($str),$key);
}
function ofb_decrypt($str, $key, $iv = ' ' )
{
if ($iv==' ' & strlen($str) < 16)
return false;
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, ' ' , MCRYPT_MODE_NOFB, ' ');
//RECEOVER IV
$iv_size = mcrypt_enc_get_iv_size($td);
if (empty($iv)) {
$iv = substr($str,0,$iv_size);
$str = substr($str,$iv_size);
}
// initialize encryption
mcrypt_generic_init($td, $key, $iv);
// decrypt
$decrypted_string = mdecrypt_generic($td, $str);
// terminate decrtypion
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $decrypted_string;
}
?>
$ encrypted_string是使用上面的CF脚本创建的。
然后我们得到的结果是 = @ &amp; O%NSC # p :
如果有人能给我一个提示,我非常感激。谢谢
答案 0 :(得分:0)
我能够得到&#34;测试&#34; (以及一些垃圾)使用记录不良的&nbsp;&#39; ncfb&#39; mcrypt_decrypt(http://php.net/manual/en/mcrypt.constants.php)的参数。我认为垃圾与块大小有关......一些额外的输入示例会有所帮助。
function decryptColdfusionString($key, $data)
{
$retVal = mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
base64_decode($key),
base64_decode($data),
'ncfb',
'0000000000000000'
);
return $retVal;
}
$key = "bf19zWnbPmJxOvzRuP85Bw==";
$data = "q2SYE7hWWltsBw5byuwl/IkGmOOm+94=";
echo decryptColdfusionString($key, $data) . PHP_EOL;
答案 1 :(得分:0)
(我意识到这已经有几年了,但万一有人遇到同样的问题......)
OFB模式需要IV。虽然ColdFusion代码没有明确指定IV ,one was still generated automatically and prepended to the result。解密时必须正确提取IV,否则结果将是胡言乱语。 1.首先解码加密的base64字符串 。 2.然后从解码值中提取IV和数据并照常解密。
<强> PHP:强>
$key = base64_decode("bf19zWnbPmJxOvzRuP85Bw==");
$encrypted="q2SYE7hWWltsBw5byuwl/IkGmOOm+94=";
$decoded = base64_decode($encrypted);
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_NOFB);
$iv = substr($decoded, 0, $ivSize);
$data = substr($decoded, $ivSize);
$text = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_NOFB, $iv);
<强>结果:强>
testing