设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2149|回复: 3
打印 上一主题 下一主题

hg:

[复制链接]
跳转到指定楼层
1#
发表于 2002-10-15 20:43:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
ASP.NET中密码保护,MD5和SHA1算法的使用
你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在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>&nbsp;</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):
<aspabel id=MD5 runat="server"></aspabel></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。   

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2002-10-15 21:11:00 | 只看该作者
又是微軟的,俺不懂,俺現在什麼加密都不用,俺用SSH和PGP。
3#
 楼主| 发表于 2002-10-15 21:31:00 | 只看该作者
厉害!!够用了!!
4#
 楼主| 发表于 2002-10-15 21:32:00 | 只看该作者
还有这是asp.net的加密方式!你应该会!!你的api和cui怎么样??
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2025-4-7 18:09 , Processed in 0.103196 second(s), 27 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表