asp.net MD5加密函数(c#)
利用下面的方法,可直接輸入數據,反回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);
??}
?}
}
?
轉載于:https://www.cnblogs.com/sikezx/archive/2008/08/02/1258525.html
總結
以上是生活随笔為你收集整理的asp.net MD5加密函数(c#)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: netbeans下开发rails快捷键
- 下一篇: WPF的样式(Style)继承