Reprint Please specify source: http://www.cnblogs.com/Joanna-Yan/p/6900536.html
Java server background when communicating with the Android app, there is an inconsistency between the two ends about using MD5 to encrypt the same string result that contains Chinese.
Specific problem Description:
Java server background and Android end as using the same MD5 tool class, and both sides of the project's default encoding is UTF-8, the encryption of the pure English number of strings, the result is consistent, the same contains Chinese string encryption, found the result is different, this is why?
The tool class Md5util code is as follows:
Public classMd5util {/*** Convert byte array to 16 binary output *@parambytes *@return */ Public StaticString convertbytetohexstring (byte[] bytes) {String result=""; for(inti = 0; i < bytes.length; i++) { intTemp=bytes[i]&0xff; String Temphex=integer.tohexstring (temp); if(Temphex.length () <2) {result+ = "0" +Temphex; }Else{result+=Temphex; } } returnresult; } /*** MD5 Encryption *@parammessage *@return * @throwsunsupportedencodingexception*/ Public StaticString md5jdk (String message)throwsunsupportedencodingexception{String Temp=""; Try{messagedigest md5digest=messagedigest.getinstance ("MD5"); byte[] Encodemd5digest=md5digest.digest (Message.getbytes ()); Temp=convertbytetohexstring (encodemd5digest); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } returntemp; }}
The last question is locked in:
The IDE's project default encoding is not the same as the encoding of the Platform runtime Environment.
Change to: Message.getbytes ("UTF-8"), can solve the problem of Chinese encryption inconsistency.
Below, a deep understanding of the GetBytes () method of the string.
The GetBytes () method of string is to get a byte array of a string, but it is particularly important to note that this method returns a byte array of the operating system's default encoding format. If you do not consider this when using this method, you will find that running a good system on one platform and putting it on another machine can create unexpected problems.
For example, the following program:
Public class Testcharset { publicstaticvoid main (string[] args) { new Testcharset (). Execute (); Private void Execute () { = "hello! Hello!" "; byte [] bytes = s.getbytes (); System.out.println ("bytes Lenght is:" + bytes.length);} }
In a Chinese windowsxp system, the result is:
Bytes Lenght Is:12
But if you put it in an English-language UNIX environment, run it:
$ java testcharset bytes Lenght Is:9
If your program relies on that result, it will cause problems in subsequent operations. Why is the result in one system 12, while the other one becomes 9? As mentioned above, this method is related to the Peace Table (coding).
In the Chinese operating system, the GetBytes method returns a Chinese-encoded byte array of GBK or GB2313, where each of the characters occupies two bytes. In the English platform, the general default encoding is "Iso-8859-1", with each character taking only one byte (regardless of whether it is not a Latin-Latin character).
Encoding support in Java
Java is a multi-country encoding, in Java, characters are stored in Unicode, for example, the "You" word Unicode encoding is "4f60", we can use the following experimental code to verify:
Public class Testcharset { publicstaticvoid main (string[] args) { Char c = ' you '; int i = C; System.out.println (c); System.out.println (i); } }
Regardless of whether you execute on any platform, you will have the same output:
20320
20320 is the integer value of the Unicode "4f60". In fact, you can decompile the above class, you can find that in the production of the. calss file the character "you" (or any other string of characters) itself is stored in Unicode encoding:
char c = '/u4f60 '; ... ...
Even if you know the encoded encoding format, for example:
Javac-encoding GBK Testcharset.java
The. class file that is generated after compilation still stores Chinese characters or strings in Unicode format.
So. to avoid this problem, it is recommended that you use the String.getbytes (String CharSet) method in your code .
Below we will extract the byte arrays of iso-8859-1 and GBK in two encoded formats from the string to see what happens:
Public classTestcharset { Public Static voidMain (string[] args) {NewTestcharset (). Execute (); } Private voidExecute () {String s= "Hello! Hello!" "; byte[] bytesISO8859 =NULL; byte[] BYTESGBK =NULL; Try{bytesISO8859= S.getbytes ("Iso-8859-1"); BYTESGBK= S.getbytes ("GBK"); } Catch(java.io.UnsupportedEncodingException e) {e.printstacktrace (); } System.out.println ("--------------/N 8859 bytes:"); System.out.println ("Bytes is:" +arraytostring (bytesISO8859)); System.out.println ("Hex format is:" +Encodehex (bytesISO8859)); System.out.println (); System.out.println ("--------------/n GBK Bytes:"); System.out.println ("Bytes is:" +arraytostring (BYTESGBK)); System.out.println ("Hex format is:" +Encodehex (BYTESGBK)); } Public Static FinalString Encodehex (byte[] bytes) {StringBuffer Buff=NewStringBuffer (Bytes.length * 2); String b; for(inti = 0; i < bytes.length; i++) {b=integer.tohexstring (Bytes[i]); //byte is two bytes, and the upper Integer.tohexstring expands the bytes to 4 bytesBuff.append (B.length () > 2? b.substring (6, 8): b); Buff.append (" "); } returnbuff.tostring (); } Public Static FinalString arraytostring (byte[] bytes) {StringBuffer Buff=NewStringBuffer (); for(inti = 0; i < bytes.length; i++) {buff.append (Bytes[i]+ " "); } returnbuff.tostring (); } }
Execution Result:
--------------/n8859 101 108 108 111 in the 6c 6c 6f----------- ---/101 108 108 111 33-60-29-70-61-93-95 hex format is:
It can be seen that the byte array in the 8859-1 format extracted in S is 9, the Chinese characters are "63", the ASCII code is "?". ", some foreign programs in the domestic Chinese environment when running, often garbled, it is covered with"? "Is the result of the encoding not being processed correctly.
The GBK encoding of Chinese characters is correctly obtained by extracting the GBK encoded byte array. Characters "You", "good", "!" The GBK codes are: "C4e3", "Bac3", "A3A1", respectively. You can use the following method when you have the correct byte array encoded in GBK and later need to revert to Chinese text strings:
New String (byte[] bytes, string charset)
If this article is helpful to you, please give me a reward!
In-depth parsing of encoding problems in Java string GetBytes ()