0

我有一个使用 crypt 和 blowfish 的简单登录系统。但是,对于我的生活,我无法弄清楚为什么该函数没有生成存储在我的数据库中的注册密码。crypt 函数有什么问题?

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

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';

}
4

1 回答 1

1

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

你的比较应该是这样的:

if ($encrypted_password_from_database == crypt($user_provided_password, $encrypted_password_from_database)) {
    // match
} else {
    // no match
}
于 2012-09-04T19:18:09.760 回答