加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:03:53 所属栏目:PHP教程 来源:网络整理
导读:我处于一个不值得羡慕的位置,我必须使用现有的ColdFusion应用程序来维护功能.作为登录过程的一部分,Coldfusion应用程序存储带有加密字符串的cookie. encrypt(strToEncrypt,theKey,"AES","Base64") 我可以使用MCrypt和以下代码在PHP中成功解密此字符串 mcrypt
我处于一个不值得羡慕的位置,我必须使用现有的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,$strToEncrypt,"0000000000000000");

但是,这与等效的ColdFusion加密算法不兼容

decrypt(strToDecrypt,"Base64")

抛出给定的最终块未正确填充错误.

任何帮助非常感谢.

詹姆士

解决方法

不知道这会有多大的帮助,但我有以下工作.我认为让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,$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,-1 * $pad); 
}

用PHP加密

function Encode($data,$format = 'uu') {
    $pad = 16 - (StrLen($data) % 16);
    if ($pad > 0) {
        $data .= Str_repeat(Chr($pad),$pad);
    }
    $data = MCrypt_encrypt(MCRYPT_RIJNDAEL_128,$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,encIv)

由于某些我无法记住的原因,我赞成’uu’进行编码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读