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

在Coldfusion中加密,然后在PHP中解密

发布时间:2020-12-13 18:12:45 所属栏目:PHP教程 来源:网络整理
导读:我有一个问题,复制 PHP和Coldfusion中生成的相同结果. 在PHP加密这种方式: ?php $key = "$224455@"; $Valor = "TESTE"; $base = chop(base64_encode(mcrypt_encrypt(MCRYPT_DES,$key,$Valor,MCRYPT_MODE_ECB))); ? 我有结果: TzwRx5Bxoa0= 在Coldfusion这
我有一个问题,复制 PHP和Coldfusion中生成的相同结果.

在PHP加密这种方式:

<?php
    $key = "$224455@";
    $Valor = "TESTE";

    $base = chop(base64_encode(mcrypt_encrypt(MCRYPT_DES,$key,$Valor,MCRYPT_MODE_ECB)));     
?>

我有结果:

TzwRx5Bxoa0=

在Coldfusion这样做:

<cfset Valor = "TESTE">
<cfset Key = "$224455@">
<cfset base = Encrypt(Valor,ToBase64(Key),"DES/ECB/PKCS5Padding","BASE64")>

结果:

qOQnhdxiIKs=

什么不是ColdFusion产生与PHP相同的价值?

非常感谢你

(评论太长)

Artjom B. already provided the answer above. Artjom B.写道

The problem is the padding. The mcrypt extension of PHP only uses
ZeroPadding […] you either need to pad the plaintext in php […] or
use a different cipher in ColdFusion such as “DES/ECB/NoPadding”. I
recommend the former,because if you use NoPadding,the plaintext must
already be a multiple of the block size.

不幸的是,很难在CF中生产null character. AFAIK,唯一有效的技术是use URLDecode("%00").如果你不能修改PHP代码为@Artjom B.建议,你可以尝试使用下面的函数填充CF中的文本.免责声明:它只是经过轻微测试(CF10),但似乎产生与上述相同的结果.

更新:
自CF encrypt()函数always interprets the plain text input as a UTF-8 string起,您还可以使用charsetEncode(bytes,“utf-8”)从单个元素字节数组创建空字符,即charsetEncode(javacast(“byte []”,[0]),“utf-8”)

例:

Valor = nullPad("TESTE",8);
Key = "$224455@";
result = Encrypt(Valor,"DES/ECB/NoPadding","BASE64");
// Result: TzwRx5Bxoa0=
WriteDump( "Encrypted Text = "& Result );

功能:

/*
   Pads a string,with null bytes,to a multiple of the given block size

   @param plainText - string to pad
   @param blockSize - pad string so it is a multiple of this size
   @param encoding - charset encoding of text
*/
string function nullPad( string plainText,numeric blockSize,string encoding="UTF-8")
{
    local.newText = arguments.plainText;
    local.bytes = charsetDecode(arguments.plainText,arguments.encoding);
    local.remain = arrayLen( local.bytes ) % arguments.blockSize;

    if (local.remain neq 0) 
    {
        local.padSize = arguments.blockSize - local.remain;
        local.newText &= repeatString( urlDecode("%00"),local.padSize );
    }

    return local.newText;
}

(编辑:李大同)

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

    推荐文章
      热点阅读