Asp.net MD5 encryption function (c #)

Source: Internet
Author: User

The following method can be used to directly input data and reverse the md5 encrypted code.

/// <Summary>
/// Use md5 Encryption
/// </Summary>
/// <Param name = "Sourcein"> input data </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 );
}

The above only applies. however, many companies are currently in the asp-to-asp.net phase. In order not to change the original user database, we need to find the same result as the md5 encryption of asp. The following code is what I have used. For your reference, refer to the yellow part of the code.

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

Namespace WindowsApplication2
{
/// <Summary>
/// Summary of 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>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;

Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();

//
// TODO: add Any constructor code after InitializeComponent calls
//
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </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 = "encrypted ";
This. button1.Click + = new System. EventHandler (this. button#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 = "password ";
//
// 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 = "after MD5 encryption ";
//
// 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 = "offset ";
//
// 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. form#load );
This. ResumeLayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}

/// <Summary>
///
/// </Summary>
/// <Param name = "sdatain"> string to be encrypted </param>
/// <Param name = "move"> offset </param>
/// <Returns> string encrypted by 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 button#click (Object sender, system. eventargs E)
{
This. textbox2.text = getmd5 (this. textbox1.text, this. textbox3.text );
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.