简体   繁体   English

密码相同,但MD5哈希不同

[英]Same password but different md5 hash

When I pass the clear password to check the login of the user and apply a md5() on the string, the md5 hash is equal to the md5 hash stored in the MySQL database (Login succeed). 当我通过清除密码以检查用户的登录并将md5()应用于字符串时,md5哈希值等于存储在MySQL数据库中的md5哈希值(成功登录)。

But I don't want to transfert the user password in clear inside my POST function, so I decided to use cryptoJS to only send the key and then decrypt the password on the PHP server side. 但是我不想在POST函数内部以明文形式传输用户密码,因此我决定使用cryptoJS仅发送密钥,然后在PHP服务器端解密密码。

The problem is, when I'm using the decrypted password, the md5 is different. 问题是,当我使用解密的密码时,md5是不同的。 This is weird because the clear password string is the same than the decrypted password, and the md5 hash is different. 这很奇怪,因为清除密码字符串与解密密码相同,并且md5哈希不同。

By doing: 通过做:

 var_dump($clearPassword); //Hello.
 var_dump($decryptedPassword); //Hello.

But: 但:

 var_dump(md5($clearPassword)); //3ea484671d7b00a1df4734ded1aa379c1.
 var_dump(md5($decryptedPassword)); //470a1ad08cbdebe075214591ea20fec9.

As you can see, it's exactly the same string but the md5 hash is different, I've noticed that var_dump() give as an output: 如您所见,它是完全相同的字符串,但md5哈希不同,我注意到var_dump()作为输出提供:

 string(16) for the $clearPassword;
 string(32) for the $decryptPassword;

I tried to change the string encoding but there's no luck. 我试图更改字符串编码,但是没有运气。 Anyone can explain me why md5() behave like that with those same passwords string? 任何人都可以解释我为什么md5()在使用相同的密码字符串时会表现出类似的行为? thanks again. 再次感谢。

the decrypted password IS NOT the same as the original. 解密的密码与原始密码不同。 Check the length of the two strings, check the encoding, do a byte to byte comparison. 检查两个字符串的长度,检查编码,做一个字节到字节的比较。 "Hello\\0" and "Hello \\0" seems identical but they are not. “ Hello \\ 0”和“ Hello \\ 0”似乎相同,但不相同。 Even "Hello\\0" and "Hello\\0\\0\\0" are not the same. 甚至“ Hello \\ 0”和“ Hello \\ 0 \\ 0 \\ 0”也不相同。 Maybe the decryption algorithm gives a string length of 32 bytes. 解密算法可能给出的字符串长度为32个字节。

我可能会像@fpierrat所说的那样做,只是在客户端加密并在PHP服务器中直接比较md5哈希。

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

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