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

Un-encrypting / re-encrypting a ColdFusion encrypted string in PHP(在 PHP 中取消加密/重新加密 ColdFusion 加密字符串)
本文介绍了在 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.

这篇关于在 PHP 中取消加密/重新加密 ColdFusion 加密字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)