对于C# 加密的认识,在C# 中可以很方便地进行MD5 和SHA1 加密,那么具体的实现步骤是什么呢?我们在C#中进行MD5 和SHA1 加密需要注意什么呢?那么下面我们就来看看具体的实现方法以及过程。
﹤%@ Import Namespace="System.Web.Security" %﹥
FormsAuthentication.HashPasswordForStoringInConfigFile
- 1.
- 2.
- 3.
只需要两步,***步引入名称空间(该名称空间也可以省略引用),第二步执行加密函数。
FormsAuthentication.HashPasswordForStoringInConfigFile 有两个参数:***个参数是要加密的字符串;第二个参数可选值有MD5 和SHA1,表示使用哪种加密方法。返回加密后的字符串,注意,返回后的字符串是大写。
C# 加密示例
﹤%@ Page Language="C#" %﹥
﹤%@ Import Namespace="System.Web.Security" %﹥
﹤script runat="server"﹥
void Enc(object sender, EventArgs e)
{
md5Text.Text =
FormsAuthentication.HashPasswordForStoringInConfigFile
(md5Text.Text, "MD5");
sha1Text.Text =
FormsAuthentication.HashPasswordForStoringInConfigFile
(sha1Text.Text, "SHA1");
}
﹤/script﹥
﹤!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥
﹤html xmlns="http://www.w3.org/1999/xhtml" ﹥
﹤head runat="server"﹥
﹤title﹥ASP.NET 中执行 MD5 和 SHA1 加密﹤/title﹥
﹤/head﹥
﹤body﹥
﹤form id="form1" runat="server"﹥
﹤div﹥
明文:﹤asp:TextBox ID="plainText" runat="server"﹥﹤/asp:TextBox﹥
﹤asp:Button ID="btn" runat="server" Text="加密" OnClick="Enc" /﹥
﹤/div﹥
﹤div﹥MD5 密文:﹤asp:TextBox ID="md5Text"
runat="server" Width="400"﹥﹤/asp:TextBox﹥﹤/div﹥
﹤div﹥SHA1 密文:﹤asp:TextBox ID="sha1Text"
runat="server" Width="400"﹥﹤/asp:TextBox﹥﹤/div﹥
﹤/form﹥
﹤/body﹥
﹤/html﹥
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
C# 加密的相关内容就向你介绍到这里,希望对你了解和学习C# 加密中的MD5和SHA1 加密有所帮助。
【编辑推荐】