web程序语言设计 c# asp.net中使用md5加密

  1. 在cs文件中引入两个命名空间,定义名为Md5Hash的方法
using System.Security.Cryptography;
using System.Text;
private static string Md5Hash(string input)
    {
        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        return sBuilder.ToString();
    }
  1. 使用
string password = xxxxxxx;
Md5Hash(password);
  1. 使用示例
//-------------------------------------定义Md5Hash()
private static string Md5Hash(string input)
{
        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        return sBuilder.ToString();
}
protected void btnLogin_Click(object sender, EventArgs e)
    {
        string veri = verify.Value;
        if ( veri == "1")
        {
            var username = Request["username"];
            var password = Request["password"];
            //---------------------使用Md5Hash()返回md5
            password = Md5Hash(password);
            adminTableAdapter st = new adminTableAdapter();
            DataTable dt = st.AdminLogin(username);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["密码"].ToString() == password)
                {
                    Session["username"] = username;
                    Response.Redirect("../manage/admin_manager.aspx");
                }
                else
                {
                    Response.Write("");
                }
            }
            else
            {
                Response.Write("");
            }
        }
        else
        {
            Response.Write("");
        }
        
    }

你可能感兴趣的:(web程序语言设计 c# asp.net中使用md5加密)