我試圖在數據庫中存儲哈希密碼。這裏是我的代碼:Bcrypt驗證密碼
string passwords = textBox2.Text;
string salt = BCrypt.Net.BCrypt.GenerateSalt(12);
string hashPwd = BCrypt.Net.BCrypt.HashPassword(passwords, salt);
try
{
SQLiteCommand command = new SQLiteCommand();
connection.Open();
command.Connection = connection;
command.CommandText = ((@"INSERT INTO acc (UserName, Pass) VALUES ('" + textBox1.Text + "','" + hashPwd+ "');"));
command.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.ToString());
return;
}
登錄/驗證碼:
try
{
SQLiteDataAdapter sqlad = new SQLiteDataAdapter("SELECT COUNT(*) From acc WHERE Username = '" + textBox1.Text + "' AND Pass = '" + textBox2.Text + "' ", connection);
DataTable dt = new DataTable();
sqlad.Fill(dt);`
string userid = dt.Rows[0]["UserName"].ToString();
string password = dt.Rows[0]["Pass"].ToString();
bool flag = BCrypt.Net.BCrypt.Verify(textBox2.Text, password);
if (userid == textBox1.Text && flag == true)
{
Form2 frm = new Form2();
frm.Show();
}
else
{
MessageBox.Show("Invalid UserId or password");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
我無法驗證密碼,我得到的錯誤,你能幫助我嗎?還有一個問題,我應該在數據庫中儲存鹽嗎?
爲什麼在你的SELECT語句,你有'並通過= ' 「+ textBox2.Text +」''?數據庫中的密碼是散列的,大概'textBox2.Text'包含純文本,所以這找不到任何東西。您還要返回計數,而不是「用戶名」和「通過」列,因此這些將不在結果集中。最後,可能想要閱讀SQL注入。 – steve16351
如何將用戶輸入的密碼與數據庫中的哈希值進行比較? –
@LuisVito看看[這篇文章](https://crackstation.net/hashing-security.htm),尤其是[本節](https://crackstation.net/hashing-security.htm#properhashing) 。相關步驟在此處列出。請考慮閱讀完整的文章;這是一個很好的:-) – khlr