Php encryption sql

I need a form where a user can change their password. I can encrypt the password, however, when it is fetched from the database (the original password says "test") it will not recognize it.

This is when the password was encrypted in the db. I check if the entered password in the form matches the one in the db:

SELECT * from table where password = md5('$typed_password')

      

This is how it is encrypted:

UPDATE table set field = md5('$typed_password' )

      

How can my choice work so that when the user enters it in the form, the original is recognized?

+2


source to share


3 answers


First: MD5 is a cryptographic hash function , not necessarily an encryption method. The hash is designed to run in one direction only and cannot be reversed. (it's good)

MD5, however, is cryptographically broken (it is no longer considered secure); you have to use a different hash function (preferred Bcrypt hash or at least SHA256)

Looking at the code, I see several errors:

  • your password is not salted
  • I really hope to $typed_password

    be properly sanitized, or you are in SQL injection .
  • You are trying to select all users from the table with the same password.

The easiest (and probably best) way to make passwords is to use the standard library: Portable hashing PHP passwords and make sure you use the algorithm CRYPT_BLOWFISH

.



require('PasswordHash.php');

$pwdHasher = new PasswordHash(8, FALSE);

// $hash is what you would store in your database
$hash = $pwdHasher->HashPassword( $password );

// $hash would be the $hash stored in your database for this user
$checked = $pwdHasher->CheckPassword($password, $hash);
if ($checked) {
    echo 'password correct';
} else {
    echo 'wrong credentials';
}

      

store / check / update requests should be tied to user id:

// Insert query
$query = "INSERT INTO users VALUES({$userId}, '{$username}', '{$hash}')";

// Select query
$query = "SELECT hash FROM users WHERE userId = {$userId}";

// Update query
$query = "UPDATE users SET hash = '{$hash}' WHERE userId = {$userId}";

      

And then you should use parameterized queries instead of directly passing variable values ​​into the query.

I realize this is a lot of information at once, but it's important if you don't want your script to be hacked by almost all programmers.

+4


source


Why not encrypt the password in PHP and then the INSERT

encrypted one.
The same with SELECT

.

So:

$enc_passwd = md($typed_password);
$sql = "SELECT * FROM table WHERE password = '$enc_passwd')";

      



looks like UPDATE

(why not INSERT

?)

+1


source


Recently Posting Passwords I digress a bit and covered a lot. Labyrinth:

Once you are happy with the password they chose, encrypt it first with PHP, then save. The following password encryption function is also not my idea, but it solves a number of problems. Encryption inside PHP prevents your unencrypted passwords from being intercepted on a shared server. Adding something to a user that will not change (I use an email address as this is the username for my sites) and adding a hash (SALT is a short constant string that I change to the site) increases resistance to attacks. Since the SALT is inside the password, and the password can be of any length, it becomes nearly impossible to attack this with a rainbow table. Alternatively, this also means that people cannot change their email address, and you cannot change SALT, but will not cancel the password.

function password_crypt($email,$toHash) {
   $password = str_split($toHash,(strlen($toHash)/2)+1);
   return hash('sha256', $email.$password[0].SALT.$password[1]); 
}

      

So, the first time you enter the user's password in pseudocode:

define(SALT,'blah');
$hashed_password = password_crypt($email,$password);
INSERT INTO users (email,hashed_password) VALUES ($email,$hashed_password);

      

Then, to test the subsequent login in pseudocode:

define(SALT,'blah');
$user_hashed_password = password_crypt($_POST['username'],$_POST['password']);
SELECT email FROM users WHERE email = ? AND hashed_password = $user_hashed_password LIMIT 1

      

If you get the string back, use login.

0


source







All Articles