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

前端之家收集整理的这篇文章主要介绍了在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.f2er.com/php/134971.html

猜你在找的PHP相关文章