Use MD5 to encrypt a string of C # VS2005 Sample Code

Source: Internet
Author: User

MD5 's encryption has been out for a long time, is not a special new technology, the purpose of writing this is also a bit like a note to yourself, after all, the function of encryption is not used, up to write a class, not to call the good, afraid that they will forget, so write this down.

The initial UI is set as follows:

H is simple encryption, and Salt is run salted hash of the action.

Simple encryption is not good, if the person guessed that the encryption method is adopted MD5, there is no salted, that will be a bit higher, and the effect of salt, because more than a value to encrypt, in addition to the previous to guess outside, but also to know salted value can. and salted Value depends on how you're going to set it up, and it's just a simple demo.

The simple encryption part, which is actually very simple, the code is as follows:

byte[] Original = Encoding.Default.GetBytes (Txt_source.text); Turn the string into byte[]
MD5 S1 = MD5. Create (); Using MD5
byte[] Change = S1.computehash (Original);//on-line encryption
Txt_result.text = convert.tobase64string (change);//turns the encrypted string from byte[] back to string

Very short four lines, it can be achieved, but the person still likes to use salted, after all to encrypt, it is necessary to reduce the risk. And this method encrypts the following results:

Value: 123456789

Outcome: jfnnldi7rtif9rgfg2jncw==

The Salted hash is a bit more complicated:

String salted = Txt_Salt.Text.Trim (); Declare the variables, save the salted value
if (salted. Length = = 0)//If the user does not give the salt value, then give the preset
...{
Salted = "t15t";
}
byte[] Original = Encoding.Default.GetBytes (Txt_source.text);//Turn source string to byte[]
byte[] Saltvalue = Encoding.Default.GetBytes (salted);//salted value to byte[]
byte[] Tosalt = new Byte[original.length + saltvalue.length]; Declare a new byte[] to store the value after encryption
Original.copyto (Tosalt, 0);//Copy source string to new byte[]
Saltvalue.copyto (Tosalt, original.length);//Copy salted value to new byte[]
MD5th = MD5. Create ();//Use MD5
byte[] Saltpwd = St.computehash (Tosalt);//encryption
byte[] PWD = new Byte[saltpwd.length + saltvalue.length];//announce the value of the byte[] stored encryption and salted
Saltpwd.copyto (PWD, 0);//Copy the value after encryption to the new byte[]
Saltvalue.copyto (PWD, saltpwd.length);//Copy salted value to new byte[]
Txt_result.text = convert.tobase64string (PWD);//Display a string after the salted hash

That byte[] to put that, that byte[] what to do, it is easy to pay no attention, put wrong to go in. But this one after the encryption if the following:

Value: 123456789

Salted Value:gotest

Outcome: erxxv9v5ryecjaif1z3yz29uzxn0aa==

Because this is still no way back to the encryption method, so even if the database of the material outflow, do not be afraid that these resources will be back, and salted value to see how we want to "move" it.

Source: Encodetest.rar


Http://www.dotblogs.com.tw/jeff-yeh/archive/2008/06/25/4371.aspx

Use MD5 to encrypt a string of C # VS2005 Sample Code

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.