daniel__
daniel__

Reputation: 11845

Database encryption - php/mysql

I have some doubts about the best way to do a database with passwords. I need encryption in the passwords, but if i use MD5 i can't recover the pass, isn't it?

And the base64 encoder ? it is secure? with this encryption the recover isn't more a problem.

Suggestions? what is the best way? e prefer a solution that permit to remember the old pass, and not define a new one password.

Thanks!!!

If anybody know a good tutorial about secure passwords in a database i really appreciate that

Upvotes: 2

Views: 3199

Answers (7)

dunedain289
dunedain289

Reputation: 2388

Stop now and read this. Then go find an open source library to do user authentication. I'm not a PHP dev, so I can't refer you to one, but I'm sure they exist. They'll have had the security holes found already.

Also, for passwords, you should be looking at bcrypt or similarly slow hash functions for passwords anyways, instead of using a fast hash algorithm like MD5 or SHA.

Upvotes: 0

ircmaxell
ircmaxell

Reputation: 165193

First off, there's a Significant Difference Between Hashing and Encryption. I suggest that you give that a read before going on...

Now, as to your exact question, there are a few ways to go about it.

  1. Encrypt your passwords with a strong cipher so that you can decrypt them when necessary. A solution such as the one in this post may work for that. However, please note that this isn't a great idea, since if your system is ever compromised, all the passwords will be leaked (never a good idea). There are very few use-cases where it makes sense to store them encrypted, but if you absolutely must, please use a strong cryptographic encryption routine to do it...

  2. Store your passwords using a strong one-way hashing method. No, md5($password) is not good enough. But neither is sha1($salt . $password). The first is trivial to lookup most passwords, and the second can be brute-forced in a reasonable amount of time by simple trial and error. Instead, stretch your passwords iteratively. The best way is to use the standard PBKDF2 function to generate a strong one-way key from the password.

    As far as how to recover if the user forgets a password, don't worry about it. If the user forgets his password, create a new one and give that one to the user. It's the industry standard way of dealing with forgotten passwords (Heck, both Windows and Mac do it that way). You may think that you're doing your users a favor by sending it to them, but all you're doing is turning off anyone who has a clue about security from every using your application (and making a lot of people mad if you get compromised).

Upvotes: 1

Ashraf Abrahams
Ashraf Abrahams

Reputation: 769

As mentioned, use a hash instead of encryption when saving passwords. I generally don't use a random salt since this means an extra field in the DB so that you can authenticate the user. The simplest solution is to use the password as the salt as shown below. Easy to use and easy to authenticate.

$salt = $_POST['password'];
$hash = sha1( $salt . $_POST['password'] );

Upvotes: 0

code_burgar
code_burgar

Reputation: 12323

MD5 is not used for encryption (which implies that it can be decrypted) but rather for message digestion/hashing. Base64 is also not encryption but rather encoding, which can be decoded with no effort.

There is usually little point in storing encrypted passwords in a database if they can be easily decrypted.

The secure approach is to store only hashes and compare submitted passwords to stored hashes after hashing them on the fly.

You should be doing something along the lines of:

$salt = 'some2%string!!here1';
$hash = sha1( $salt . $_POST['password'] );

to create a hash of the password. You store that hash in the database. When a user wants to log in, you take his submitted function, hash it using the same process, and compare to the hash in the database. If they match, the password is correct.

Upvotes: 2

Carson Myers
Carson Myers

Reputation: 38564

You should not ever need to remember the user's password - to do so is a violation of their trust and presents a security hole.

Normally you will hash the password with MD5 (these days it's better to use SHA-2), and when the user submits their password to try and log in, hash that password as well, and see if the hashes are a match.

For added security, you can create a "salt" column to the database and generate a random salt when the password is first submitted. Add the salt to the beginning of the password, and then hash it. Store the hash of the salt+password, and the salt together. Now when the user submits his password to log in, you would combine it with the salt, hash it, and check if the hash is a match.

The salt ensures that if multiple users have the same password (chances are they do), their password hashes will not be identical.

If the user forgets their password they will have to provide a new one, simply storing their password and sending it back to them when they forget is bad practice and a sign to the user that you aren't handling their privacy very well.

Upvotes: 0

Marc B
Marc B

Reputation: 360562

base64 isn't "encryption". It's intended to convert binary data into a format that's safe for transmission through potentially "broken" email systems that can't process 8-bit binary data properly. It's a best the equivalent of a cereal box decoder ring.

If you want encryption, there's AES, DES, and various other functions available. Problem is, if your code can decrypt the password, the it's trivial for an attacker to figure out how you do it, and/or subvert your code to do it for them.

Passwords should never be stored in a format where the plaintext can be retrieved. If a user forgets their password, you wipe out the old one, generate a new temporary one, and force them to change this temporary password to something else on first login.

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 400912

if i use MD5 i can't recover the pass, isn't it?

Indeed, if you hash your password using md5 or sha1 (adding a salt is a good idea, btw), you will not be able to recover the password ; and that's the goal of doing so !

The idea is if anyone is able to take a look at your database (be it some evil doer, or one of your employees -- who can be an evil-doer), he will not be able to find any usefull password.


what is the best way? e prefer a solution that permit to remember the old pass, and not define a new one password.

The best way is to do the opposite of what you want : not allow one to get his old password -- and develop some way of generating a new password.

This way, you will ensure that no-one is able to get a dump of your logins and corresponding password ; which will make your service safer for your users (especially considering that many people use the same login/password couple of several websites).

Upvotes: 4

Related Questions