微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个问题,复制 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;
}

原文地址:https://www.jb51.cc/php/134971.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐