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

2018-11-25  本文已影响0人  jjjkkkhhhggg
  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("<script>alert('密码错误')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('用户不存在')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('请拖动滑块以完成验证')</script>");
        }
        
    }
上一篇下一篇

猜你喜欢

热点阅读