我处于一个令人羡慕的位置,我必须维护现有 ColdFusion 应用程序的功能。作为登录过程的一部分,Coldfusion 应用程序会存储一个带有加密字符串的 cookie。
encrypt(strToEncrypt, theKey, "AES", "Base64")
我可以使用 MCrypt 和以下代码在 PHP 中成功解密此字符串
mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
base64_decode($theKey),
base64_decode($encrypted_string),
MCRYPT_MODE_ECB, "0000000000000000")
我现在需要在 PHP 中执行相同的加密,以便 ColdFusion 应用程序可以访问 cookie 中的数据。
目前我有的是
mcrypt_encrypt( MCRYPT_RIJNDAEL_128, base64_decode($theKey), $strToEncrypt, MCRYPT_MODE_ECB, "0000000000000000");
然而,这与等效的 ColdFusion 加密算法不兼容
decrypt(strToDecrypt, theKey, "AES", "Base64")
抛出给定的最终 block 未正确填充
错误。
非常感谢任何帮助。
詹姆斯
最佳答案
不知道这会有多大帮助,但我已经完成了以下工作。我认为要让 CF 开心,您必须将加密填充到一定长度
在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”作为编码。
关于php - 在 PHP 中取消加密/重新加密 ColdFusion 加密的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3196846/