在 PHP 中取消加密/重新加密 ColdFusion 加密字符串

发布于 2024-09-08 16:33:50 字数 806 浏览 7 评论 0原文

我的处境并不令人羡慕,我必须维护现有 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")

抛出 Given Final block notproperly padded 错误。

非常感谢任何帮助。

詹姆斯

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")

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")

I now have the need to perform the same encryption within PHP so that the ColdFusion app can access the data in the cookie.

At the moment what I have is

mcrypt_encrypt( MCRYPT_RIJNDAEL_128, base64_decode($theKey), $strToEncrypt, MCRYPT_MODE_ECB, "0000000000000000");

This, however, is incompatible with the equivalent ColdFusion encryption algorithm

decrypt(strToDecrypt, theKey, "AES", "Base64")

Throwing a Given final block not properly padded error.

Any help much appreciated.

James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2024-09-15 16:33:50

不知道这会有多大帮助,但我已经进行了以下工作。我认为要让 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”进行编码。

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

Encrypt in CF

Encrypt(data, encKey, 'AES/CBC/PKCS5Padding', encoding, encIv)

Decrypt in 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); 
}

Encrypt in 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);
    }
}

Decrypt in CF

Decrypt(data, encKey, 'AES/CBC/PKCS5Padding', encoding, encIv)

For some reason that I can't remember, I favoured 'uu' for the encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文