C # hash MD5 Encryption

Source: Internet
Author: User
Tags md5 hash
From: http://hi.baidu.com/zhouwillpower/blog/item/e8da888ba81060d4fd1f1052.html
Public partial class hashencrypt
{
/// <Summary>
/// </Summary>
/// <Param name = "strsource"> string to be encrypted </param>
/// <Returns> encrypted string </returns>
Public static string md5encrypt (string strsource)
{
Return md5encrypt (strsource, 16 );
}

/// <Summary>
/// </Summary>
/// <Param name = "strsource"> string to be encrypted </param>
/// <Param name = "length"> one of 16 or 32 values, others use the. NET default MD5 encryption algorithm </param>
/// <Returns> encrypted string </returns>
Public static string md5encrypt (string strsource, int length)
{
Byte [] bytes = encoding. ASCII. getbytes (strsource );
Byte [] hashvalue = (system. Security. cryptography. hashalgorithm) system. Security. cryptography. cryptoconfig. createfromname ("MD5"). computehash (bytes );
Stringbuilder sb = new stringbuilder ();
Switch (length)
{
Case 16:
For (INT I = 4; I <12; I ++)
SB. append (hashvalue [I]. tostring ("X2 "));
Break;
Case 32:
For (INT I = 0; I <16; I ++)
{
SB. append (hashvalue [I]. tostring ("X2 "));
}
Break;
Default:
For (INT I = 0; I {
SB. append (hashvalue [I]. tostring ("X2 "));
}
Break;
}
Return sb. tostring ();
}

/// <Summary>
/// MD5 Encrypt
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public static string msmd5 (string Str)
{
MD5 MD5 = new md5cryptoserviceprovider ();
Byte [] DATA = system. Text. encoding. Default. getbytes (STR );
Byte [] result = md5.computehash (data );
String ret = "";
For (INT I = 0; I <result. length; I ++)
RET + = Result [I]. tostring ("X"). padleft (2, '0 ');
Return ret;
}

/// <Summary>
/// Get MD5 Hash Value
/// </Summary>
/// <Param name = "input"> </param>
/// <Returns> </returns>
Static string msgetmd5hash (string input)
{
// Create a new instance of the md5cryptoserviceprovider object.
MD5 md5hasher = system. Security. cryptography. md5.create ();
// Convert the input string to a byte array and compute the hash.
Byte [] DATA = md5hasher. computehash (encoding. Default. getbytes (input ));
// Create a new stringbuilder to collect the bytes
// And create a string.
Stringbuilder sbuilder = new stringbuilder ();
// Loop through each byte of the hashed data
// And format each one as a hexadecimal string.
For (INT I = 0; I <data. length; I ++)
{
Sbuilder. append (data [I]. tostring ("X2 "));
}
// Return the hexadecimal string.
Return sbuilder. tostring ();
}

/// <Summary>
/// Verify a hash against a string.
/// </Summary>
/// <Param name = "input"> </param>
/// <Param name = "hash"> </param>
/// <Returns> </returns>
Static bool verifymd5hash (string input, string hash)
{
// Hash the input.
String hashofinput = msgetmd5hash (input );
// Create a stringcomparer an comare the hashes.
Stringcomparer comparer = stringcomparer. ordinalignorecase;
If (0 = comparer. Compare (hashofinput, hash ))
{
Return true;
}
Else
{
Return false;
}
}
}

MD5 is one-way encryption. Well, how can we implement reversible encryption? Well, here is a good collection of articles, which is well summarized. There are two parts in total. Thank you for your interest.

Http://hi.baidu.com/srxljl/blog/item/a0508cb130cbe6550823020e.html well, urgent use of words, you can see here, this simple good understanding http://hi.baidu.com/planner/blog/item/04f3272d10b41a30359bf729.html http://hi.baidu.com/flying02/blog/item/94f185448ebd4c4c500ffe6c.html

Private string MD5 (string Str)
{
String Cl = STR;
String Pwd = "";
System. Security. cryptography. MD5 MD5 = system. Security. cryptography. md5.create (); // instantiate an MD5 object
// After encryption, it is an array of the byte type. Pay attention to the selection of UTF-8/Unicode encoding.
Byte [] S = md5.computehash (encoding. utf8.getbytes (CL ));
// Convert an array of the byte type into a string by using a loop. This string is obtained by regular character formatting.
For (INT I = 0; I <S. length; I ++)
{
// Use the hexadecimal format of the obtained string. The characters in the format are lowercase letters. If uppercase letters (x) are used, the characters in the format are uppercase letters.
Pwd = PWD + s [I]. tostring ("X ");
}
Return PWD;
}

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.