Introduction of MD5 algorithm and implementation method of JDK self-bring

Source: Internet
Author: User
Tags decrypt

MD5 algorithm, has been known, as a senior or entry-level developers, are called "MD5 ah, know know." A few days ago the program slightly small problem, because some places are encrypted, and some places are not encrypted. A colleague is proud to say that MD5 is a hash algorithm, it is easy to decrypt. I was speechless at the time. Because I remember MD5 is irreversible, since irreversible, and where to "decrypt" it. So we search the Internet "MD5 decryption", wow Kao, unexpectedly really have the result, but, the fact is not so simple, we put MD5 a string of sequence into the "online MD5 decryption" to decrypt, most of them have not been cracked. One of the shows said it was found, but paid ...

Since then the brothers still defy, not to say MD5 can be cracked. I was speechless again. Do you say there is encryption in the world that can not be cracked??!! is the National Defense Bureau so powerful that it was attacked by various hackers? In other words, if there is a cryptographic algorithm can not be cracked, there are hackers in the world?

Any algorithm can be cracked, but it's just a matter of time. So why do we have to encrypt it? Because not everyone has enough time to decipher your password, but everyone has plenty of time to crawl your plaintext. This is where the meaning of encryption lies.


OK, let me explain briefly how the MD5 algorithm works:

1. Concept and Development history:

The full name of MD5 [3] is message-digest algorithm 5 (Information-abstract algorithm), MD5 is an irreversible algorithm, that is, to generate the ciphertext inverse, corresponding to an infinite number of inverse. Developed by the Ronald L.rivest of MIT Laboratory for computer Sciences (IT computer Science Lab) and RSA Data Security Inc. in the early 90, MD2, MD3 and MD4 developed.

Its role is to allow bulk information to be "compressed" into a confidential format before signing a private key with a digital signature software (converting an arbitrary-length byte string into a long, large integer). Both MD2,MD4 and MD5 need to get a random length of information and generate a 128-bit message digest. Although these algorithms are more or less similar in structure, the design of MD2 is completely different from MD4 and MD5 because MD2 is optimized for 8-bit machines, while MD4 and MD5 are for 32-bit computers. Rivest developed the MD2 algorithm in 1989, in which the information is first used to complement the data, so that the byte length of the information is a multiple of 16, then a 16-bit test and append to the end of the information, and based on the newly generated information to calculate the hash value. Later, Rogier and Chauvaud found that if the test was omitted, the MD2 conflict would arise. The encryption of the MD2 algorithm results in a unique---that is not duplicated. To the MD4 full version of the conflict (this conflict is actually a vulnerability that would result in encryption of different content and possibly the same post-encryption result), without a doubt, MD4 was eliminated from this.

         a year later, in 1991, Rivest developed a technically more sophisticated MD5 algorithm. It adds the concept of "safety-tape" (safety-belts) on the basis of MD4. Although MD5 is slightly slower than MD4, it is more secure. The algorithm is clearly composed of four and MD4 designs with slightly different steps. In the MD5 algorithm, the information--the size and padding of the digest--is exactly the same as the MD4. Den Boer and Bosselaers have found false collisions in the MD5 algorithm (pseudo-collisions), but there are no other results that have been found to be encrypted. Van Oorschot and Wiener once considered a function of violent search collisions in hashing (brute-force hash function), and they guessed that a machine designed specifically to search for MD5 conflicts could find a conflict on average every 24 days, But in the 10 years from 1991 to 2001, there was no new algorithm to replace the MD5 algorithm, we can see that this flaw does not have much effect on the security of MD5. And all these are not enough to become MD5 in the actual application of the problem, and because the use of MD5 algorithm does not need to pay any copyright fees, so in general, MD5 can be considered relatively safe.

           2 related theoretical basis  
           2.1 one-way hash function  
            One-way hash function [4] also known as hash (hash) function. It is the core of modern cryptography. Hash functions are always used in computer science, and a hash function is a function that converts a variable input length string into a fixed-length output value (called a hash value). While a one-way hash function is a hash function that works in one direction, it is easy to calculate its hash value from the pre-mapped value, but it is difficult to make its hash value equal to a special value.  
            Hash functions are public, and the process is not confidential, and the security of a one-way hash function is one-way, and its output is not dependent on input. On average, a change in the single bit of a pre-mapped value will cause a change in half of the hash value. Given a hash value, it is not feasible to find a pre-mapped value so that its value equals a known hash value, and one-way hash function can be thought of as a method to form a fingerprint file. If you verify that someone holds a specific file (you also hold the file), but you do not want him to pass the file on to you, then you should inform him to pass the hash value of the file to you, and if the hash value he transmits is correct, then you can be sure that he holds the file.  
            2.1.1 The basic principle of one-way hash function  
          One-way hash function h ( m) is the operation of an arbitrary-length message M. Returns a fixed-length value of H. &NBSP
          h=h (m)  
          where h is the length M.  
          input is arbitrary length and output as fixed-length functions have many kinds, but the intent hash function has the following characteristics to make it one-way:  
        1) The H value can be calculated conveniently with the given value M.  

2) Given H, it is difficult to calculate m according to H (m) =h.
3) given m, to find another message m ' and make H (m) =h (M ') difficult.
That is, if an intruder wants to get a message, he must destroy the security of each protocol that uses one-way functions, because the key to a one-way function is to produce a unique message digest of M.
In fact, the one-way hash function is based on the thought of the compression function. The output value of a one-way hash function is a hash value of length n, which is much smaller than the length of the input value, which is n<m.

The precondition of this function operation is that we should divide the message into several message fragments according to certain rules, the input value of the hash function is the output value of a message fragment and the previous message fragment, and its output value is the hash value of all the message fragments before the message fragment. This means that the hash value of the message fragment m can be expressed as:



The process can be illustrated as follows:


At the same time, the hash value will be one of the input values of the next operation, thus the hash value of the entire message n is the computed hash value of the last message fragment. In this way, we will eventually get a fixed-length function value.
2.1.2 The length of the hash value
As we all know, the longer the hash value, the better the security, the MD5 algorithm is the one-way hash function produces a 128-bit hash value, so as to ensure its security, the following is the method of generating a long hash value:
1) Use a one-way hash function to generate a hash value for a message.

2) Enclose the hash value after the message.

3) A hash value that produces a series of values, including hash values and messages.
4) combine the hash value produced by the first step with the hash value generated by the third step to generate a larger hash value.
5) Repeat 1 to 3) step several times.

The JDK comes with the algorithm implementation, the Java calling code is as follows:

public class Md5util {public static string MD5 (string source) {String des = ""; try {messagedigest MD = Messagedigest.getins Tance ("MD5"); byte[] result = Md.digest (Source.getbytes ()); StringBuilder buf = new StringBuilder (); for (int i=0;i<result.length;i++) {byte b = result[i];buf.append ( String.Format ("%02x", b));} des = buf.tostring (). toLowerCase ();} catch (Exception e) {e.printstacktrace (); throw new RuntimeException ("MD5 failure");} Return des;} public static void Main (string[] args) {System.out.println (Md5util.md5 ("157701"));}}


Reference Documentation:

Http://wenku.baidu.com/link?url=VFEfTG5U8_ Wpiynhtjezhjgezfymj3tdhpntgfbu2wyt5wu11g1olzaoomsj6hiyibhbz04lovudov5yeeo5cpnxq-xgo08cterx7jx9zlq



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction of MD5 algorithm and implementation method of JDK self-bring

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.