Because of the work needs, to use the. NET program to invoke a Java-written Web service interface, an interface parameter requires MD5 mode encryption.
MD5 encryption in. NET is easy, using the methods given in MSDN:
Hash an input string and return the hash AS//a character hexadecimal string. static string Getmd5hash (string input) {//Create a new instance of the MD5CryptoServiceProvider object. MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider (); 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 (); }
However, when the input string is Chinese, the problem arises, and the encrypted string is different:
Input work
Java 8c87df93416ebdf251bcb6b328ec3c3b
. NET 12e709e3a9a82b441b739a5c8ba035de
The first thought is that the encoding of Chinese is not the same, but regardless of the UTF8 or Unicode results are different.
Google search for "Java MD5 algorithm" found an article to explain my doubts
Http://blog.csdn.net/Peter_K/archive/2007/03/13/1527629.aspx
char[] Chararray = Instr.tochararray (); byte[] ByteArray = new Byte[chararray.length]; for (int i = 0; i < chararray.length i++) bytearray[i] = (byte) chararray[i]; byte[] md5bytes = Md5.digest (ByteArray);
In the same way in. Net
private string Getmd5hash (String input) {MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider ();//Convert The input string to a byte array and compute the hash. char[] temp = input. ToCharArray (); byte[] buf = new byte[temp. Length]; for (int i = 0; i < temp. Length; i++) {Buf[i] = (byte) temp[i];} byte[] data = Md5hasher.computehash (BUF); 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 (); }
It turns out the same thing.
Doubt: So in Java for MD5 encryption do not know which is the creation of the great God, Java string also has GetBytes function Ah, why do you want to put the Chinese multibyte? is because the default result of GetBytes () is platform-dependent, GetBytes can specify the encoding format ah, such as GetBytes ("UTF-8"), I think the results of this to be more clear.
Fortunately, in Goole search "Java MD5 algorithm" ranked first, or really want me to trouble for a long time ...
The results of the MD5 encryption are recorded as follows:
Input
Job
Output
unicode:71626ce19b9ceee1cd9488793f3c282e
utf8:9a018b21ab114a51dd7b5979198a941b
Default:12e709e3a9a82b441b739a5c8ba035de
Char2byte:8c87df93416ebdf251bcb6b328ec3c3b