It is still convenient to use the MD5 Digest in Java, directly on the code.
1 Packagecom.cxc.nothing;2 3 ImportJava.nio.charset.Charset;4 Importjava.security.MessageDigest;5 6 Public classMd5test {7 Public Static voidMain (string[] args) {8 9String Text = "Md5test";Ten Try { OneMessageDigest messagedigest = messagedigest.getinstance ("MD5"); A byte[] md5bytes = Messagedigest.digest (Text.getbytes (Charset.forname ("UTF-8")))); - for(byteb:md5bytes) { - System.out.println (b); the } -}Catch(Exception e) { - e.printstacktrace (); - } + - } + A}
The above code can be implemented MD5 summary. Since the result of the digest is a byte array, it is not a common string, so there is work to do.
byte array to string, not yet simple, direct string str = new string (md5bytes); and after printing it, I found I was naïve.
Then a variety of Baidu, I finally know that the original byte array here can not be directly to the string, and the usual practice is to turn into 16 binary numbers.
The result of the MD5 summary is a large integer of 128bit,
Because 8bit represents a byte, it is 128/8=16 bytes when represented in bytes.
Because 4bit represents a hexadecimal, it is 128/4=32 hexadecimal when used in hexadecimal notation.
If you print the md5bytes of the above code, you will see a byte array as follows:
[-83,-42,-124,-31,-122, 59, 93,-112, 5,-125, 35, 104, 37,-92, 49,-62]
There is a total of 16 bytes, and each byte needs to be split into two hexadecimal digits.
We first take 59来 demolition, here the 59 is the decimal, put her into 8 bits of binary 0011 1011, and then 4 bits for a group into 16 is 3 B, so here's 59, finally converted is 3 B.
Next look at 5, turn her into 8 bits of binary 0000 0101, and then 4 bits for a group into 16 is 0 5, so here's 5, the final conversion is 05, note is "05", not "5".
The splitting of positive integers is the same as above, followed by the splitting of negative integers.
Next we split-83, because-83 is a negative integer, so what actually exists is his complement. The complement of the negative integer, which corresponds to the positive binary representation of all the bit negation (including the sign bit, 0 change to 0) plus 1.
First Write 83 binary represents 01010011, then the inverse is 10101100, plus 1 is 10101101, so-83 the actual stored binary should be 1010 1101, the same 4 bits for a set of 16 binary is a D.
The following is an attached conversion
-83-->1010 1101-->-42-->1101 0110-->d 6-124-->1000 0100-->8 4-31-->1110 0001-->E 1-122-->1000 0110-->8 6 59-->0011 1011-->3 B 93-->0101 1101-->5 D-112-- >1001 0000-->9 0 5-->0000 0101-->0 5-125-->1000 0011-->8 3 35-->0010 0011-->2 3 104-- >0110 1000-->6 8 37-->0010 0101-->2 5-92-->1010 0100-->a 4 49-->0011 0001-->3 1-62--& gt;1100 0010-->c 2
Change
So the MD5 abstract actually generates 128bit (please disregard space): 1010 1101 1101 0110 1000 0100 1110 0001 1000 0110 0011 1011 0101 1101 1001 0000 0000 0101 1000 0011 0010 0011 0110 1000 0010 0101 1010 0100 0011 0001 1100 0010
The hexadecimal representation that eventually turns into a 32-bit is ADD684E1863B5D900583236825A431C2.
The above writing is the idea of transformation, concrete implementation, the Internet has a lot of code, here is not posted out.
Before completing this article, I would like to refer to the following information, here to express my gratitude:
Http://www.cnblogs.com/renchunxiao/p/3411370.html
http://blog.csdn.net/hll174/article/details/51063689
http://blog.csdn.net/jiaomenglei/article/details/52728796
Https://wenku.baidu.com/view/3145fb45ad51f01dc281f1c5.html
Https://baike.baidu.com/item/%E8%A1%A5%E7%A0%81/6854613?fr=aladdin
Java Plus decryption _md5