Asp. Several methods of MD5 and SHA1 encryption in net

Source: Internet
Author: User
Tags hash md5 sha1 sha1 encryption

The full name of MD5 is message-digest algorithm 5 (Information-Digest algorithm), in the early 90 by MIT Laboratory for Computer and RSA Data Security Inc, Ronald L. Riv EST developed and developed by MD2, Md3 and MD4. Its role is to allow bulk information to be "compressed" into a confidential format (that is, to transform a byte string of any length into a long, large integer) before signing the private key with the digital signature software. Whether they are MD2, MD4, or MD5, they need to obtain a random length of information and produce a 128-bit summary of the information.

The cryptographic hash function maps a binary string of any length to a small binary string of fixed length. The cryptographic hash function has the property that it is not possible to find two different inputs with the same value in the calculation, that is, the hash value of the two sets of data matches only if the corresponding data matches. A small amount of change in the data produces unpredictable and significant changes in the hash value. So it's hard to find clues from the encrypted text.

The full name of SHA1 is secure Hash algorithm (Secure Hash Algorithm)

The hash value of the MD5 algorithm is 128 bits in size. The hash value of the SHA1 algorithm is 160 bits in size. Both of these algorithms are irreversible.

Although August 17, 2004 at the International Cryptography Conference in Santa Barbara, Calif. (Crypto ' 2004), Professor Wangxiaoyun of the University of Shandong, China, made a report deciphering the MD5, HAVAL-128, MD4 and RIPEMD algorithms, releasing the results of the MD series algorithm. Declared an impregnable world password standard MD5 fortress collapsed, triggering a firestorm in the field of cryptography. But I think for us to do ordinary software, the three-link material, this encryption security level is enough to use.

We usually use the most is nothing but encrypt the user password, the encrypted password stored in the database, password comparison, the user entered the password to encrypt again, and then the database with the ciphertext to compare. As for the ASP.net class is how to implement the encryption algorithm, which we do not need to care, will be used on the line.

Here are several encryption methods in asp.net. There are two cryptographic algorithms, the above mentioned MD5 and SHA1, here I cite the example of MD5, SHA1 roughly the same, but the use of different classes.

MD5 Related classes:

System.Security.Cryptography.MD5

System.Security.Cryptography.MD5CryptoServiceProvider ()

System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (strsource, "MD5")

SHA1 Related classes:

System.Security.Cryptography.SHA1

System.Security.Cryptography.SHA1CryptoServiceProvider ()

System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (strsource, "SHA1")

The method is as follows: (used vs2005)

1/**////<summary>


2///Method One: Create an object by using the new operator


3///</summary>


4///<param name= "strsource" > Need encrypted plaintext </param>


5///<returns> returns 16-bit encryption results, which take the 9th to 25-bit </returns>
of 32-bit encryption results

6 public string Get_md5_method1 (string strsource)


7 {


8//new


9 System.Security.Cryptography.MD5 MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider ();


10


11//Get the ciphertext section array


byte[] Bytresult = Md5.computehash (System.Text.Encoding.Default.GetBytes (strsource));


13


14//Convert to string and take 9 to 25 bits


string strresult = Bitconverter.tostring (Bytresult, 4, 8);


16//Convert to String, 32-bit


//string strresult = bitconverter.tostring (Bytresult);


18

The converted string of
//bitconverter produces a separator between each character and needs to be removed


strresult = Strresult.replace ("-", "");


return strresult;


22}


23


/**////<summary>


25///Method Two: Creates an object that implements a particular cryptographic algorithm by invoking the Create method on the abstract class of a particular cryptographic algorithm.


///</summary>


///<param name= "strsource" > Need encrypted plaintext </param>


///<returns> return 32-bit encryption results </returns>


public string Get_md5_method2 (string strsource)


30 {


to String strresult = "";


32


//create


System.Security.Cryptography.MD5 MD5 = System.Security.Cryptography.MD5.Create ();


35


36//Pay attention to the choice of encoding UTF8, UTF7, Unicode, etc.


Notoginseng byte[] Bytresult = Md5.computehash (System.Text.Encoding.UTF8.GetBytes (strsource));


38


39//Byte type array is converted to a string


for (int i = 0; i < bytresult.length; i++)


41 {


42//16 Conversion


strresult = Strresult + bytresult[i]. ToString ("X");


44}


return strresult;


46}


47


/**////<summary>


49///Method III: Direct use of hashpasswordforstoringinconfigfile generation


///</summary>


Wuyi///<param name= "strsource" > Need encrypted plaintext </param>


///<returns> return 32-bit encryption results </returns>


public string get_md5_method3 (string strsource)


54 {


return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (strsource, "MD5");


56}

These cryptographic functions are executed on the server side, that is, when the user enters the password, from the client to the server side of the transmission, the user's password is not protected, very dangerous. The bank's approach is to install ActiveX controls on the client, encrypt some important information on the client, and then send it. This I will not pull, I would like to learn to do this ActiveX control.

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.