This is a common problem. MD5 is implemented in many ways and the results are not consistent. Let alone cross-language. Today I have tried the two methods and the conclusion is as follows:
For-bit encryption, character encoding must be selected. After testing with Java, we found that apart from utf8, ASCII, and default (default corresponds to Java GBK, but this should depend on the current operating system) none of the output results of other encoding methods are correct.
2, 16 bits are intercepted in C # But 32 bits.
3. If you want C #, Java, and other languages to recognize each other after MD5, you should select utf8 During encoding. Of course you must use ASCII and GBK, but you have to know, C # Inside find a GBK are hard to find, unless you know its code page (http://msdn.microsoft.com/en-us/library/system.text.encoding.codepage.aspx), in order to avoid trouble, or use the universal utf8
I used C # To write a console application and output the result to the end. You can compare the results in different languages. zkx is used for English and CEN is used for Chinese.
Using system; using system. security. cryptography; using system. text; using system. web. security; namespace temp {class program {static void main (string [] ARGs) {console. writeline ("======= zkx =========="); console. writeline ("utf8:" + md5_32 ("zkx"); console. writeline ("Default:" + md5_32 ("zkx", encoding. default); console. writeline ("ASCII:" + md5_32 ("zkx", encoding. ASCII); console. writeline ("UNICODE:" + md5_32 ("zkx", encoding. unicode); console. writeline ("UTF32:" + md5_32 ("zkx", encoding. UTF32); console. writeline ("utf7:" + md5_32 ("zkx", encoding. utf7); console. writeline ("========= zhongke mail ========="); console. writeline ("utf8:" + md5_32 ("zhongkexin"); console. writeline ("Default:" + md5_32 ("zhongkexin", encoding. default); console. writeline ("ASCII:" + md5_32 ("zhongkexin", encoding. ASCII); console. writeline ("UNICODE:" + md5_32 ("zhongkexin", encoding. unicode); console. writeline ("UTF32:" + md5_32 ("zhongke Xin", encoding. UTF32); console. writeline ("utf7:" + md5_32 ("zhongkexin", encoding. utf7); console. writeline ("=============="); console. writeline ("zkx @ 16 \ t" + md5_16 ("zkx"); console. writeline ("zkx @ 16 \ t" + md5_16b ("zkx"); console. writeline ("zkx @ 16 \ t" + md5_web ("zkx"); console. writeline ("zhongkexin @ 16 \ t" + md5_16 ("zhongkexin"); console. writeline ("zhongkexin @ 16 \ t" + md5_16b ("zhongkexin"); console. writeline ("zhongkexin @ 16 \ t" + md5_web ("zhongkexin"); console. writeline ("===================="); console. writeline ("zkx @ 16 \ t" + encrypt ("zkx"); console. writeline ("zhongkexin @ 16 \ t" + encrypt ("zhongkexin");} static string md5_32 (string input) {return md5_32 (input, null );} /// <summary> /// MD5 32-bit encryption /// </Summary> /// <Param name = "input"> source string </param> /// <Param name = "encoding"> character encoding, the default value (null) is utf8 </param> // <returns> </returns> static string md5_32 (string input, encoding) {If (encoding = NULL) {encoding = encoding. utf8;} // using (md5cryptoserviceprovider md5hash = new md5cryptoserviceprovider () using (MD5 md5hash = md5.create () {// convert the input string to a byte array and compute the hash. byte [] DATA = md5hash. computehash (encoding. 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> /// after MD5 16-bit encryption, the password is capitalized /// </Summary> /// <Param name = "input"> </param> /// <returns> </returns> Public static string md5_16 (string input) {using (md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider () {string t2 = bitconverter. tostring (md5.computehash (utf8encoding. default. getbytes (input), 4, 8); t2 = t2.replace ("-", ""); Return T2 ;}} /// <summary> /// after MD5 16-bit encryption, the password is capitalized /// </Summary> /// <Param name = "input"> </param> /// <returns> </returns> Public static string md5_16b (string input) {using (md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider () {return bitconverter. tostring (md5.computehash (utf8encoding. default. getbytes (input), 4, 8) ;}} public static string md5_web (string input) {return formsauthentication. hashpasswordforstoringinconfigfile (input, "MD5");} // Add a public static string encrypt (string password) to each two digits of the md5_32 Method) {// obtain the byte array /// obviously, different outputs are generated based on different character encodings. The md5_32 method has demonstrated that, the variable byte [] clearbytes = encoding is not encapsulated here. unicode. getbytes (password); // obtain the hash value byte [] hashedbytes = (hashalgorithm) cryptoconfig. createfromname ("MD5 ")). computehash (clearbytes); // get the encrypted information return bitconverter. tostring (hashedbytes );}}}
Output: