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

前端之家收集整理的这篇文章主要介绍了在Coldfusion中加密,然后在PHP中解密前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个问题,复制 PHP和Coldfusion中生成的相同结果.

PHP加密这种方式:

  1. <?PHP
  2. $key = "$224455@";
  3. $Valor = "TESTE";
  4.  
  5. $base = chop(base64_encode(mcrypt_encrypt(MCRYPT_DES,$key,$Valor,MCRYPT_MODE_ECB)));
  6. ?>

我有结果:

TzwRx5Bxoa0=

在Coldfusion这样做:

  1. <cfset Valor = "TESTE">
  2. <cfset Key = "$224455@">
  3. <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”)

例:

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

功能

  1. /*
  2. Pads a string,with null bytes,to a multiple of the given block size
  3.  
  4. @param plainText - string to pad
  5. @param blockSize - pad string so it is a multiple of this size
  6. @param encoding - charset encoding of text
  7. */
  8. string function nullPad( string plainText,numeric blockSize,string encoding="UTF-8")
  9. {
  10. local.newText = arguments.plainText;
  11. local.bytes = charsetDecode(arguments.plainText,arguments.encoding);
  12. local.remain = arrayLen( local.bytes ) % arguments.blockSize;
  13.  
  14. if (local.remain neq 0)
  15. {
  16. local.padSize = arguments.blockSize - local.remain;
  17. local.newText &= repeatString( urlDecode("%00"),local.padSize );
  18. }
  19.  
  20. return local.newText;
  21. }

猜你在找的PHP相关文章