MD5加密 +防sql注入

   public static string GetMD5(string myString)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
            byte[] targetData = md5.ComputeHash(fromData);
            string byte2String = null;

            for (int i = 0; i < targetData.Length; i++)
            {
                byte2String += targetData[i].ToString("x");
            }

            return byte2String;
        }

            string sqlstr = string.Format("select * from userinfo where UserName=@UserName and pwd=@Pwd");
            SqlCommand com = new SqlCommand(sqlstr,conn);
            Pwd = GetMD5(Pwd);
            com.Parameters.AddWithValue("@UserName", UserName); //' 设定参数 @c1 的值。
            com.Parameters.AddWithValue("@Pwd", Pwd);// ' 设定参数 @c2 的值。
            SqlDataReader reader=  com.ExecuteReader();
            if (reader.Read()==true)
            {
            MessageBox.Show("登录成功!");
            }

猜你喜欢

转载自blog.csdn.net/qq_42899641/article/details/86573016
今日推荐