7

I am currently using MD5 encryption for storing the password in the database. We didn't have the password reset functionality before. But now we are implementing it. So I can't decrypt MD5 and send the password to the user. But I can do if it is encrypted in base64.Now I am little bit confused which is best encryption method.
I already did the client side validation for strong password (like 8 char length, special characters etc).

Aqeel Ashiq
  • 1,988
  • 5
  • 24
  • 57
svk
  • 4,513
  • 18
  • 63
  • 100
  • 1
    Please read this previous question + answers: http://stackoverflow.com/questions/2780198/phphow-to-send-the-original-password-to-the-user-when-he-clicks-forgot-password – Amber Oct 22 '10 at 04:49
  • 1
    Don't validate on the client side unless you're okay with that validation being broken. If someone writes an alternate client at some point, you don't want them poking holes in your security. You can do the check on the client side as well, but that would only be to avoid a round trip. – Merlyn Morgan-Graham Oct 22 '10 at 04:52
  • Md5 hash isn't as secure as it once was, use sha1 at least and sha256 is (from what I know) secure enough. – Joakim Oct 22 '10 at 04:53
  • @Joakim: MD5 was never that secure. – Amber Oct 22 '10 at 05:03
  • @Amber indeed, but now we know that it is insecure, and it is widely known how to exploit those vulnerabilities. – pkaeding Oct 22 '10 at 05:07
  • If you're going to store passwords using base64 encoding, you might as well just store them as plain text! – mellowsoon Oct 23 '10 at 06:24
  • removed php tag – Aqeel Ashiq Apr 12 '19 at 10:32

4 Answers4

26

Base 64 is not an encryption mechanism, it is an encoding scheme. It is easily reversed, so it is not a good choice for protecting critical data.

The common approach for passwords is to hash them with something like MD5, and then store the hash. When the user logs in again, hash the input password, and compare that to the stored hash.

If the user forgets his password, you should not be able to tell him what it is. Instead, allow him to reset it to something else (presumably something he can remember).

Also, as @Phil Brown mentions, MD5 is not considered a strong encryption mechanism. SHA-1 would be better suited for this task.

Base 64 encoding is generally used to transmit binary data over a mechanism that only allows ASCII text.

pkaeding
  • 36,513
  • 30
  • 103
  • 141
  • 4
    The common approach for forgotten passwords is to reset it, send the user the new password and force them to change it when they login. Also, SHA1 is a stronger hashing algorithm than MD5 – Phil Oct 22 '10 at 04:46
  • at this forgot password time whether I can get the password once again? – svk Oct 22 '10 at 04:46
  • 2
    @vinothkumar: You don't. You randomly generate a completely new password for them - not of their choosing. In addition, once they get their password reset, I'd consider locking the account if they fail to change their password after some period of time, since e-mail is sent in clear text. – Merlyn Morgan-Graham Oct 22 '10 at 04:57
  • MD5 is not an encryption mechanism at all. It's a hashing mechanism. – Rob Grant Jun 22 '21 at 13:37
8

Base64 and MD5 are not encryption methods. Base64 is simply a way of encoding characters, which provides absolutely no security - it is as good as storing the password in plain text. MD5 is a hash function, which means it is one-way and cannot be decrypted.

Hashing is definitely the way to go. MD5 is okay, but you should switch to a more secure function such as SHA-256.

As for a "forgot password" feature, never store the user's password and send it back to them. Instead, generate a (random) temporary password for them so that they can login and change it.

casablanca
  • 69,683
  • 7
  • 133
  • 150
7

Base64 is not encryption, it is an easily reversible encoding mechanism. MD5 is a one-way cryptographic hash, though its use is not recommended because it is cryptographically weak.

For your needs you probably want to store the hash of the password (better with salt), probably using SHA-256 or better. When users forget their password, you generate a random one-time use password for them and force them to recreate a password, or just make them do it after verifying some credentials.

wkl
  • 77,184
  • 16
  • 165
  • 176
1

Best practice is to store the password hash using MD5 as you are now (or even better SHA256).

Don't do password recovery. Instead, when a user forgets their password, create a new random password and send it to them. They can then login and set the password to something they prefer. Much more secure.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116