Usage:
Create a C # console applicationProgram, Set the followingCodeCopy it to use it. For code explanations, see annotations.
Example 1
Using system; <br/> using system. security. cryptography; <br/> using system. text; </P> <p> class example <br/> {<br/> // hash an input string and return the hash as <br/> // a 32 character hexadecimal string. <br/> static string getmd5hash (string input) <br/>{< br/> // create a new instance of the md5cryptoserviceprovider object. <br/> md5cryptoserviceprovider md5hasher = new md5cryptoserviceprovider (); <Br/> // convert the input string to a byte array and compute the hash. <br/> byte [] DATA = md5hasher. computehash (encoding. default. getbytes (input); <br/> // create a new stringbuilder to collect the bytes <br/> // and create a string. <br/> stringbuilder sbuilder = new stringbuilder (); <br/> // loop through each byte of the hashed data <br/> // and format each one as a hexadecimal string. <br/> For (INT I = 0; I <data. length; I ++) <br/>{< br/> sbuilder. append (data [I]. tostring ("X2"); <br/>}< br/> // return the hexadecimal string. <br/> // return sbuilder. tostring (); // return the 32-bit MD5 value <br/> return sbuilder. tostring (). substring (8, 16); // return the 16-bit MD5 value <br/>/* string. substring (INT index, int length) <br/> * index: Start position, starting from 0 <br/> * length: length of the substring <br/> */<br/>}< br/> // verify a hash against String. <br/> static bool verifymd5hash (string input, string hash) <br/>{< br/> // hash the input. <br/> string hashofinput = getmd5hash (input); <br/> // create a stringcomparer an comare the hashes. <br/> stringcomparer comparer = stringcomparer. ordinalignorecase; <br/> If (0 = comparer. compare (hashofinput, hash) <br/>{< br/> return true; <br/>}< br/> else <br/>{< br/> return false; <br/>} <Br/>}< br/> static void main () <br/> {<br/> string source = "Hello world! "; <Br/> string hash = getmd5hash (source); <br/> console. writeline ("the MD5 hash of" + Source + "is:" + hash + ". "); <br/> console. writeline ("verifying the hash... "); <br/> If (verifymd5hash (source, hash) <br/>{< br/> console. writeline ("the hashes are the same. "); <br/>}< br/> else <br/>{< br/> console. writeline ("the hashes are not same. "); <br/>}< br/> // This code example PR Oduces the following output: <br/> // the MD5 hash of Hello world! Is: ed076287532e86365e841e92bfc50d8c. <br/> // verifying the hash... <br/> // The hashes are the same. <br/>/* <br/> * encoding. ASCII. getbytes (inputstring) is used to convert a string into a byte array in ASCII mode. <br/> * this is because the computehash method only receives the byte [] parameter, the following content is to connect the encrypted byte [] into a string. <br/> * Format String in appendformat {0: x2} refers to formatting each character in the array into hexadecimal format with a precision of 2 <br/> */
Example 2: similar to Example 1
Using system; <br/> using system. collections. generic; <br/> using system. text; <br/> using system. security. cryptography; </P> <p> namespace consoleapplication1 <br/>{< br/> class Program <br/>{< br/> Public static void main (string [] ARGs) <br/> {<br/> console. writeline ("Hello world! "); <Br/> string STR = encryption. stringtomd5hash ("xuwei"); // The static method can be called directly by class name <br/> console. writeline (STR ); <br/>}< br/> class encryption // MD5 encryption for 32 <br/>{< br/> Public static string stringtomd5hash (string inputstring) <br/>{< br/> md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider (); <br/> byte [] encryptedbytes = md5.computehash (encoding. ASCII. getbytes (inputstring); <br/> stringbuilder sb = new stringbuilder (); <br/> for (INT I = 0; I <encryptedbytes. length; I ++) <br/>{< br/> Sb. appendformat ("{0: X2}", encryptedbytes [I]); <br/>}< br/> return sb. tostring (); <br/>}</P> <p >}< br/>
Updated on January 1, March 8
When I sorted out my blog, I found that the MD5 method was actually provided. The Code is as follows:
// Note: static method for self-adding MD5 passwords <br/> Public static string getmd5 (string Str) // MD5 calculation <br/>{< br/> return system. web. security. formsauthentication. hashpasswordforstoringinconfigfile (STR, "MD5 "). tolower (). substring (8, 16); <br/>}< br/>