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 ();
}