MD5 encryption algorithm for Java irreversible encryption algorithm using the example _java

Source: Internet
Author: User
Tags md5 md5 encryption

The full name of MD5 is message-digest algorithm 5,message-digest a hash transformation of a byte string (message) that transforms a byte string of any length into a long, large integer. MD5 transforms any length of "byte string" into a large integer of 128bit, and it is an irreversible string transform algorithm, in other words, even if you see the source program and the algorithm description, you can not transform a MD5 value back to the original string, mathematically speaking, is because the original string has infinitely many, which is somewhat like a mathematical function with no inverse function.

Copy 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 ();
To 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);
Double-byte encryption with no number (int) b
str[k++]=hexdigits[b>>4&0xf];
str[k++]=hexdigits[b&0xf];
}
Returnnewstring (str);
}catch (Exceptione) {returnnull;}
}
Test
Publicstaticvoidmain (String[]args) {
System.out.println ("Caidao MD5 encrypted: \ n" +test_md5.) MD5 ("Caidao"));
System.out.println ("http://www.jb51.net/MD5 encrypted: \ n" +test_md5.) MD5 ("http://www.jb51.net/"));
}
}

A typical application of MD5 is to produce a fingerprint (fingerprint) of a message (a byte string) to prevent tampering. For example, you would write a phrase in a file called Readme.txt and create a MD5 value on the Readme.txt and record it, and then you can propagate the file to someone else, and if you change anything in the file, you'll find it when you recalculate MD5. If there is a third party certification body, using MD5 can also prevent the file author's "Repudiation", which is called the digital * * * * application.
MD5 is also widely used in encryption and decryption technology, in many operating systems, the user's password is MD5 value (or similar to other algorithms) to save the way, when the user login, the system is the user entered the password to calculate the MD5 value, and then to the system to save the MD5 value, and the system is not " Know what the user's password is.
MD5 is theoretically a one-way hash, and some hackers have cracked the cipher by a method known as a "running dictionary." There are two ways to get dictionaries, one is the daily collection of string tables used for passwords, the other is generated by the permutation and combination method, first the MD5 value of these dictionary items is computed with the MD5 program, and then the MD5 value of the target is retrieved in the dictionary.
Even assuming 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 entries in the combined dictionary is P (62,1) +p (62,2) ... +p (62,8), which is already a very astronomical figure, Storing this dictionary requires a TB-level disk group, and there is a prerequisite for it to be able to obtain the password MD5 value of the target account.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.