我有一个简单的登录窗口应用程序。我已经在本地主机中成功创建了一个 SQL 数据库,其中包含用户名、密码(哈希)、Salt 和电子邮件等用户信息,并且我还成功创建了一个用户注册表单,新用户可以在其中输入他们的详细信息,这些详细信息将被添加到数据库,它工作正常。
但是我无法根据数据库(localhost)中包含用户数据的行中的存储值验证用户名和密码以提供访问权限。
我已经能够编写散列、加盐和验证方法以供进一步使用。
HashSalt.cs 类
// --- constructor to initialize num_of_iterations
public HashSalt(int numOfIterations)
{
num_of_iterations = numOfIterations;
}
// --- Generate Salt ---
public string generateSalt()
{
var salt = new byte[32];
var randomProvider = new RNGCryptoServiceProvider();
randomProvider.GetBytes(salt);
return Convert.ToBase64String(salt); // returns salt as a string
}
// --- converts salt string into byte[]
public byte[] saltToByte(string salt)
{
var byteSalt = Convert.FromBase64String(salt);
return byteSalt;
}
// --- Generate hash of(pass+salt) ---
public string generateHash(string password, byte[] salt)
{
var rfc2898 = new Rfc2898DeriveBytes(password, salt, num_of_iterations);
var Password = rfc2898.GetBytes(32); // gives 32 byte encoded password
return Convert.ToBase64String(Password); // returns hash
}
// --- Authenticate User ---
public bool AuthenticateUser(string enteredPassword, string storedHash, string storedSalt)
{
var saltBytes = Convert.FromBase64String(storedSalt);
var rfc2898DeriveBytes = new Rfc2898DeriveBytes(enteredPassword, saltBytes, 10000);
return Convert.ToBase64String(rfc2898DeriveBytes.GetBytes(256)) == storedHash;
}
}
}
这是我 用于登录表单的 Login.cs
private void btnSignin_Click(object sender, EventArgs e)
{
string enteredPass = txtLoginPassword.Text;
DbHandler db = new DbHandler();
MySqlDataAdapter adapter = new MySqlDataAdapter();
DataTable table = new DataTable();
MySqlCommand cmd = new MySqlCommand("SELECT password, salt FROM student WHERE indexno=@index;", db.getConnection());
db.openConnection(); // open connection
cmd.Parameters.Add("@index", MySqlDbType.VarChar).Value = txtLoginusername.Text;
adapter.SelectCommand = cmd;
adapter.Fill(table);
if (table.Rows.Count > 0)
{
string pass = table.Rows[0][0].ToString();
string salt = table.Rows[0][1].ToString();
string newPass = hashSalt.generateHash(enteredPass, hashSalt.saltToByte(salt));
if (hashSalt.authenticateUser(enteredPass, pass, salt))
{
// MessageBox.Show("correct ='" + pass + "'\nEntered pass ='" + hashSalt.authenticateUser(enteredPass, pass, salt) + "'");
// --- form Records obj
Records gotoRecords = new Records();
gotoRecords.Show(); // goto Records
this.Hide();
}
else
{
string message = "User name & Password did not Match!?";
string title = "Attention!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Warning);
}
}
else
{
string message = "User name NotFound!?";
string title = "Attention!";
MessageBoxButtons buttons = MessageBoxButtons.OK;
DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Warning);
}
//this.Close();
db.openConnection(); // close connection