ASP.NET中密码保护,MD5和SHA1算法的使用

你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在ASP.NET中实现加密非常容易。.NET SDK中提供了CookieAuthentication类,其中的HashPasswordForStoringInConfigFile方法可直接使用MD5和SHA1算法。例子如下:

1<br/>

file: encrypting.aspx

1<br/>
1@ Page language="c#" Codebehind="encrypting.cs" AutoEventWireup="false" Inherits="encrypting.encrypting" 
1<br/>
 1<html><head><br/>
 2<meta content="Microsoft Visual Studio 7.0" name="GENERATOR"/><br/>
 3<meta content="C#" name="CODE_LANGUAGE"/></head><br/>
 4<body><br/>
 5<br/>
 6<form method="post" runat="server"><br/>
 7<p> </p><br/>
 8<p><br/>
 9<asp:textbox id="TextBox1" runat="server"></asp:textbox><br/>
10<asp:button id="Button1" runat="server" text="encrypting"></asp:button></p><br/>
11<p>Encrypting Password(MD5):<br/>
12<asp:label id="MD5" runat="server"></asp:label></p><br/>
13</form><br/>
14<br/>
15</body></html>
1<br/>
1<br/>

file:encrypting.cs

1<br/>
1<br/>

namespace encrypting

1<br/>

{

1<br/>

using System;

1<br/>

using System.Collections;

1<br/>

using System.ComponentModel;

1<br/>

using System.Data;

1<br/>

using System.Drawing;

1<br/>

using System.Web;

1<br/>

using System.Web.SessionState;

1<br/>

using System.Web.UI;

1<br/>

using System.Web.UI.WebControls;

1<br/>

using System.Web.UI.HtmlControls;

1<br/>

using System.Web.Security;

1<br/>

///

1<summary><br/>   
2/// Summary description for encrypting.<br/>   
3/// </summary>
1<br/>

public class encrypting : System.Web.UI.Page

1<br/>

{

1<br/>

protected System.Web.UI.WebControls.Label MD5;

1<br/>

protected System.Web.UI.WebControls.Button Button1;

1<br/>

protected System.Web.UI.WebControls.TextBox TextBox1;

1<br/>
1<br/>

public encrypting()

1<br/>

{

1<br/>

Page.Init += new System.EventHandler(Page_Init);

1<br/>

}

1<br/>

protected void Page_Load(object sender, EventArgs e)

1<br/>

{

1<br/>

if (!IsPostBack)

1<br/>

{

1<br/>

//

1<br/>

// Evals true first time browser hits the page

1<br/>

//

1<br/>

}

1<br/>

}

1<br/>

protected void Page_Init(object sender, EventArgs e)

1<br/>

{

1<br/>

//

1<br/>

// CODEGEN: This call is required by the ASP+ Windows Form Designer.

1<br/>

//

1<br/>

InitializeComponent();

1<br/>

}

1<br/>

///

1<summary><br/>   
2/// Required method for Designer support - do not modify<br/>   
3/// the contents of this method with the code editor.<br/>   
4/// </summary>
1<br/>

private void InitializeComponent()

1<br/>

{

1<br/>

Button1.Click += new System.EventHandler (this.Button1_Click);

1<br/>

this.Load += new System.EventHandler (this.Page_Load);

1<br/>

}

1<br/>

public void Button1_Click (object sender, System.EventArgs e)

1<br/>

{

1<br/>

MD5.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"MD5");

1<br/>

//SHA1 use CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"SHA1");

1<br/>

}

1<br/>

}

1<br/>

}

1<br/>

注意:类CookieAuthentication的namespace是System.Web.Security。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus