,ASP.net   工具软件   办公软件   操作系统   网络安全   设计在线   程序开发   教程宝典   软件下载   软件论坛,ASP.net
您的位置:软件 > 开发者网络 > 微软开发专栏 > Visual Studio.net专栏 > ASP.net > 正文
ASP.NET中使用MD5和SHA1算法加密
[文章信息]
作者:iiboy
时间:2005-04-07
出处:BLOG
责任编辑:方舟
[文章导读]
你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患
advertisement
热点推荐
· 17日精选 Longhorn系统详细解析(下)
· 如何消除Windows开机错误提示
· PHP开发中文件操作疑难问答
· 腾讯TM2005beta1最新技巧四则
· 体验V5E变幻万千的地形编辑器
[正文]
  你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在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">


<asp:TextBox id=TextBox1 runat="server"></asp:TextBox>
<asp:Button id=Button1 runat="server" Text="encrypting"></asp:Button>
Encrypting Password(MD5):
<asp:Label id=MD5 runat="server"></asp:Label>
</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。

发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
,ASP.net相关内容,ASP.net焦点新闻
  • ASP.NET技巧:为Blog打造个性日历
  • ASP.NET中实现MSN通知消息功能
  • GridView根据值的变化改变行列样式
  • 调试ASP.NET应用程序的方法和技巧
  • 优化ASP.NET应用程序性能研究与探讨
  • FVD刺激高清碟机加速商业化 抢占商机最重要
  • 3家搜索引擎集体诉讼8848 吕春维未敢出席
  • 杨元庆:没有准备不会获批的备用方案
  • 军队信息化诞生新领域 电子军务呼之欲出
  • 世界经济论坛公布信息化程度全球最新排名
  • 2004政务绩效评估:政府门户尚处于发展阶段
  • 甲骨文出资5.15亿美元 意图收购RetekInc
  • 技术并购:帮你突破传统增长的“天花板”
  • ,ASP.netAdvertisement

    天极无线
    经典名曲 华语流行 欧美风情 人声音效 原声大碟
    你好周杰伦-安又琪
    直线-张韶涵
    天边-张韶涵
    看我72变-蔡依林
    乱世佳人-Twins
    全世界下雨-张娜拉
    我们的爱-飞儿乐队
    分手快乐-梁静茹
    爱在西元前-周杰伦
    三万英尺-迪克牛仔
    爱如潮水-张信哲
    爱情宣言-齐秦
    寻找玛依拉-刀郎
    驼铃-刀郎
    老鼠爱大米-杨臣刚
    功夫主题曲
    仙剑主题曲杀破狼
    天下无贼片尾曲
    冲动的惩罚-刀郎
    情人-刀郎
    爱情-莫文蔚
    挪威的森林-伍佰
    奔跑 - 羽泉
    因为爱 - 羽泉
      ·时尚女人圈
    ·经典影视专集
    ·美女秀场
    ·美丽大自然专题
    ·流行金曲
    ·科幻电影特区
    | 最新推荐
    ·你好周杰伦
    ·杀破狼
    ·寻找玛依拉
    ·孤单公园
    ·男人海洋
    | 魔术铃声
    ·少女丰胸
    ·少妇丰胸
    ·减肥魔铃
    ·增高魔铃
    ·美容魔铃

    今日报价查询 更多报价