您现在的位置是:首页-> 米鼠技术 ->ASP.NET中MD5和SHA1算法的使用

ASP.NET中MD5和SHA1算法的使用

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


热点文章
最新项目
相关文章 最新文章