|
技术资料 > .Net专区 > Asp.Net开发 : ASP.NET中密码保护,MD5和SHA1算法的使用 |
ASP.NET中密码保护,MD5和SHA1算法的使用 March 25,2004 |
你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在ASP.NET中实现加密非常容易。.NET SDK中提供了CookieAuthentication类,其中的HashPasswordForStoringInConfigFile方法可直接使用MD5和SHA1算法。例子如下:<br>
file: encrypting.aspx<br>
<%@ Page language="c#" Codebehind="encrypting.cs" AutoEventWireup="false" Inherits="encrypting.encrypting" %><br>
<html><head><br>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"><br>
<meta name="CODE_LANGUAGE" Content="C#"></head><br>
<body><br>
<br>
<form method="post" runat="server"><br>
<p> </p><br>
<p><br>
<asp:TextBox id=TextBox1 runat="server"></asp:TextBox><br>
<asp:Button id=Button1 runat="server" Text="encrypting"></asp:Button></p><br>
<p>Encrypting Password(MD5):<br>
<asp:Label id=MD5 runat="server"></asp:Label></p><br>
</form><br>
<br>
</body></html><br>
<br>
file:encrypting.cs<br>
<br>
namespace encrypting<br>
{<br>
using System;<br>
using System.Collections;<br>
using System.ComponentModel;<br>
using System.Data;<br>
using System.Drawing;<br>
using System.Web;<br>
using System.Web.SessionState;<br>
using System.Web.UI;<br>
using System.Web.UI.WebControls;<br>
using System.Web.UI.HtmlControls;<br>
using System.Web.Security;<br>
/// <summary><br>
/// Summary description for encrypting.<br>
/// </summary><br>
public class encrypting : System.Web.UI.Page<br>
{<br>
protected System.Web.UI.WebControls.Label MD5;<br>
protected System.Web.UI.WebControls.Button Button1;<br>
protected System.Web.UI.WebControls.TextBox TextBox1;<br>
<br>
public encrypting()<br>
{<br>
Page.Init += new System.EventHandler(Page_Init);<br>
}<br>
protected void Page_Load(object sender, EventArgs e)<br>
{<br>
if (!IsPostBack)<br>
{<br>
//<br>
// Evals true first time browser hits the page<br>
//<br>
}<br>
}<br>
protected void Page_Init(object sender, EventArgs e)<br>
{<br>
//<br>
// CODEGEN: This call is required by the ASP+ Windows Form Designer.<br>
//<br>
InitializeComponent();<br>
}<br>
/// <summary><br>
/// Required method for Designer support - do not modify<br>
/// the contents of this method with the code editor.<br>
/// </summary><br>
private void InitializeComponent()<br>
{<br>
Button1.Click += new System.EventHandler (this.Button1_Click);<br>
this.Load += new System.EventHandler (this.Page_Load);<br>
}<br>
public void Button1_Click (object sender, System.EventArgs e)<br>
{<br>
MD5.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"MD5");<br>
//SHA1 use CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"SHA1");<br>
}<br>
}<br>
}<br>
注意:类CookieAuthentication的namespace是System.Web.Security。
|
|
Copyright © 2001-2008 Shenzhen Hiblue Software Team All rights reserved