Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypt password before storing in database?

I have a password being passed from my iPhone app to the database via a php script, user.php.

The variable $pass is populated by the following:

$pass = str_replace("'", "", $_REQUEST['pass']);

How can I encrypt this before it's inserted into my database? I've read a little about the different techniques, but looking for the best way to manage this.

Thanks to everyone.

like image 555
BigMike Avatar asked Oct 20 '10 19:10

BigMike


People also ask

Can we store encrypted password in a database?

Encrypted passwordsIn some cases, passwords are stored in a database after being encrypted by a reversible algorithm (rot13, mask encryption…).

Should you encrypt password before sending to server?

Use HTTPS. Securely hash passwords, irreversibly, with a unique salt per password. Do this on the client - do not transmit their actual password. Transmitting the users original password to your servers is never "OK" or "Fine".

Should passwords be stored in a database?

It sounds simple but needs to be done carefully to make sure your users are protected against data breaches or potential security failures. In a nutshell, you should never really store user passwords in the database! Yes, you read it right. User passwords as is (otherwise called plain-text) should never be stored.

What is the significance of encryption while saving password in database?

Encryption helps us by protecting data from hackers. In network communication, the same techniques can be used in saving passwords. Any encryption algorithm can be used to protect passwords. So on registration plain text passwords are encrypted and saved to your database.


2 Answers

While the answer below is technically still correct, php has new recommendations with regards to the hashing algorithms to use. Their recommendation, as of php >= 5.5.0, is to use the password_hash and password_verify functions to hash and verify hashed passwords . As an added benefit, these functions automatically include an individualized salt as part of the returned hash, so you don't need to worry about that explicitly.


If you don't care about retrieving the actual password's value (from the database encrypted value), you can run a one-way hash algorithm on it (such as sha1). This function will return a specific length string (hash) which cannot be used to find the original string (theoretically). It is possible that two different strings could create the same hash (called a collision) but this shouldn't be a problem with passwords.
Example: $pass = sha1($_REQUEST['pass']);

One thing, to make it a little more secure is to add a salt to the hash and run the hash function again. This makes it more difficult to generate a password hash maliciously since the salt value is handled server-side only.
Example: $pass = sha1(sha1($_REQUEST['pass']).sha1("mySalt@$#(%"));

like image 121
Richard Marskell - Drackir Avatar answered Sep 22 '22 23:09

Richard Marskell - Drackir


Use php's crypt library. Md5 is not encryption, it is hashing.

Also, salt your passwords. Why?

  • This answer
  • Another good answer
like image 27
Chris Avatar answered Sep 24 '22 23:09

Chris