2

I'm kind of new to this stuff, but this is my login authorization page.

function encrypt($password, $salt) {
    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($salt), $password, MCRYPT_MODE_CBC, md5(md5($salt))));
}

$query = "SELECT * FROM users WHERE LOWER(`username`) = :user";
$stmt = $dbh->prepare($query);
$stmt->bindValue(':user', strtolower($_POST['username']));
$stmt->execute();

if ($stmt->rowCount() == 1) {
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    if (encrypt($_POST['password'], $row['salt']) == $row['password']) {
        echo "Logged in!";
    }
}

In my database, I have columns username, password, and salt. The password column contains the encrypted password.

Is my workflow correct here?

Norse
  • 5,674
  • 16
  • 50
  • 86
  • 1
    @Zaffy http://stackoverflow.com/questions/1289061/best-way-to-use-php-to-encrypt-and-decrypt – Norse Oct 03 '12 at 20:16
  • 3
    You should not be encrypting passwords, but hashing them. Encryption is reversible, hashing (at least theoretically), isn't. – NullUserException Oct 03 '12 at 20:17
  • He specifically says you should hash and not encrypt passwords. You are leaving passwords open to decryption `$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");` – deizel. Oct 03 '12 at 20:17
  • Here's some reading for you: http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php – Madara's Ghost Oct 03 '12 at 20:18
  • 1
    @Mr.Alien SHA-1 is about to be replaced. And most security minded people will use hardware scalable algorithms like bcrypt or scrypt anyways. – NullUserException Oct 03 '12 at 20:20
  • http://codahale.com/how-to-safely-store-a-password/ (2 years ago) – deizel. Oct 03 '12 at 20:21
  • @NullUserException what's the best? and btw does it really matter for a small application for a simple use? – Mr. Alien Oct 03 '12 at 20:21
  • I would go with SHA-1 for applications that don't operate on sensitive data (banking accounts, medical data and etc). – tftd Oct 03 '12 at 20:23
  • 1
    Use BCrypt http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php – Tchoupi Oct 03 '12 at 20:45

1 Answers1

1

You should not encrypt the password. If someone is able to retrieve your users table, he could easily reverse all passwords:

$password = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($salt), base64_decode($row['password']), MCRYPT_MODE_CBC, md5(md5($salt)));

Better use a hash function that is specifically designed for hashing passwords.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844