ASP.NET中密碼保護,MD5和SHA1演算法的使用

來源:互聯網
上載者:User
asp.net|演算法 你的首頁或者你管理的網站有各種密碼需要保護,把密碼直接放在資料庫或者檔案中存在不少安全隱患,所以密碼加密後儲存是最常見的做法。在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">
<p> </p>
<p>
<asp:TextBox id=TextBox1 runat="server"></asp:TextBox>
<asp:Button id=Button1 runat="server" Text="encrypting"></asp:Button></p>
<p>Encrypting Password(MD5):
<asp:Label id=MD5 runat="server"></asp:Label></p>
     </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。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.