The full name of MD5 is Message-Digest Algorithm 5. Message-Digest generally refers to the Hash transformation of a byte string (Message), which is to transform a byte string of any length into a large integer of a certain length. MD5 converts a "byte string" of any length into a 128-bit large integer, and it is an irreversible string conversion algorithm. In other words, even if you see the source program and algorithm description, you cannot convert an MD5 The value is converted back to the original string, mathematically speaking, because there are infinitely many original strings, which is a bit like a mathematical function that does not have an inverse function.
Copy the code code as follows:
import java.security.MessageDigest;
public class Test_MD5{
public final static String MD5(String s) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[]strTemp=s.getBytes();
//Create a MessageDigest object using MD5
MessageDigestmdTemp=MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[]md=mdTemp.digest();
intj=md.length;
charstr[]=newchar[j*2];
intk=0;
for(inti=0;i<j;i++){
byteb=md[i];
//System.out.println((int)b);
//Encrypt the number (int)b with double bytes
str[k++]=hexDigits[b>>4&0xf];
str[k++]=hexDigits[b&0xf];
}
returnnewString(str);
}catch(Exceptione){returnnull;}
}
//test
publicstaticvoidmain(String[]args){
System.out.println("caidao's MD5 encryption:/n"+Test_MD5.MD5("caidao"));
System.out.println("//www.VeVB.COm/ after MD5 encryption:/n"+Test_MD5.MD5("//www.VeVB.COm/"));
}
}
A typical application of MD5 is to generate a fingerprint on a Message (byte string) to prevent it from being "tampered with." For example, you write a paragraph in a file called readme.txt, and generate an MD5 value for this readme.txt and record it. Then you can spread this file to others. If others modify the file Any content, you will find it when you recalculate the MD5 of this file. If there is a third-party certification agency, using MD5 can also prevent "repudiation" of the file author. This is the so-called digital encryption application.
MD5 is also widely used in encryption and decryption technology. In many operating systems, the user's password is stored in the form of MD5 value (or other similar algorithms). When the user logs in, the system calculates the password entered by the user into The MD5 value is then compared with the MD5 value saved in the system, and the system does not "know" what the user's password is.
MD5 is theoretically a one-way hash, and some hackers use a method called "dictionary running" to crack this password. There are two ways to obtain a dictionary. One is a daily collection of string tables used as passwords, and the other is generated using a permutation and combination method. First, use the MD5 program to calculate the MD5 values of these dictionary items, and then use the target's MD5 values are searched in this dictionary.
Even if it is assumed that the maximum length of the password is 8, and the password can only be letters and numbers, a total of 26+26+10=62 characters, the number of items in the dictionary formed by the permutation is P(62,1)+P(62, 2)....+P(62,8), that is already an astronomical number. Storing this dictionary requires a TB-level disk group, and this method also has a prerequisite, which is to obtain the target account. This is only possible if the password has an MD5 value.