地穴函数未返回正确的密码


Crypt Function Not Returning Correct Password

我有一个使用crypt和blowfish的简单登录系统。然而,就我的一生而言,我不明白为什么该函数没有生成存储在我的数据库中的注册密码。密码功能出了什么问题?

这是将密码注册到数据库中的时间。

function unique_md5() {
  mt_srand(microtime(true)*100000 + memory_get_usage(true));
  return md5(uniqid(mt_rand(), true));
}
if ($password == $password_again) {
$md5 = substr(unique_md5(), 0, 15);
$string = '$2a$07$' . $md5;
$password = trim($password);
$protected_password = crypt($password, $string);
//rest of code involved putting that $protected_password into database

登录页面代码

$password = 'password';
echo '$2a$07$4cf0aa3a82e8d78$$$$$$.M4dWdC3N7OF.hphzfyswwszM7RFJUfu';
//the echo below echos out the exact same thing as the echo above, but the if statement 
//recognizes it as not equal to
echo $registered_password = registered_password($mysqli, $username);
if ($password == crypt($password, $registered_password))
    {
    echo 'Working';
    } else {
    echo 'Not working';
}

您错误地使用了crypt函数。您需要将加密密码与crypt的结果进行比较,而不是将纯文本密码进行比较。

你的比较应该是这样的:

if ($encrypted_password_from_database == crypt($user_provided_password, $encrypted_password_from_database)) {
    // match
} else {
    // no match
}