我想创建一个登录表单,密码中的字段区分大小写,如果我的密码是“PassWord”,它只接受关键字“PassWord”而不接受“password”或“PASSWORD”关键字等。我想要它是一个字符敏感谢谢请帮助我我是一个新的程序员使用数据库MS访问谢谢这是我的代码
Private Sub btnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLog.Click
Try
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\NIASecurity.accdb")
Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Username] FROM [Security] WHERE [Username] = @User and Password =@Pass ", con)
cmd.Parameters.AddWithValue("@User", txtUser.Text)
cmd.Parameters.AddWithValue("@Pass", txtPass.Text)
con.Open()
Dim sdr As OleDbDataReader = cmd.ExecuteReader()
If sdr.Read Then
If txtPass.Text = sdr(0) Then
MessageBox.Show("Welcome")
Dim win As New frmAdd
win.MdiParent = frmMDI
win.Show()
Me.Close()
Else
MsgBox("Invalid name or password!")
End If
End If
Catch ex As Exception
MessageBox.Show("Invalid name or password!")
End Try
End Sub
答案 0 :(得分:0)
我可以说你在db中保存密码,你必须使用哈希Algorithm
保存密码数据,例如MD5
或SHA1
当用户键入用于登录的密码时,您键入为密码键入的字符串,并将此字符串与保存在db
public static void HashPassword(string Password, out string Salt, out string Hash)
{
System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed();
Random rnd = new Random();
byte[] s = new byte[20];
rnd.NextBytes(s);
Salt = Convert.ToBase64String(s);
System.Text.UTF8Encoding u = new UTF8Encoding();
byte[] pass = u.GetBytes(Password);
byte[] all = new byte[pass.Length + s.Length];
Array.Copy(pass, all, pass.Length);
Array.Copy(s, 0, all, pass.Length, s.Length);
Byte[] H = sha.ComputeHash(all);
Hash = Convert.ToBase64String(H);
}
public bool IsPasswordCorrect(string Password, string Salt, string Hash)
{
System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed();
byte[] s = Convert.FromBase64String(Salt);
System.Text.UTF8Encoding u = new UTF8Encoding();
byte[] pass = u.GetBytes(Password);
byte[] all = new byte[pass.Length + s.Length];
Array.Copy(pass, all, pass.Length);
Array.Copy(s, 0, all, pass.Length, s.Length);
Byte[] H = sha.ComputeHash(all);
return (Hash == Convert.ToBase64String(H));
}
现在你必须使用HashPassword
方法为哈希提供一个盐,并为每个用户将哈希和盐保存到数据库。
想要检查密码时使用IsPasswordcorrect
方法