I have the following codes to check md5 encrypted password against user input password:
UserDAO userDAO = new UserDAO();
// encrypt the input password
MD5 md5 = new MD5CryptoServiceProvider();
UTF8Encoding encoder = new UTF8Encoding();
Byte[] encryptedPassword;
encryptedPassword = md5.ComputeHash(encoder.GetBytes(TxtBoxPassword.Text));
// get information for this username and begin checking authentication
DataTable data = userDAO.GetUserInformation(TxtBoxUsername.Text);
if (data.Rows.Count == 0)
{
LblError.Text = "Wrong username!";
return;
}
Byte[] password = (Byte[])data.Rows[0]["Password"];
if (!Convert.ToBase64String(password).Equals(Convert.ToBase64String(encryptedPassword)))
{
LblError.Text = "Wrong password!";
return;
}
The problem is I can run this code just fine on my computer (admin/123456 validated correctly) whereas when I publish my website to a server, the check always return “wrong password”? What gives?
Not sure why yours isn’t working, but when I wrote the SHA512 implementation below I had some issues with the hash. It doesn’t output like you would normally see it displayed for humans. For this reason your data type should be binary in the database. Also here is the implementation I use (with the salt changed) that uses SHA512. Using ByteArrayToHexString puts it in a human recognizable format. Then you can use a varchar in the database.