熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .NET編程 

  你的主頁或者你管理的網站有各種密碼需要保護把密碼直接放在數據庫或者文件中存在不少安全隱患所以密碼加密後存儲是最常見的做法在ASPNET中實現加密非常容易

  NET SDK中提供了CookieAuthentication類其中的HashPasswordForStoringInConfigFile方法可直接使用MD和SHA算法例子如下

  file: encryptingaspx
<%@ Page language=c# Codebehind=encryptingcs AutoEventWireup=false Inherits=encryptingencrypting %>
<html><head>
<meta name=GENERATOR Content=Microsoft Visual Studio
<meta name=CODE_LANGUAGE Content=C#></head>
<body>

<form method=post runat=server


<asp:TextBox id=TextBox runat=server></asp:TextBox>
<asp:Button id=Button runat=server Text=encrypting></asp:Button>
Encrypting Password(MD):
<asp:Label id=MD runat=server></asp:Label>
</form>

</body></html>

file:encryptingcs

namespace encrypting
{
using System;
using SystemCollections;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemWeb;
using SystemWebSessionState;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIHtmlControls;
using SystemWebSecurity;
/// <summary>
/// Summary description for encrypting
/// </summary>
public class encrypting : SystemWebUIPage
{
 protected SystemWebUIWebControlsLabel MD;
 protected SystemWebUIWebControlsButton Button;
 protected SystemWebUIWebControlsTextBox TextBox;

 public encrypting()
 {
  PageInit += new SystemEventHandler(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()
 {
  ButtonClick += new SystemEventHandler (thisButton_Click);
  thisLoad += new SystemEventHandler (thisPage_Load);
 }
 public void Button_Click (object sender SystemEventArgs e)
 {
  MDText = CookieAuthenticationHashPasswordForStoringInConfigFile(TextBoxTextMD);
  //SHA use CookieAuthenticationHashPasswordForStoringInConfigFile(TextBoxTextSHA);
 }
}
}

  注意類CookieAuthentication的namespace是SystemWebSecurity


From:http://tw.wingwit.com/Article/program/net/201311/13925.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.