C# 加密-MD5 和 SHA1
2009-12-25 23:20
302 查看
在 ASP.NET 中可以非常方便地执行 MD5
或 SHA1 加密。
<%@ Import Namespace="System.Web.Security" %>
FormsAuthentication.HashPasswordForStoringInConfigFile
只需要两步,第一步引入名称空间
(该名称空间也可以省略引用),第二步执行加密函数。
FormsAuthentication.HashPasswordForStoringInConfigFile 有两个参数:第一个参数是要加密的字符串;第二个参数可选值有 MD5
和 SHA1
,表示使用哪种加密方法。返回加密后的字符串,注意,返回后的字符串是大写
。
示例
<%@ 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>
或 SHA1 加密。
<%@ Import Namespace="System.Web.Security" %>
FormsAuthentication.HashPasswordForStoringInConfigFile
只需要两步,第一步引入名称空间
(该名称空间也可以省略引用),第二步执行加密函数。
FormsAuthentication.HashPasswordForStoringInConfigFile 有两个参数:第一个参数是要加密的字符串;第二个参数可选值有 MD5
和 SHA1
,表示使用哪种加密方法。返回加密后的字符串,注意,返回后的字符串是大写
。
示例
<%@ 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>
相关文章推荐
- C#:Md5和Sha1两种加密方式
- C# 加密-MD5 和 SHA1
- C#_MD5与SHA1加密
- C#中 MD5和SHA1加密代码
- ASP.NET with C#使用md5,sha1加密初探
- C# 加密-MD5 和 SHA1
- 浅谈C#加密中MD5和SHA1加密实现
- 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解
- 浅谈C#中Md5和Sha1两种加密方式
- 使用Apache digest可以做md5 SHA1 BASE64加密
- C#中使用DES HMACSHA1 SHA1 RC2 MD5 进行字符串加密的例程
- 项目里面常用的加密方式base64 AES MD5 sha1
- javascript实现base64 md5 sha1 密码加密
- C#实现的MD5加密功能与用法示例
- 常见的数据传输加密方法MD5和SHA1
- C# MD5 32位加密 UTF-8编码
- iOS开发之算法加密md5,sha1,AES,base64
- c# MD5及盐值加密
- 用SHA1或MD5 算法加密数据(示例:对用户身份验证的简单实现)
- C# MD5-16位加密实例,32位加密实例(两种方法)