encoding
protected void Button2_Click(object sender, EventArgs e)
{
try
{
string str = "update AdminLogin set password=@password where userid=@userid";
SqlCommand cmd = new SqlCommand(str, Db.GetConnection());
cmd.Parameters.AddWithValue("@userid", TextBox2.Text);
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedBytes = null;
UTF8Encoding encoder = new UTF8Encoding();
hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(TextBox3.Text));
SqlParameter sqp = new SqlParameter("@password", System.Data.SqlDbType.Binary, 16);
sqp.Value = hashedBytes;
cmd.Parameters.Add(sqp);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
Decoding
protected void Button1_Click1(object sender, EventArgs e)
{
try
{
cnn.Open();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlDataReader dr;
cmd.CommandText = "select * from AdminLogin where UserID=@userid and password=@pass";
cmd.Parameters.AddWithValue("userid", TextBox1.Text);
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedBytes = null;
UTF8Encoding encoder = new UTF8Encoding();
hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(TextBox2.Text));
SqlParameter sqp = new SqlParameter("@pass", SqlDbType.Binary, 16);
string strpa = hashedBytes.ToString();
sqp.Value = hashedBytes;
cmd.Parameters.Add(sqp);
cmd.Connection = cnn;
da.SelectCommand = cmd;
dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["name"] = dr["name"].ToString();
}
}
catch(Exception ex)
{
}
}