问题
I have encoded a string with md5()
and I want to decrypt the same string.
I have written the following code:
<?php
$rr = md5(utf8_encode('hello'));
echo $rr;
$rr1 = md5(utf8_decode('5d41402abc4b2a76b9719d911017c592'));
echo '<br/>' .$rr1;
?>
Now I am able to encrypt the string. But now, I want to decrypt the string. I have tried:
md5(utf_decode('string'));
But it is not working. How can I decrypt the string which is encrypted with md5()
?
回答1:
You can't! MD5 is a one-way hash, not encryption. The whole point is that it's supposed to be impossible to reverse. What you'd use it for is, for example, storing a password. You don't need to know what the password is; you just need to md5()
whatever the user typed into the password field, and see if it matches the stored md5()
value of the user's password.
If you want to be able to encrypt and decrypt, look into something like AES / Twofish / Blowfish. The MCrypt module will help you quite a bit.
This question provides some additional resources for how to encrypt and decrypt in PHP.
I should also point out that md5()
is a weak, collision-prone hashing algorithm, and shouldn't be trusted for high-security applications. I would use sha256 at minimum, or sha512.
来源:https://stackoverflow.com/questions/35137474/how-to-decrypt-an-md5-string-in-php