ASP.net中md5加密码的方法
程序员文章站
2023-11-25 13:20:34
<%@ page language="c#" contenttype="text/html"%> <%@&nbs...
<%@ page language="c#" contenttype="text/html"%>
<%@ import namespace="system"%>
<script language="c#" runat="server">
void page_load(object sender,eventargs e){
//获取要加密的字段,并转化为byte[]数组
byte[] data=system.text.encoding.unicode.getbytes(source.text.tochararray());
//建立加密服务
system.security.cryptography.md5 md5 = new system.security.cryptography.md5cryptoserviceprovider();
//加密byte[]数组
byte[] result= md5.computehash(data);
//将加密后的数组转化为字段
string sresult=system.text.encoding.unicode.getstring(result);
//显示出来
sha1_1.text="md5普通加密:"+sresult.tostring()+"<br/>";
//作为密码方式加密
string enpswdstr=system.web.security.formsauthentication.hashpasswordforstoringinconfigfile(source.text.tostring(),"md5");
//显示出来
sha1_2.text="md5密码加密:"+enpswdstr+"<br/>";
}
</script>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>测试</title>
</head>
<body>
<h3>sha1加密</h3>
<form runat="server">
<asp:label id="sha1_1" runat="server"></asp:label>
<asp:label id="sha1_2" runat="server"></asp:label>
<asp:textbox id="source" runat="server" textmode="singleline" text="test" autopostback="true" />
(回车)
</form>
</body>
</html>
<%@ import namespace="system"%>
<script language="c#" runat="server">
void page_load(object sender,eventargs e){
//获取要加密的字段,并转化为byte[]数组
byte[] data=system.text.encoding.unicode.getbytes(source.text.tochararray());
//建立加密服务
system.security.cryptography.md5 md5 = new system.security.cryptography.md5cryptoserviceprovider();
//加密byte[]数组
byte[] result= md5.computehash(data);
//将加密后的数组转化为字段
string sresult=system.text.encoding.unicode.getstring(result);
//显示出来
sha1_1.text="md5普通加密:"+sresult.tostring()+"<br/>";
//作为密码方式加密
string enpswdstr=system.web.security.formsauthentication.hashpasswordforstoringinconfigfile(source.text.tostring(),"md5");
//显示出来
sha1_2.text="md5密码加密:"+enpswdstr+"<br/>";
}
</script>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>测试</title>
</head>
<body>
<h3>sha1加密</h3>
<form runat="server">
<asp:label id="sha1_1" runat="server"></asp:label>
<asp:label id="sha1_2" runat="server"></asp:label>
<asp:textbox id="source" runat="server" textmode="singleline" text="test" autopostback="true" />
(回车)
</form>
</body>
</html>
上一篇: 程序中常用的种代码