MD5,SHA1在C#中的应用 

下文源代码实现了 MD5,SHA1 算法的转换
如需转载,请注册出处
原文作者:Jaron
网址:http://www.jiangdu.net
2003.01.01

Convert.ASPX
=====================
<%@ Page language="c#" Codebehind="md.aspx.cs" AutoEventWireup="false" Inherits="jiangdu.net.md" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>md</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="md" method="post" runat="server">
   <asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 27px; POSITION: absolute; TOP: 81px" runat="server" DESIGNTIMEDRAGDROP="64">EnterPassword</asp:Label>
   <asp:TextBox id="Password" runat="server" style="Z-INDEX: 102; LEFT: 134px; POSITION: absolute; TOP: 80px"></asp:TextBox>
   <asp:Label id="Label3" runat="server" style="Z-INDEX: 103; LEFT: 30px; POSITION: absolute; TOP: 112px">SHA1 Resault :</asp:Label>
   <asp:Label id="txtMD5" runat="server" style="Z-INDEX: 104; LEFT: 136px; POSITION: absolute; TOP: 139px"></asp:Label>
   <asp:Label id="txtSHA1" runat="server" style="Z-INDEX: 105; LEFT: 135px; POSITION: absolute; TOP: 113px"></asp:Label>
   <asp:Label id="Label2" runat="server" style="Z-INDEX: 106; LEFT: 29px; POSITION: absolute; TOP: 139px">MD5 Resault :</asp:Label>
   <asp:Button id="Button1" runat="server" Text="Button" style="Z-INDEX: 107; LEFT: 305px; POSITION: absolute; TOP: 80px"></asp:Button>
  </form>
 </body>
</HTML>

Convert.aspx.cs
============

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;

namespace jiangdu.net
{
 /// <summary>
 /// md 的摘要说明。
 /// </summary>
 public class md : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox Password;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.Label txtSHA1;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.Label txtMD5;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   Security cSecurity = new Security(); //定义类
   txtMD5.Text=cSecurity.EncryptPassword(Password.Text,"MD5").ToString().ToLower();
   txtSHA1.Text=cSecurity.EncryptPassword(Password.Text,"SHA1").ToString().ToLower();
  }
 }
}

JiangDu.CS
=========

using System;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Mail;

namespace jiangdu.net
{
 /// <summary>
 /// DomainClient 的摘要说明。
 /// </summary>
 ///

 public class Security
 {
  public string EncryptPassword(string PasswordString,string EncryptType)
  {
   if (EncryptType=="MD5")
   {
    return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5");
   }
   else
   {
    return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1");
   }

   
  }
 }

 }
}