標籤:
利用下面的方法,可直接輸入資料,反回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[]bytValue,bytHash; bytValue = System.Text.Encoding.UTF8.GetBytes(move+sDataIn); bytHash = md5.ComputeHash(bytValue); 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); } } }
asp.net MD5加密函數(c#)