ASP.NET中MD5和SHA1密码保护算法的使用

作者:郁郁小蝎 来源:中国站长学院 时间:2007-08-24 09:18:00 

你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在ASP.NET中实现加密非常容易。.NET SDK中提供了CookieAuthentication类,其中的HashPasswordForStoringInConfigFile方法可直接使用MD5和SHA1算法。例子如下:

文件: 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> 


文件: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。

标签:asp.net,md5,密码保护
0
投稿

猜你喜欢

  • 解决Windows 7下安装Oracle 11g相关问题的方法

    2024-01-22 05:19:19
  • Python利用Redis计算经纬度距离案例

    2021-03-05 04:51:35
  • django session完成状态保持的方法

    2021-12-07 16:12:02
  • 一行代码给你的WordPress Blog添加下雪效果

    2008-12-14 09:43:00
  • python-for x in range的用法(注意要点、细节)

    2022-11-12 22:40:49
  • python装饰器原理源码示例分析

    2022-03-25 18:31:04
  • js实现固定区域内的不重叠随机圆

    2024-05-13 09:18:40
  • 匹配 IP 地址与域名的正则表达式

    2023-06-17 05:55:48
  • WxPython界面利用pubsub如何实现多线程控制

    2021-01-28 08:10:03
  • 用Vue编写抽象组件的方法

    2024-05-10 14:10:30
  • 如何通过Django使用本地css/js文件

    2022-04-28 22:04:33
  • javascript实现简单的可随机变色网页计算器示例

    2024-04-16 09:37:07
  • js将URL网址转为16进制加密与解密函数

    2024-02-26 13:37:30
  • 关于Python3爬虫利器Appium的安装步骤

    2022-06-04 15:30:59
  • Python 调用 Outlook 发送邮件过程解析

    2023-11-17 19:34:57
  • 如何恢复/修复MS SQL数据库的MDF文件

    2007-10-30 13:52:00
  • python飞机大战游戏实例讲解

    2021-12-07 14:43:26
  • Python enumerate函数遍历数据对象组合过程解析

    2023-09-22 02:51:06
  • 通过python爬虫赚钱的方法

    2023-04-27 11:48:17
  • Asp无组件上传进度条解决方案

    2010-04-24 16:01:00
  • asp之家 网络编程 m.aspxhome.com