在 PHP 中取消加密/重新加密 ColdFusion 加密字符串
我处于一种令人羡慕的境地,我必须使用现有的 ColdFusion 应用程序来维护功能.作为登录过程的一部分,Coldfusion 应用程序会存储一个带有加密字符串的 cookie.
I'm in the unenviable position where I have to maintain functionality with an existing ColdFusion application. As part of it's login process the Coldfusion app stores a cookie with an encrypted string.
encrypt(strToEncrypt, theKey, "AES", "Base64")
我可以使用 MCrypt 和以下代码在 PHP 中成功解密此字符串
I can successfully decrypt this string in PHP using MCrypt and the following code
mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
base64_decode($theKey),
base64_decode($encrypted_string),
MCRYPT_MODE_ECB, "0000000000000000")
我现在需要在 PHP 中执行相同的加密,以便 ColdFusion 应用程序可以访问 cookie 中的数据.
I now have the need to perform the same encryption within PHP so that the ColdFusion app can access the data in the cookie.
目前我拥有的是
mcrypt_encrypt( MCRYPT_RIJNDAEL_128, base64_decode($theKey), $strToEncrypt, MCRYPT_MODE_ECB, "0000000000000000");
然而,这与等效的 ColdFusion 加密算法不兼容
This, however, is incompatible with the equivalent ColdFusion encryption algorithm
decrypt(strToDecrypt, theKey, "AES", "Base64")
抛出一个给定的最终块没有正确填充
错误.
非常感谢任何帮助.
詹姆斯
推荐答案
不知道这会有多大帮助,但我已经完成了以下工作.我认为为了让 CF 开心,你必须将你的加密填充到一定的长度
Don't know how much help this will be but I have had the following working. I think to make CF happy you have to pad your encryption to a certain length
在 CF 中加密
Encrypt(data, encKey, 'AES/CBC/PKCS5Padding', encoding, encIv)
PHP 中的解密
function Decode($data, $encKey, $encIv, $format = 'uu') {
if ($format === 'uu') {
$data = Convert_uudecode($data);
} else if ($format === 'hex') {
$data = Pack('H*', $data);
} else if ($format === 'base64') {
$data = Base64_Decode($data);
} else if ($format === 'url') {
$data = UrlDecode($data);
}
$data = MCrypt_decrypt(MCRYPT_RIJNDAEL_128, $encKey, $data, 'cbc', $encIv);
$pad = Ord($data{strlen($data)-1});
if ($pad > strlen($data)) return $data;
if (strspn($data, chr($pad), strlen($data) - $pad) != $pad) return $data;
return substr($data, 0, -1 * $pad);
}
PHP 加密
function Encode($data, $encKey, $encIv, $format = 'uu') {
$pad = 16 - (StrLen($data) % 16);
if ($pad > 0) {
$data .= Str_repeat(Chr($pad), $pad);
}
$data = MCrypt_encrypt(MCRYPT_RIJNDAEL_128, $encKey, $data, 'cbc', $encIv);
if ($format === 'uu') {
return Convert_uuencode($data);
} else if ($format === 'hex') {
return Bin2Hex($data);
} else if ($format === 'base64') {
return Base64_Encode($data);
} else if ($format === 'url') {
return UrlEncode($data);
}
}
在 CF 中解密
Decrypt(data, encKey, 'AES/CBC/PKCS5Padding', encoding, encIv)
由于某种我不记得的原因,我倾向于使用uu"作为编码.
For some reason that I can't remember, I favoured 'uu' for the encoding.
相关文章