duanchazhou6779 2012-08-28 23:59
浏览 65

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

I have started to work on making a login system and using the crypt function for the encoding of passwords. 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

and its salt is for:
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$$$$$$."

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

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

  • 写回答

2条回答 默认 最新

  • duanpi7107 2012-08-29 02:14
    关注

    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. 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘