Two Methods of MD5 encryption: one is similar to the MD5 encryption method used in ASP, and the other is similar to the hex_md5 encryption method used in Js.

Source: Internet
Author: User
Tags md5 hash

Using system. Security. cryptography needs to be referenced;

Method 1: similar to the MD5 encryption method in ASP

/// <Summary>
/// Obtain the MD5 encrypted string
/// </Summary>
/// <Param name = "sourcestring"> string to be encrypted </param>
/// <Param name = "enclen"> Data Encryption length, 16-bit or 32-bit </param>
/// <Returns> </returns>
Public static string MD5 (string sourcestring, int enclen)
{
Md5cryptoserviceprovider md5hasher = new md5cryptoserviceprovider ();
Byte [] DATA = md5hasher. computehash (encoding. Default. getbytes (sourcestring ));
Stringbuilder sbuilder = new stringbuilder ();
Int I = 0;
If (enclen = 16)
{
For (I = 4; I <= 11; I ++)
{
Sbuilder. append (data [I]. tostring ("X2 "));
}
}
Else
{
For (I = 0; I <= 15; I ++)
{
Sbuilder. append (data [I]. tostring ("X2 "));
}
}
Return sbuilder. tostring ();
}

 

Method 2: similar to the hex_md5 encryption method in JS

// The same as hex_md5 in Javascript
Public static string calculatemd5hash (string input)
{
// Step 1, calculate MD5 hash from input
MD5 MD5 = system. Security. cryptography. md5.create ();
Byte [] inputbytes = system. Text. encoding. ASCII. getbytes (input );
Byte [] hash = md5.computehash (inputbytes );

// Step 2, convert byte array to hex string
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I {
SB. append (hash [I]. tostring ("X2 "));
}
Return sb. tostring ();
}

 

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.