asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

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


发布日期:2021年10月04日
 
ASP.NET中使用MD5和SHA1算法加密

你的主页或者你管理的网站有各种密码需要保护把密码直接放在数据库或者文件中存在不少安全隐患所以密码加密后存储是最常见的做法在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

上一篇:使用反射将业务对象绑定到 ASP.NET 窗体控件

下一篇:ASP.NET中为DataGrid添加合计字段