You should not "encrypt" the password at all. I know this sounds contradictory. But there is no reason why you will need to decrypt the password. For this, your database will be opened for hackers, because if you store your decryption password in your codes / servers, the hacker can steal this information.
The correct process is the hash password. A hash is a one-way (cannot be returned to source) process. The current standard would be to use SHA256 to hash your password. Here is the basic flowchart:
- Take your user password. The example password "mypass" will have the value
ea71c25a7a602246b4c39824b855678894a96f43bb9b71319c39700a1e045222 - Save this hash (
ea71c25a7a602246b4c39824b855678894a96f43bb9b71319c39700a1e045222 ) in your database.
When the user logs in, you take the password that he just sent and the hash of it. If he enters the same password, he will hash to the same value in your database.
When the user switches to changing passwords, you get "enter your old password" to check that the old password still matches, if it contains the hash "enter new password" and save it.
In my example, I did not mention salt . This is what you should use on your system as it protects your data from rainbow table exploits. But this is for another discussion.
Hope this helps :)
source share