Java MD5 encryption and decryption

Source: Internet
Author: User
Tags md5 encryption
The full name of MD5 is message-Digest algorithm 5. Message-digest refers to the hash transformation of the message. It is to convert a byte string of any length into a long big integer. MD5 converts a "Byte string" of any length into a large integer of BITs, and it is an irreversible String Conversion Algorithm. In other words, even if you see the source program and algorithm description, it is also impossible to convert an MD5 value back to the original string. In terms of mathematical principle, it is because there are infinite numbers of original strings, which is a bit like a mathematical function without an inverse function. 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 (); // use MD5 to create the messagedigest object messagedigest mdtemp = messagedigest. getinstance ("MD5"); mdtemp. update (strtemp); byte [] MD = mdtemp. digest (); Int J = md. length; char STR [] = new char [J * 2]; int K = 0; For (INT I = 0; I <j; I ++) {byte B = md [I]; // system. out. println (INT) B); // double-byte encryption for no count (INT) B str [k ++] = hexdigits [B> 4 & 0xf]; STR [k ++] = hexdigits [B & 0xf];} return new string (STR);} catch (exception e) {return NULL ;}} // test public static void main (string [] ARGs) {system. out. println ("after the MD5 encryption of caidao:/N" + test_md5.md5 ("caidao"); system. out. println (" Http://www.baidu.com/ After the MD5 encryption of:/N "+ test_md5.md5 (" Http://www.baidu.com/ "));}} A typical application of MD5 is to generate fingerprint (fingerprint) for a message (byte string) to prevent "tampering ". For example, you write your statement in a readme.txt file and generate an MD5 value for this readme.txt file and record it. Then you can spread the file to others. If someone else modifies any content in the file, you will find it when re-calculating the MD5 value for this file. If there is another third-party certification organization, MD5 can also prevent the file author from "Denying". This is the so-called number *** application.

MD5 is also widely used in encryption and decryption technology. In many operating systems, users' passwords are stored in MD5 values (or similar algorithms, the system calculates the password entered by the user into an MD5 value, and then compares it with the MD5 value saved in the system. The system does not "know" what the user password is. In theory, MD5 is a one-way hash Hash hash. Some hackers crack this password in a way called "Running Dictionary. There are two ways to get the dictionary: one is the string table that is collected daily and the other is generated by means of arrangement and combination, use the MD5 program to calculate the MD5 value of these dictionary items, and then use the target MD5 value for retrieval in this dictionary. Even if the maximum length of the password is 8, the password can only contain letters and numbers, a total of 26 + 26 + 10 = 62 characters, the number of items in the dictionary is P (62,1) + P (62,2 ).... + P () is already a very astronomical number. to store this dictionary, you need a TB-level disk group. In addition, this method has a premise, it is only possible to obtain the MD5 value of the password of the target account. 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 (); // use MD5 to create the messagedigest object messagedigest mdtemp = messagedigest. getinstance ("MD5"); mdtemp. update (strtemp); byte [] MD = mdtemp. digest (); Int J = md. length; char STR [] = new char [J * 2]; int K = 0; For (INT I = 0; I <j; I ++) {byte B = md [I]; // system. out. println (INT) B); // double-byte encryption for no count (INT) B str [k ++] = hexdigits [B> 4 & 0xf]; STR [k ++] = hexdigits [B & 0xf];} return new string (STR);} catch (exception e) {return NULL ;}} // test public static void main (string [] ARGs) {system. out. println ("after the MD5 encryption of caidao:/N" + test_md5.md5 ("caidao"); system. out. println (" Http://www.baidu.com/ After the MD5 encryption of:/N "+ test_md5.md5 (" Http://www.baidu.com/ "));}} A typical application of MD5 is to generate fingerprint (fingerprint) for a message (byte string) to prevent "tampering ". For example, you write your statement in a readme.txt file and generate an MD5 value for this readme.txt file and record it. Then you can spread the file to others. If someone else modifies any content in the file, you will find it when re-calculating the MD5 value for this file. If there is another third-party certification organization, MD5 can also prevent the file author from "Denying". This is the so-called number *** application.

MD5 is also widely used in encryption and decryption technology. In many operating systems, users' passwords are stored in MD5 values (or similar algorithms, the system calculates the password entered by the user into an MD5 value, and then compares it with the MD5 value saved in the system. The system does not "know" what the user password is. In theory, MD5 is a one-way hash Hash hash. Some hackers crack this password in a way called "Running Dictionary. There are two ways to get the dictionary: one is the string table that is collected daily and the other is generated by means of arrangement and combination, use the MD5 program to calculate the MD5 value of these dictionary items, and then use the target MD5 value for retrieval in this dictionary. Even if the maximum length of the password is 8, the password can only contain letters and numbers, a total of 26 + 26 + 10 = 62 characters, the number of items in the dictionary is P (62,1) + P (62,2 ).... + P () is already a very astronomical number. to store this dictionary, you need a TB-level disk group. In addition, this method has a premise, it is only possible to obtain the MD5 value of the password of the target account.

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.