Asp. Several encryption methods in net

Source: Internet
Author: User
Tags array object hash net return string tostring client

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 that for us to do ordinary software, 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 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 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.MD5CryptoServiceProvider ()

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

SHA1 Related classes:


System.Security.Cryptography.SHA1CryptoServiceProvider ()

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

The method is as follows: (used vs2005)

2///Method One: Creating an object by using the new operator
4///<param name= "strsource" > Need encrypted plaintext </param>
5///<returns> returns 16-bit encryption result, which takes the 9th to 25 bits of the 32-bit encryption result </returns>
6 public string Get_md5_method1 (string strsource)
7 {
9 System.Security.Cryptography.MD5 MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider ();
11//Get an array of dense text sections
byte[] Bytresult = Md5.computehash (System.Text.Encoding.Default.GetBytes (strsource));
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);
The//bitconverter converted string produces a delimiter in the middle of each character and needs to be removed
strresult = Strresult.replace ("-", "");
return strresult;
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.
///<param name= "strsource" > Need encrypted plaintext </param>
///<returns> return 32-bit encryption results </returns>
The public string get_md5_method2 (string strsource)
30 {
To string strresult = "";
System.Security.Cryptography.MD5 MD5 = System.Security.Cryptography.MD5.Create ();
36//Note the choice of encoding UTF8, UTF7, Unicode, etc.
Panax Notoginseng byte[] Bytresult = Md5.computehash (System.Text.Encoding.UTF8.GetBytes (strsource));
An array of 39//byte types is converted to a string
for (int i = 0; i < bytresult.length; i++)
41 {
42//16 in-process conversion
strresult = Strresult + bytresult[i]. ToString ("X");
Strresult return;
49///Method Three: Direct use of hashpasswordforstoringinconfigfile generation
Wuyi///<param name= "strsource" > Need encrypted plaintext </param>
///<returns> return 32-bit encryption results </returns>
-public string Get_md5_method3 (string strsource)
54 {
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (strsource, "MD5");

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: 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.