当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  ASP.NET中使用MD5和SHA1算法加密

 ASP.NET中使用MD5和SHA1算法加密

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


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华