You don't have to reverse encrypted password to authenticate user.
First figure out which encryption you want to use. I use md5, which is not an encryption algorithm but more of Message digest. But pretty secure as far as user authication is needed.
Add user to database:
$query = "INSERT INTO user VALUES ('DummyUser',md5('DummyPassword'))";
And then for matching the password use:
$password = md5($password);// form vairable, convert it to equivelent of md5 hash
$query = "SELECT * FROM user WHERE username='”.$username.”' AND password='".$password."'";
Just to make sure that password is secure against brute force attack, ensure that minimum length for password is 6 and set the field size in db for password to 60.
OR you can use SSHA algorithm:
use HashPassword($password) before saving the password in database
//code to validate
$sql = "Select * FROM user Where username='"$username"'";
$result = mysql_query($sql);
if(!result){
die("Error executing sql: ".mysql_error();
}
$fields = mysql_fetch_assoc($result);
if(ValidatePassword($password,$fields["password"])){
//session start
}else{
//clear session
//relogin
}
public function HashPassword($password)
{
mt_srand((double)microtime()1000000);
$salt = mhash_keygen_s2k(MHASH_SHA1, $password, substr(pack('h', md5(mt_rand())), 0, 8), 4);
$hash = "{SSHA}".base64_encode(mhash(MHASH_SHA1, $password.$salt).$salt);
return $hash;
}
Source code to validate SSHA passwords...
//$password = user entered password
//$hash = password stored in database.
public function ValidatePassword($password, $hash)
{
$hash = base64_decode(substr($hash, 6));
$original_hash = substr($hash, 0, 20);
$salt = substr($hash, 20);
$new_hash = mhash(MHASH_SHA1, $password . $salt);
if (strcmp($original_hash, $new_hash) == 0)
return true;
else
return false;
}
... be sure to clear your session data .