asp.net中md5加密的方法

.net中已包含有md5加密的方法,如下:

<%@ 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);  
//顯示出來  
label1.Text="MD5普通加密:"+sResult.ToString()+"<br/>";  
//作爲密碼方式加密  
string EnPswdStr=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(source.Text.ToString(),"MD5");  
//顯示出來  
label2.Text="MD5密碼加密:"+EnPswdStr+"<br/>";  
}  
</script>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
<title>test</title>  
</head>  
<body>  
<h3>SHA1test</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>

from:http://www.codes51.com/article/detail_246.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章