简体   繁体   English

crypt函数以相同的salt,password和blowfish返回不同的结果

[英]crypt function returning different results with same salt,password and blowfish

I have started to work on making a login system and using the crypt function for the encoding of passwords. 我已经开始着手制作一个登录系统并使用crypt函数对密码进行编码。 My problem is that when I register the user with there user and password it all works and saves the username password and salt to the database. 我的问题是,当我使用那里的用户名和密码注册用户时,所有功能都可以正常工作并将用户名密码和盐保存到数据库中。 here is the code for the registration: 这是注册代码:

Note: this is only a test register page at the moment 注意:这只是目前的测试注册页面

$username=$_POST["username"];
$password=$_POST["password"];


$salt = substr(md5(microtime()),rand(0,26),15);
$hashedPass = crypt($password,'$2y$10$' . $salt);


$sql="INSERT INTO `users`(`id`, `username`, `password`, `salt`, `Perm_level`) VALUES (NULL,'$username','$hashedPass','$salt','test')";
$result=mysql_query($sql);

it encrypts it corrrect so a real example is the password test comes out as: $2y$10$d395985a2ca993f$$$$$$.k8lxPkUCenMKsOJ6V8tdO6Pl/Gl1/OW 它会正确加密,因此一个真实的例子是密码测试出来:$ 2y $ 10 $ d395985a2ca993f $$$$$$。k8lxPkUCenMKsOJ6V8tdO6Pl / Gl1 / OW

and its salt is for: 它的盐是:
d395985a2ca993f d395985a2ca993f

When I go to login though I pull the salt out of the database and try to re encrypt it in the same way but I get a different encrypted password... the salt is correct and so is the first part of the password so this part "$2y$10$d395985a2ca993f$$$$$$." 当我登录时,虽然我从数据库中取出了盐,然后尝试以相同的方式对其进行重新加密,但是我得到了一个不同的加密密码...盐是正确的,所以密码的第一部分也是正确的,所以这部分“ $ 2y $ 10 $ d395985a2ca993f $$$$$$。”

Here is the code below for the login: 这是用于登录的以下代码:

$sql="SELECT * FROM `users` WHERE username='$user'";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
$salt=$rows['salt'];
}

$hashedPass = crypt($password,'$2y$10$' . $salt);

$sql="SELECT * FROM `users`WHERE username='$user' AND `password` = '$hashedPass'";
$result=mysql_query($sql);

    if($result) {
        if(mysql_num_rows($result) == 1) {
            echo "Successful Login";

        }
    }

for the login page the salt is correct but here is the password once it has been hashed: $2y$10$d395985a2ca993f$$$$$$.ccy3PKl.TsG26FWJBFXKmpQ3wtk4AqC 对于登录页面,salt是正确的,但是一旦经过哈希处理,则使用以下密码:$ 2y $ 10 $ d395985a2ca993f $$$$$$。ccy3PKl.TsG26FWJBFXKmpQ3wtk4AqC

the first part up to the full stop is correct its just the second half is different 直到句号的第一部分是正确的,只是下半部分是不同的

I have tried to set the salt to a manual one so like abc or 123 just for tests on the logon and register pages but I still have the same error 我试图将盐设置为手动盐,例如abc或123,仅用于登录和注册页面上的测试,但是我仍然遇到相同的错误

When you put your values into the database, make sure you escape them. 将值放入数据库时​​,请确保将其转义。

$escapeUserName= mysql_real_escape_string($username);
$escapeHashedPass= mysql_real_escape_string($hashedPass);
$escapeSalt= mysql_real_escape_string($salt);

$sql="INSERT INTO `users`(`id`, `username`, `password`, `salt`, `Perm_level`)
      VALUES (NULL,'$escapeUserName','$escapeHashedPass','$escapeSalt','test')"; 

Otherwise special characters may not be inserted correctly, and you'll get the problem you experience. 否则可能无法正确插入特殊字符,并且会遇到问题。


Note that these mysql functions are being deprecated and you should use mysqli or PDO instead. 请注意,不建议使用这些mysql函数,而应改用mysqli或PDO。 If you did, and use parametrized queries, this would never have happened as they handle the escaping for you. 如果您这样做了,并且使用了参数化查询,那么这将永远不会发生,因为它们会为您处理转义。 This inherently makes them more secure. 这从本质上使它们更安全。 You should consider using the new functions before you get used to the old ones. 在习惯旧功能之前,您应该考虑使用新功能。

From your code I see you use blowfish hashing. 从您的代码中,我看到您使用河豚哈希。

when use blowfish hashing, your salt need be base64 encoded and after that you have to replace '+' with dot. 使用河豚散列时,您的盐必须使用base64编码,然后必须用点替换'+'。

And your salt should be 22 character long. 您的盐应该是22个字符长。

also, best practice is to use a PHP hashing package , like this one http://www.openwall.com/phpass/ 另外,最佳做法是使用PHP哈希包,例如http://www.openwall.com/phpass/

very few people can code a correct hashing function. 很少有人可以编写正确的哈希函数。 it requires very advanced and specialised knowledge. 它需要非常先进和专业的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM