加密密码失效

Encrypted password becomes invalid

我在数据库中有一个加密密码,我正在尝试检索它:

型号:

function get_user($usr, $pwd)
     {
         $encriptKey = 'super-secret-key';
         $sql = "
           select * from user
           where username = '" . $usr . "' and
           password = '". $this->encrypt->decode($pwd,$encriptKey) . "'
         ";
         $query = $this->db->query($sql);
         return $query->num_rows();
     }

在控制器中我有:

$username = $this->input->post("txt_username");
$password = $this->input->post('txt_password');     

$usr_result = $this->account_model->get_user($username, $password);

if ($usr_result > 0) //active user record is present
    {
        //login
    }

为什么密码还是无效?

既然你问我,我也把这个作为答案提交。

首先要注意的是,通常如果您要在数据库中存储一个秘密值,稍后检查是否有匹配它的方法是存储加密并比较一个普通的值以与您存储的内容相同的方式加密。我这样说是因为您似乎正在尝试解码作为参数接收到的内容,并将其与 table.

中的内容进行比较

此外,由于这是关于密码的,一般来说,散列值而不是加密它们是一种更好、更安全的方法。您还可以使完全相同的密码不会以相同的方式被散列两次,这会增加另一层安全性。

我认为在 PHP 中复制和粘贴散列密码的好方法对我来说不是一个好主意,所以我将仅参考一些其他问题,您可以在 [=26] 的此页面上找到=]侧边栏:

  • Best way to use PHP to encrypt and decrypt passwords?
  • How do you use bcrypt for hashing passwords in PHP?

这样试试

在控制器中

$username = $this->input->post("txt_username");
$password = $this->input->post('txt_password');     

$usr_result = $this->account_model->get_user($username, $password);

if ($usr_result == FALSE) 
{
    echo "Invalid User";
}
else{
    echo "Valid User";
}

模型中

function get_user($usr, $pwd)
{
    $encriptKey = 'super-secret-key';
    $password = $this->encrypt->decode($pwd,$encriptKey);
    $sql = "SELECT * FROM user WHERE username = '$usr' AND  password = '$password' ";
    $query = $this->db->query($sql);

    $count = count($query);

    if (empty($count) || $count > 1) {
        return FALSE;
    }
    else{
        return TRUE;
    }

}