I am trying to make a login and register for my website. I have used hash to encrypt the password but im struggling to log back in. The code for the login page is below
This is what ive used to encrypt the password
public string ToSHA2569(string value)
{
SHA256 sha256 = SHA256.Create();
byte[] hashData = sha256.ComputeHash(Encoding.Default.GetBytes(value));
StringBuilder returnValue = new StringBuilder();
for (int i = 0; i < hashData.Length; i++)
{
returnValue.Append(hashData[i].ToString());
}
return returnValue.ToString();
}
This is my register page
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection sqlcon = new SqlConnection(connectionString))
{
sqlcon.Open();
SqlCommand cmd = new SqlCommand("UserRegister", sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
cmd.Parameters.AddWithValue("@Password", ToSHA2569(txtPassword.Text.Trim()));
cmd.Parameters.AddWithValue("Created", DateTime.Now);
cmd.ExecuteNonQuery();
lblMessage.Text = "You have registered succussfully";
}
}
catch (Exception ex)
{
lblWrong.Text = "Something went wrong please try again later";
}
}
}
This is my login page i think there is something wrong here. Is it easier to use an encryption key instead of hash?
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection sqlcon = new SqlConnection(connectionString))
{
sqlcon.Open();
string checkPasswordQuery = "select Password from [dbo.Register] where Username ='" + ToSHA2569(txtEmail.Text) + "'";
SqlCommand passcom = new SqlCommand(checkPasswordQuery, sqlcon);
if (txtPassword.Text == ToSHA2569(txtPassword.Text))
{
Response.Redirect("default.aspx");
}
else
{
Response.Write("Password is not correct");
}
}
}
catch
{
lblWrong.Text = "Something went wrong please try again later";
}
}