5

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

Please tell me what I am doing wrong.

Thank you!

4

1 回答 1

2

AES 总是以 16 字节为单位对事物进行加密。显然 mcrypt_encrypt用零字节填充字符串,直到它是 16 的倍数。mcrypt_decrypt尽职尽责地解密它,但缺乏删除填充的信息。而且您在自欺欺人,因为即使oppa_dec 实际上以 9 个零字节结尾,显示的值看起来也一样。请改用合理的填充方案。–格雷格

要删除这些空字符,您可以使用该rtrim函数。在运行解密后的输出之后,它应该是相等的。

于 2013-03-12T02:17:31.677 回答