md5加密方法

MD5加密方法代码部分:
必须要有命名空间: System.Web.Security;
View Code
#region MD5加密算法



/// <summary>

/// MD5加密算法

/// </summary>

/// <paramname="str">字符串</param>

/// <paramname="code">加密方式,16或32</param>

/// <returns></returns>

public string MD5(string str, int code)

{

if(code == 16) //16位MD5加密(取32位加密的9~25字符)

{

return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToUpper().Substring(8, 16);

}

else//32位加密

{

return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"MD5").ToUpper();

}

}



#endregion



 

 

使用方式:

View Code
protectedvoid btnlogin_Click(objectsender, EventArgs e)//登陆click事件

{

stringpassword = MD5(txtpw,32).ToString();//调用加密方法,将密码加密,然后与数据库中的对比是否相同

}

页面源码:

<body>

<form id="form1" runat="server">

<div>

<asp:Label ID="labname" runat="server" Text="用户名"></asp:Label>

<asp:TextBox ID="txtname"runat="server"></asp:TextBox>

<asp:Label ID="labpw" runat="server" Text="密码"></asp:Label>

<asp:TextBox ID="txtpw" runat="server" TextMode="Password"></asp:TextBox>

<asp:Button ID="btnlogin" runat="server" Text="登录" onclick="btnlogin_Click" />

</div>

</form>
</body>



 

 
原文地址:https://www.cnblogs.com/fuge/p/2342567.html