In the MD5 mode, sha1 generates hash encryption for the specified string

Source: Internet
Author: User
Tags md5 hash

# Region encrypts the specified string in MD5 Mode

//************************************** *******************************
//
// Security. Encrypt () method
//
// The encrypt method encrypts a clean string into a hashed string
//
//************************************** *******************************

/// <Summary>
/// Encrypt the specified string in MD5 Mode
/// </Summary>
/// <Param name = "cleanstring"> </param>
/// <Returns> </returns>
Public static string encrypt (string cleanstring)
{
Byte [] clearbytes = new unicodeencoding (). getbytes (cleanstring );
Byte [] hashedbytes = (hashalgorithm) cryptoconfig. createfromname ("MD5"). computehash (clearbytes );

Return bitconverter. tostring (hashedbytes );
}

# Endregion

 

. NET developers can use a variety of hashesAlgorithmClass. Sha1 and MD5 are commonly used. Next, let's take a look at how to generate a hash for a normal string like "PAUL", so that no one can recognize it.

Use sha1 to generate a hash

We create a new routine and use it to generate a hash for the string "PAUL. Open a new windows application in Visual Studio. NETProgram, Put a command button on the form. When this command button occursClickWhen an event is calledHashtext (). You canCodeAdd it to the form and check the actual effect of the hash algorithm. Before writing the following code, you must import the namespaceSystem. Security. Cryptography.

 
Private sub hashtext (byval texttohash as string) dim sha1 as sha1cryptoserviceprovider dim bytvalue () as byte dim bythash () as byte 'create a new encryption service provider object sha1 = new sha1cryptoserviceprovider' to convert the original string to a byte array bytvalue = _ system. text. encoding. utf8.getbytes (texttohash) 'calculates the hash, and returns a byte array bythash = sha1.computehash (bytvalue) sha1.clear ()' to return the base64 encoded string debug of the hash value. writeline (convert. tobase64string (bythash) end sub

You can call this routine by passing different string values to view the hash value changes. For example, if you pass the string "PAUL" to this routine, the debug window displays the following text:

 
W2h6uygmjt/nq5zqihcbteaxwv8 =

Now, change the input value in this process to "Pauly ". You will see the following output:

 
Proywxj0znmpgf5sbb18 + 7 gsasm =

As you can see, a small change in the input string will produce a completely different character combination. This is the reason why the hash algorithm is effective. It makes it difficult for us to find the rules of the input string, and it is difficult to figure out the original character string based on the encrypted characters.

MD5 can also be used to generate a hash.

After learning about how to use a hash class, you can basically understand all the hash classes. The following method is used for the MD5 hash algorithm. Note that apart fromCryptoserviceproviderExcept for different classes, the code is identical.

 
Private sub hashtextmd5 (byval texttohash as string) dim MD5 as md5cryptoserviceprovider dim bytvalue () as byte dim bythash () as byte create a new encryption service provider object MD5 = new md5cryptoserviceprovider convert the original string to a byte array bytvalue = system. text. encoding. _ utf8.getbytes (texttohash) 'calculate the hash and return a byte array bythash = md5.computehash (bytvalue) md5.clear ()' to return the base64 encoded string debug of the hash value. writeline (convert. tobase64string (bythash) end sub

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.