【发布时间】:2017-06-05 12:08:25
【问题描述】:
请帮助检查用户身份验证密码。当我创建用户时,我使用 hash + salt 来加密密码,然后再存储到数据库中。我希望用户能够使用注册的密码登录,我是哈希新手。
HTML
<asp:TextBox ID="txtUsername" runat="server" Width="267px" TextMode="Password"></asp:TextBox>
<asp:TextBox ID="txtPassword" runat="server" Width="267px" TextMode="Password"></asp:TextBox>
<asp:Button ID="Login" runat="server" Text="Login" onclick="Login_Click"
Width="111px" />
使用的哈希函数
public String CreatedSalt(int size)
{
var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
var buff = new byte[size];
rng.GetBytes(buff);
return Convert.ToBase64String(buff);
}
public String GenerateSHA256Hash(String input, String salt)
{
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
System.Security.Cryptography.SHA256Managed sha256hashstring = new System.Security.Cryptography.SHA256Managed();
byte[] hash = sha256hashstring.ComputeHash(bytes);
return Convert.ToBase64String(hash);
}
登录.cs
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["MyConn"].ConnectionString);
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT AdminID FROM [User] WHERE StaffEmail = '" + txtUsername.Text + "' AND StaffPassword ='" + txtPassword.Text + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Session["AdminID"] = dr["AdminID"].ToString();
Response.Redirect("AppointmentMenu.aspx");
}
Response.Write("<script>alert('Please check your Username or Password')</script>");
【问题讨论】:
-
当用户登录时,您对登录密码进行哈希处理,并将其与数据库中的哈希密码进行比较。如果匹配,则 authenticate = true。
-
你需要保留你用来散列每个密码的盐。
-
仅使用散列函数是不够的,仅添加盐对提高安全性无济于事。而是使用随机盐在 HMAC 上迭代大约 100 毫秒,然后将盐与哈希一起保存。使用
PBKDF2(又名Rfc2898DeriveBytes)、password_hash/password_verify、Bcrypt等函数和类似函数。关键是让攻击者花费大量时间通过蛮力寻找密码。保护您的用户很重要,请使用安全的密码方法。 -
所以,如果我使用“随机”盐来存储密码,那么,在以后的某个时间,用户会尝试登录 - 我如何以如下方式散列新的登录尝试将其与存储的散列密码进行比较?由于我不再拥有我用来存储密码的盐......
标签: c# authentication hash salt