专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »DotNet » md5sha1:ASP.NET中MD5和SHA1算法的使用 »正文

md5sha1:ASP.NET中MD5和SHA1算法的使用

来源: 发布时间:星期三, 2008年9月10日 浏览:74次 评论:0

  你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在ASP.NET中实现加密非常容易。.NET SDK中提供了CookieAuthentication类,其中的HashPasswordForStoringInConfigFile方法可直接使用MD5和SHA1算法。例子如下:<br>
  file: encrypting.aspx<br>
  <%@ Page language="c#" Codebehind="encrypting.cs" AutoEventWireup="false" Inherits="encrypting.encrypting" %><br>
  <html><head><br>
  <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"><br>
  <meta name="CODE_LANGUAGE" Content="C#"></head><br>
  <body><br>
  <br>
  <form method="post" runat="server"><br>
  <p> </p><br>
  <p><br>
  <asp:TextBox id=TextBox1 runat="server"></asp:TextBox><br>
  <asp:Button id=Button1 runat="server" Text="encrypting"></asp:Button></p><br>
  <p>Encrypting Password(MD5):<br>
  <asp:Label id=MD5 runat="server"></asp:Label></p><br>
  </form><br>
  <br>
  </body></html><br>
  <br>
  file:encrypting.cs<br>
  <br>
  namespace encrypting<br>
  {<br>
  using System;<br>
  using System.Collections;<br>
  using System.ComponentModel;<br>
  using System.Data;<br>
  using System.Drawing;<br>
  using System.Web;<br>
  using System.Web.SessionState;<br>
  using System.Web.UI;<br>
  using System.Web.UI.WebControls;<br>
  using System.Web.UI.HtmlControls;<br>
  using System.Web.Security;<br>
  /// <summary><br>
  /// Summary description for encrypting.<br>
  /// </summary><br>
  public class encrypting : System.Web.UI.Page<br>
  {<br>
  protected System.Web.UI.WebControls.Label MD5;<br>
  protected System.Web.UI.WebControls.Button Button1;<br>
  protected System.Web.UI.WebControls.TextBox TextBox1;<br>
  <br>
  public encrypting()<br>
  {<br>
  Page.Init += new System.EventHandler(Page_Init);<br>
  }<br>
  protected void Page_Load(object sender, EventArgs e)<br>
  {<br>
  if (!IsPostBack)<br>
  {<br>
  //<br>
  // Evals true first time browser hits the page<br>
  //<br>
  }<br>
  }<br>
  protected void Page_Init(object sender, EventArgs e)<br>
  {<br>
  //<br>
  // CODEGEN: This call is required by the ASP+ Windows Form Designer.<br>
  //<br>
  InitializeComponent();<br>
  }<br>
  /// <summary><br>
  /// Required method for Designer support - do not modify<br>
  /// the contents of this method with the code editor.<br>
  /// </summary><br>
  private void InitializeComponent()<br>
  {<br>
  Button1.Click += new System.EventHandler (this.Button1_Click);<br>
  this.Load += new System.EventHandler (this.Page_Load);<br>
  }<br>
  public void Button1_Click (object sender, System.EventArgs e)<br>
  {<br>
  MD5.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"MD5");<br>
  //SHA1 use CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"SHA1");<br>
  }<br>
  }<br>
  }<br>
  注意:类CookieAuthentication的namespace是System.Web.Security。
0
- 返回顶部
Copyright 2003-2008 www.Zxbc.cn All Rights Reserved
湘ICP备07001293号 E-mail:[email protected]
中国自学编程网版权所有,禁止转载!

相关文章

读者评论

  • 共0条 分0页

发表评论

  • 昵称:
  • 内容: