asp.net implement MD5 encryption _ practical skills

Source: Internet
Author: User
Tags lowercase md5 md5 encryption

MD5 encryption simply means that a section of the clear text through some sort of calculation of the ciphertext. For example: Clear text: ABCDEFG through some column operations to get ciphertext 7AC66C0F148DE9519B8BD264312C4D64

It has two characteristics: 1. No collisions, 2 irreversible.

No collision means: 7ac66c0f148de9519b8bd264312c4d64 This cipher text can only be obtained by the ABCDEFG of this paragraph, other than the other plaintext encrypted its value will never equal 7ac66c0f148de9519b8bd264312c4d64, that is to say, without those two plaintext encryption will get the same ciphertext.

Irreversible refers to: clear text through encryption to get ciphertext, but not through the ciphertext to find clear text. That is, when we know that plaintext ADCDEFG can get 7ac66c0f148de9519b8bd264312c4d64 by encryption, but if we know that a piece of text is encrypted and then gets 7AC66C0F148DE9519B8BD264312C4D64, But I can't figure out who 7ac66c0f148de9519b8bd264312c4d64 this text from.

Then some students will ask, specific should be used in what place?

Generally speaking, we do the website login system when the password is stored in ciphertext, generally used are MD5 encryption.

Users fill in the User name password click Registration, we verify that, to the user information into the database, you need to first put the user input password, through the MD5 encryption, the encrypted ciphertext, stored in the password field.

Then there must be a reunion of the careful discovery, just also mentioned that MD5 encryption is irreversible, then the user log in when how to determine the user entered the password is correct?

For example, the user set the password is ABCDEFG, and store the time we store is ABCDEFG encrypted after the value of 7AC66C0F148DE9519B8BD264312C4D64, then the user will enter the password ABCDEFG login, How do we compare the two to equal?

We cannot convert the value before encryption by the encrypted value, so our usual practice is to encrypt the password entered in the user's login again and compare it with the value stored in the database, if equal, the input password is correct.

OK, the basic principle and the use of the scene basically said almost, and finally say how to asp.net in the MD5 encryption.

The MD5 encryption in asp.net is simple, and the code is as follows:

Copy Code code as follows:

FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5"). ToLower ();

It should be noted that if the MD5 encrypted when the conversion is lowercase, then in the verification time to be converted to lowercase, to maintain unity. In addition, the above method is 32-bit MD5 encryption, if it is 16 bits, 32-bit encryption results of the middle 16-bit value can be.

Here are some examples, you can refer to

Copy Code code as follows:

<summary>
MD5 encryption
</summary>
<param name= "strsource" > Need encrypted plaintext </param>
<returns> return 32-bit encryption results </returns>
public static string Get_md5 (String strsource, String sencode)
{
New
System.Security.Cryptography.MD5 MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider ();

Get an array of dense text sections
byte[] Bytresult = Md5.computehash (System.Text.Encoding.GetEncoding (Sencode). GetBytes (strsource));

Converts to a string and takes 9 to 25 bits
String strresult = Bitconverter.tostring (Bytresult, 4, 8);
Convert to String, 32-bit

String strresult = Bitconverter.tostring (Bytresult);

Bitconverter the converted string produces a delimiter in the middle of each character and needs to be removed
strresult = Strresult.replace ("-", "");

return Strresult.tolower ();
}

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.