简体   繁体   English

PHP mcrypt_encrypt / mcrypt_decrypt问题,返回不同的值

[英]PHP mcrypt_encrypt/mcrypt_decrypt issue, returns a different value

I was looking for an answer but could not find it here. 我正在寻找答案,但在这里找不到。 Please excuse me if this question was already asked. 如果已经提出这个问题,请原谅。

I have a simple code encrypting and decrypting a string, strings look the same, but when comparing them using == they do not appear to be the same, so hashes are different as well.. 我有一个简单的代码加密和解密字符串,字符串看起来一样,但是当使用==比较它们时它们似乎不一样,所以哈希也是不同的...

Here is my code: 这是我的代码:

$oppa = "rompish";
$opp_enc = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, "key", $oppa, MCRYPT_MODE_ECB);
$opp_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, "key", $opp_enc, MCRYPT_MODE_ECB);

echo $oppa."<br />".$opp_dec."<br />";

if ($oppa == $opp_dec) echo "YAY"; else echo "NOPE";

On the page: 在页面上:

rompish rompish NOPE 肆无忌惮的NOPE

Please tell me what I am doing wrong. 请告诉我我做错了什么。

Thank you! 谢谢!

AES always encrypts things in blocks of 16 bytes. AES总是以16字节的块加密事物。 Apparently mcrypt_encrypt pads the string out with zero bytes until it is a multiple of 16. mcrypt_decrypt dutifully decrypts this but lacks the information to remove the padding. 显然mcrypt_encrypt用零字节mcrypt_encrypt字符串,直到它是16的mcrypt_decrypt尽职地解密这个但缺少删除填充的信息。 And you are fooling yourself because the displayed values look the same even though oppa_dec actually ends with 9 zero bytes. 而你自欺欺人,因为即使oppa_dec实际上以9个零字节结束,显示的值看起来也是一样的。 Use a sensible padding scheme instead. 请使用合理的填充方案。 GregS - 格雷格

To remove these null characters, you can use the rtrim function. 要删除这些空字符,可以使用rtrim函数。 After running the decrypted output through that it should be equal. 通过它运行解密输出后它应该是相等的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM