如何使用加密的(河豚)密码登录到现有用户帐户


How to login to an existing user account with an encrypted (blowfish) password

我在youtube上学习了如何使用加密blowfish功能加密用户密码的教程。我已经在我的注册脚本中正确地实现了它,它成功地注册了一个帐户并将加密的密码发送到数据库。我的问题,虽然,检索加密密码时,试图登录用户登录。当我尝试登录到一个现有用户时,它会下降到最后一个else语句,表示它不存在,这意味着无法识别散列密码。

加密密码功能代码:

public function encryptPass($password, $rounds = 11)
        {
            $salt = "";
            // creates array of capital letters A-Z & lowercase as well as #'s 0-9
            $saltChars = array_merge(range('A', 'Z'), range('a', 'z'), range(0,9));
            for($i = 0; $i < 22; $i++)
            {
                // randomize the array
                $salt .= $saltChars[array_rand($saltChars)];
            }
            return crypt($password, sprintf('$2y$%02d$', $rounds) . $salt);
        }

用于注册帐户的代码:

/// REGISTER ACCOUNT ///
if(isset($_POST['register']))
{
    // clean up the fields
    $username = mysql_real_escape_string(trim($_POST['username']));
    $emailid = mysql_real_escape_string(trim($_POST['emailid']));
    $password = mysql_real_escape_string(trim($_POST['password']));
    $confirmPassword = mysql_real_escape_string(trim($_POST['confirm_password']));
    if($password == $confirmPassword)
    {
        $iUe = $dbMan->ifUsernameExist($username);
        $iEe = $dbMan->ifEmailExist($emailid);
        // if username and email don't already exist, continue with registration
        if(!$iUe && !$iEe)
        {
            // encrypt the users password
            $hashedPassword = $dbMan->encryptPass($password);
            echo "$password <br> 'n";
            // register the account
            $register = $dbMan->UserRegister($username, $emailid, $hashedPassword);
            // if registration was succesful
            if($register)
            {
                 echo "<script>alert('Registration Successful')</script>";
            }
            else
            {
                echo "<script>alert('Registration Not Successful')</script>";
            }
        } 
        else 
        {
            echo "<script>alert(' That email or username already exists! ')</script>";
        }
    } 
    else 
    {
        echo "<script>alert(' Passwords do not match! ')</script>";
    }

}

登录代码:

/// LOGIN ACCOUNT /// 
if(isset($_POST['login']))
{   
    // 'convert' post variables to session variables
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['password'] = $_POST['password'];
    // clean em up, get rid of any white spaces or sql injection special chars
    $username = mysql_real_escape_string(trim($_SESSION['username']));
    $password = mysql_real_escape_string($dbMan->encryptPass(trim($_SESSION['password'])));
    echo "$password<br>'n";
    $user = $dbMan->Login($username, $password);
    // if theres an acccount with that username/pw in the db
    if ($user) 
    {
        // login successful
        header("location:index.php");
    } 
    else 
    {
        // Registration Failed
        echo "<script>alert(' The email or password do not match! ')</script>";
    }
}

dbManager代码:

<?php  
require_once 'dbConnect.php'; 
//session_start();

    class dbManager
    {   
        function __construct() 
        {  
            // connecting to database  
            $db = new dbConnect();  
        }  
        // destructor  
        function __destruct() 
        {  
        }
        public function UserRegister($username, $emailid, $password)
        {  
            $query = mysql_query("INSERT INTO users(username, emailid, password) values('".$username."','".$emailid."','".$password."')") or die(mysql_error());  
            return $query;  
        } 
        public function Login($username, $password)
        {  
            $query = mysql_query("SELECT * FROM users WHERE username = '".$username."' AND password = '".$password."'");  
            $user_data = mysql_fetch_array($query);  
            //print_r($user_data);  
            $num_rows = mysql_num_rows($query);  
            if ($num_rows == 1)   
            {  
                $_SESSION['login'] = true;  
                $_SESSION['uid'] = $user_data['id'];  
                $_SESSION['username'] = $user_data['username'];  
                $_SESSION['emailid'] = $user_data['emailid'];  
                return TRUE; 
            }  
            else  
            {  
                return FALSE;  
            }  

        }

        // check if username exists in db
        public function ifUsernameExist($username)
        {  
            $qr = mysql_query("SELECT * FROM users WHERE username = '".$username."'");  
            echo $row = mysql_num_rows($qr);  
            if($row > 0)
            {  
                return TRUE;
            } 
            else 
            {  
                return FALSE;  
            }  
        }
        // check if email exists in db
        public function ifEmailExist($emailid)
        {
            $qr = mysql_query("SELECT * FROM users WHERE emailid = '".$emailid."'");
            echo $row = mysql_num_rows($qr);
            if($row > 0)
            {
                return TRUE;    
            }
            else
            {
                return FALSE;
            }
        }
        // encrypt password 
        public function encryptPass($password, $rounds = 11)
        {
            $salt = "";
            // creates array of capital letters A-Z & lowercase as well as #'s 0-9
            $saltChars = array_merge(range('A', 'Z'), range('a', 'z'), range(0,9));
            for($i = 0; $i < 22; $i++)
            {
                // randomize the array
                $salt .= $saltChars[array_rand($saltChars)];
            }
            return crypt($password, sprintf('$2y$%02d$', $rounds) . $salt);
        }
    }  
?>  

注意:login和register '方法'都在同一个php文件中,包括表单标记。加密函数位于另一个名为dbManager的文件中。

希望我提供足够的信息,有人指出我在正确的方向。任何帮助都是感激的!

谢谢,Dev .

您需要传递您的明文密码进行加密,以便在数据库中进行比较。

改变
$password = trim(mysql_real_escape_string($_SESSION['password']));

$password = $dbMan->encryptPass(trim(mysql_real_escape_string($_SESSION['password'])));

在您的登录操作。

理想情况下,在INSERTSELECT上运行mysql_real_escape_string之前运行$dbMan->encryptPass

$password = mysql_real_escape_string($dbMan->encryptPass(trim($_SESSION['password'])));

对于加密和解密,盐必须相同,因为您使用的是array_rand,所以每次通过的盐都是不同的。你必须把盐储存在别的地方。如果你去掉盐或者将它设置为一个常量,它现在就可以工作了。