Click here to Skip to main content
15,311,104 members
Please Sign up or sign in to vote.
2.09/5 (3 votes)
See more:
I am first using md5 encrpt one string and saving binary in database ..
how to retrieve the binary value using
Decrypt MD5 in ASP.Net?
Posted
Updated 11-Jun-21 23:02pm
v2

   
Comments
uspatel 13-Jan-12 0:34am
   
5! good article link...
Tech Code Freak 13-Jan-12 0:51am
   
5up! Great link.
encoding
C#
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
C#
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);

            //string result = cmd.ExecuteScalar().ToString();
            //cmd.Parameters.AddWithValue("pass", TextBox2.Text);
             cmd.Connection = cnn;
            da.SelectCommand = cmd;
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Session["name"] = dr["name"].ToString();
             }
}
catch(Exception ex)
{
}
}
   
v2
Comments
NandaKumer 13-Jan-12 1:25am
   
good one
uspatel 13-Jan-12 5:48am
   
thanks, you can rate it......

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900