.net md5加密后的代码

来源:互联网 发布:文章被审稿人质疑数据 编辑:程序博客网 时间:2024/05/16 07:12

利用下面的方法,可直接输入数据,反回md5加密后的代码

/// <summary>
/// 用md5加密
/// </summary>
/// <param name="Sourcein">输入的数据</param>
/// <returns></returns>
public static string MD5(string Sourcein)
{
   MD5CryptoServiceProvider MD5CSP = new MD5CryptoServiceProvider();
   byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(Sourcein);
   byte[] MD5Out = MD5CSP.ComputeHash(MD5Source);
   return Convert.ToBase64String(MD5Out);
}

以上只是适用于.net的MD5加密,但是现在很多公司处于asp转asp.net的阶段,为了不改动原来的教大的用户数据库,我们需要找到一种跟asp的md5加密后相同的结果,下面的代码是我曾经用过的,给出来大家参考:黄色部分代码

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.Label label3;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.textBox3 = new System.Windows.Forms.TextBox();
   this.label3 = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(200, 96);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "加密";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(88, 24);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(120, 21);
   this.textBox1.TabIndex = 1;
   this.textBox1.Text = "";
   //
   // textBox2
   //
   this.textBox2.Location = new System.Drawing.Point(88, 64);
   this.textBox2.Name = "textBox2";
   this.textBox2.Size = new System.Drawing.Size(312, 21);
   this.textBox2.TabIndex = 2;
   this.textBox2.Text = "";
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(48, 32);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(32, 16);
   this.label1.TabIndex = 3;
   this.label1.Text = "密码";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(16, 64);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(64, 16);
   this.label2.TabIndex = 4;
   this.label2.Text = "md5加密后";
   //
   // textBox3
   //
   this.textBox3.Location = new System.Drawing.Point(280, 24);
   this.textBox3.Name = "textBox3";
   this.textBox3.Size = new System.Drawing.Size(120, 21);
   this.textBox3.TabIndex = 5;
   this.textBox3.Text = "";
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(216, 32);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(48, 16);
   this.label3.TabIndex = 6;
   this.label3.Text = "偏移量";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(416, 133);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.textBox3);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.textBox2);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  /// <summary>
  /// 
  /// </summary>
  /// <param name="sDataIn">需要加密的字符串</param>
  /// <param name="move">偏移量</param>
  /// <returns>sDataIn加密后的字符串</returns>

  public string GetMD5(string sDataIn,string move)
  {
   System.Security.Cryptography.MD5CryptoServiceProvider md5=new System.Security.Cryptography.MD5CryptoServiceProvider();
   byte[]byt,bytHash;
   byt = System.Text.Encoding.UTF8.GetBytes(move+sDataIn);
   bytHash = md5.ComputeHash(byt);
   md5.Clear();
   string sTemp="";
   for(int i=0;i<bytHash.Length;i++)
   {
    sTemp+=bytHash[i].ToString("x").PadLeft(2,'0');
   }
   return sTemp;
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
  
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
    this.textBox2.Text = GetMD5(this.textBox1.Text,this.textBox3.Text);
  }
 }
}

转自:www.jxqc.net

 
原创粉丝点击