2016-11-19 63 views
0

我有一个用$ hash = password_hash($ accountpassword,PASSWORD_DEFAULT);登录的主页。 (http://php.net/manual/de/function.password-hash.phpmySQl用c登录#

密码保存为哈希以$ 2y $开头。

然后创建在C#登录:

//crypting the PW that user enter 
     string cryptedPassword = Crypter.Blowfish.Crypt(textBox_password.Text); 


     string user = textBox_username.Text; 
     string pass = cryptedPassword; 
     if (user == "" || pass == "") 
     { 
      MessageBox.Show("Empty Fields Detected ! Please fill up all the fields"); 
      return; 
     } 
     bool r = validate_login(user, pass); 
     if (r) 
      MessageBox.Show("Correct Login Credentials"); 
     else 
      MessageBox.Show("Incorrect Login Credentials"+cryptedPassword); 

我的验证方法:

private bool validate_login(string user, string pass) 
    { 
     db_connection(); 
     MySqlCommand cmd = new MySqlCommand(); 
     cmd.CommandText = "Select * from users where [email protected] and [email protected]"; 
     cmd.Parameters.AddWithValue("@user", user); 
     cmd.Parameters.AddWithValue("@pass", pass); 
     cmd.Connection = connect; 
     MySqlDataReader login = cmd.ExecuteReader(); 
     if (login.Read()) 
     { 
      connect.Close(); 
      return true; 
     } 
     else 
     { 
      connect.Close(); 
      return false; 
     } 
    } 

在Crypter.Blowfish.Crypt(textBox_password.Text)是拨错。我成为哈希以$ 2a开头$

任何人都可以帮助解决这个问题吗?

回答

1

当您使用Crypter.Blowfish.Crypt方法时,您需要指定第二个参数salt,如果没有提供,那么为您生成一个随机参数。显然你会在随后的调用中获得不同的随机值。

Alternativly你可以得到Crypter库要检查的哈希提供的密码:

if (Crypter.CheckPassword(password, cryptedPassword)) { 
    // Valid password code here 
    return true; 
} else { 
    // Invalid password code here 
    return false; 
} 

你显然需要读取数据库的加密的密码,但我相信这个方法将意味着你不” t需要为密码散列提供/存储salt。

HTH

+0

嗯php脚本不是我的工作。我怎样才能找出2参数? – Andreas

+0

请使用类似'Crypter.Blowfish.GenerateSalt(6)'的东西,并存储该值,或使用您自己选择的字符串(不太安全)。我已经更新了我的答案,以包含一种替代方法,该方法可以使您无需存储盐即可执行检查。 – Graeme