Java encryption/Decryption _ MD5, java decryption _ md5
It is very convenient to use the MD5 Digest in Java.
1 package com.cxc.nothing; 2 3 import java.nio.charset.Charset; 4 import java.security.MessageDigest; 5 6 public class MD5Test { 7 public static void main(String[] args) { 8 9 String text = "MD5Test";10 try {11 MessageDigest messageDigest = MessageDigest.getInstance("MD5");12 byte[] md5Bytes = messageDigest.digest(text.getBytes(Charset.forName("UTF-8")));13 for (byte b : md5Bytes) {14 System.out.println(b);15 }16 } catch (Exception e) {17 e.printStackTrace();18 }19 20 }21 22 }
The above code implements the MD5 Digest. Because the digest result is a byte array, which is not a common string, you still have to do it.
It is not easy to convert a byte array to a String. String str = new String (md5Bytes) is used directly. After the result is printed, it turns out that I am so naive.
Then I finally learned about the various types of Baidu. The original byte arrays cannot be directly converted into strings, but the common practice is to convert them into hexadecimal numbers.
The MD5 Digest result is a large integer of bits,
Because 8 bits represent one byte, 128/8 = 16 bytes are used for representation.
Because 4bit represents a hexadecimal number, it is 128/4 = 32 hexadecimal numbers when expressed in hexadecimal format.
If you print the md5Bytes code above, you will see the following byte array:
[-83,-42,-124,-31,-122, 59, 93,-112, 5,-125, 35,104, 37,-92, 49,-62]
There are 16 bytes in total, and each byte needs to be split into two hexadecimal numbers.
Let's take 59 for the split first. The 59 here is in decimal format. Write her as an 8-bit binary 0011 1011, and then convert the four digits into a group of hexadecimal values that are 3 B, so here 59 is converted to 3B.
Next let's take a look at 5. Convert her into an 8-bit binary 0000 0101, and then four digits into a group and convert it into a hexadecimal value of 0 5. So here the 5 is converted to 05, note that it is "05" instead of "5 ".
The split of positive integers is the same as that of negative integers.
Next we split-83, because-83 is a negative integer, so the actual memory is his complement. Calculate the complement of the negative integer, and Add 1 after the positive number binary represents the inverse of all BITs (including the sign bit, 0 to 1, 1 to 0.
First write the binary code of 83 to 01010011, and then take the inverse code of 10101100, and then add 1 to 10101101. Therefore, the actual binary data stored in-83 is 1010 1101, the four digits in the same group are converted to hexadecimal notation a d.
The conversion is attached below
-83-->1010 1101-->A D -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-->1100 0010-->C 2
Change
So the actual bit generated by the MD5 Digest is (ignore spaces ): 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 32-bit hexadecimal representation is add684e1863b5d900583241525a431c2.
The above describes the conversion idea. For specific implementation, there is a lot of code on the Internet, so I will not post it here.
Before finishing this article, I have referred to the following materials and would like 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