MD5 Compression algorithm Experience

Source: Internet
Author: User
Tags bitwise rounds

See many articles on the internet about MD5 algorithm, is basically a version, obscure, look at the foggy. Until now to understand some of the fur, write to share.

The full name of the MD5 algorithm is the message Digest algorithm (the fifth edition of the Messages Digest algorithm), which is a compressed cryptographic hashing algorithm widely used in the field of computer security, which mainly provides the message integrity. Know that this algorithm can be compressed encryption can be.

The main idea of the algorithm is: the input information is divided into many groupings (the length is L), each grouping has 512 bits (note is bit, MD5 is bit operation). Each grouping is then divided into 16 groupings, each grouping has 32 groups, and after some processing, output a 128-bit hash value.

First, the input information (assuming that the bit length is BL) to group, to ensure that each group is enough 512 bits, but it is impossible to enter the number of bits of information each time is a multiple of 512, so the question is, how to guarantee a multiple of 512? Discard the last group, if only one group? To abandon this method is not feasible, it is only a fill. Fill the words, there is a rule: after the number of digits to 512 for the remainder of the result is 448, so the information bit length into n*512+448, that is, n*54+56 bytes, n is a positive integer. The rule of padding is to add a 1 and several 0 after the information, until the bit length is n*512+448, and then add the information to the in-situ length of the BL 64-bit binary representation, and now the length of the information becomes (n+1) * 512 bits. The effect of these two steps is that the information length happens to be an integer multiple of 512, while ensuring that different messages are not the same after filling.

There are 4 32-bit integer arguments in MD5 called link variables, respectively.

A = 0x01234567

B = 0x89abcdef

C = 0xfedcba98

D = 0x76543210

After setting these four link variables, we enter the four-wheel loop operation of the algorithm, the number of cycles is L. Copy the above 4 link variables into another four variables: A to A, B to B,c to C,d to D.

The algorithm main loop has four rounds, each round is basically very similar, the first round carries on 16 operations, each time does the function operation to three in the ABCD, then adds the result to the fourth variable, the information one sub-group and a constant. The result is then shifted to the right by an indefinite number, plus one of the ABCD, and the result is replaced by the ABCD one.

The function operation has only the following four:

F (x, y, Z) = (x&y) | ( (~x) &z)
G (x, y, Z) = (x&z) | (y& (~z))
H (x, Y, z) =x^y^z
I (x, Y, z) =y^ (x| ( ~Z))
(& is with, | yes or, ~ right and wrong, ^ is XOR)


These functions are designed like this: if the corresponding bits of x, Y, and Z are independent and homogeneous, then each bit of the result should be independent and uniform. the function f is operated in a bitwise manner: if x, then y, otherwise Z. The function h is a bitwise odd-even operator.
set MJ to represent the J Sub-group of the message (from 0 to <<<s), which indicates that the loop moves left s bit, then four actions are:
FF (A,b,c,d,mj,s,ti) represents a=b+ ((A + (F (b,c,d) +mj+ti) <<<s)
GG (a,b,c,d,mj,s,ti) means a=b+ ((A + (G (b,c,d) +mj+ti) <<<s)
HH (a,b,c,d,mj,s,ti) means a=b+ ((A + (H (b,c,d) +mj+ti) <<<s)
II (a,b,c,d,mj,s,ti) means a=b+ ((A + (I (b,c,d) +mj+ti) <<<s)


these four rounds (64 steps) are:
First round
FF (a,b,c,d,m0,7,0xd76aa478)
FF (d,a,b,c,m1,12,0xe8c7b756)
FF (c,d,a,b,m2,17,0x242070db)
FF (b,c,d,a,m3,22,0xc1bdceee)
FF (A,B,C,D,M4,7,0XF57C0FAF)
FF (d,a,b,c,m5,12,0x4787c62a)
FF (c,d,a,b,m6,17,0xa8304613)
FF (b,c,d,a,m7,22,0xfd469501)
FF (A,B,C,D,M8,7,0X698098D8)
FF (D,A,B,C,M9,12,0X8B44F7AF)
FF (C,D,A,B,M10,17,0XFFFF5BB1)
FF (B,C,D,A,M11,22,0X895CD7BE)
FF (a,b,c,d,m12,7,0x6b901122)
FF (d,a,b,c,m13,12,0xfd987193)
FF (c,d,a,b,m14,17,0xa679438e)
FF (b,c,d,a,m15,22,0x49b40821)
Second round
GG (a,b,c,d,m1,5,0xf61e2562)
GG (d,a,b,c,m6,9,0xc040b340)
GG (c,d,a,b,m11,14,0x265e5a51)
GG (B,C,D,A,M0,20,0XE9B6C7AA)
GG (a,b,c,d,m5,5,0xd62f105d)
GG (d,a,b,c,m10,9,0x02441453)
GG (c,d,a,b,m15,14,0xd8a1e681)
GG (B,C,D,A,M4,20,0XE7D3FBC8)
GG (A,B,C,D,M9,5,0X21E1CDE6)
GG (D,A,B,C,M14,9,0XC33707D6)
GG (c,d,a,b,m3,14,0xf4d50d87)
GG (b,c,d,a,m8,20,0x455a14ed)
GG (a,b,c,d,m13,5,0xa9e3e905)
GG (D,A,B,C,M2,9,0XFCEFA3F8)
GG (C,D,A,B,M7,14,0X676F02D9)
GG (b,c,d,a,m12,20,0x8d2a4c8a)
Third round
HH (a,b,c,d,m5,4,0xfffa3942)
HH (d,a,b,c,m8,11,0x8771f681)
HH (c,d,a,b,m11,16,0x6d9d6122)
HH (b,c,d,a,m14,23,0xfde5380c)
HH (a,b,c,d,m1,4,0xa4beea44)
HH (D,A,B,C,M4,11,0X4BDECFA9)
HH (c,d,a,b,m7,16,0xf6bb4b60)
HH (B,C,D,A,M10,23,0XBEBFBC70)
HH (A,B,C,D,M13,4,0X289B7EC6)
HH (D,A,B,C,M0,11,0XEAA127FA)
HH (c,d,a,b,m3,16,0xd4ef3085)
HH (B,C,D,A,M6,23,0X04881D05)
HH (a,b,c,d,m9,4,0xd9d4d039)
HH (d,a,b,c,m12,11,0xe6db99e5)
HH (C,D,A,B,M15,16,0X1FA27CF8)
HH (b,c,d,a,m2,23,0xc4ac5665)
Fourth round
II (a,b,c,d,m0,6,0xf4292244)
II (D,A,B,C,M7,10,0X432AFF97)
II (C,D,A,B,M14,15,0XAB9423A7)
II (b,c,d,a,m5,21,0xfc93a039)
II (A,B,C,D,M12,6,0X655B59C3)
II (d,a,b,c,m3,10,0x8f0ccc92)
II (c,d,a,b,m10,15,0xffeff47d)
II (B,C,D,A,M1,21,0X85845DD1)
II (a,b,c,d,m8,6,0x6fa87e4f)
II (D,A,B,C,M15,10,0XFE2CE6E0)
II (c,d,a,b,m6,15,0xa3014314)
II (B,C,D,A,M13,21,0X4E0811A1)
II (a,b,c,d,m4,6,0xf7537e82)
II (d,a,b,c,m11,10,0xbd3af235)
II (C,D,A,B,M2,15,0X2AD7D2BB)
II (b,c,d,a,m9,21,0xeb86d391)
The constant TI can be selected as follows:In step I, TI is an integral part of 4294967296*abs (sin (i)), and the unit of I is radians. (2 of the 32-second party)
after all this is done, add the a,b,c,d to A,b,c,d respectively. Then the algorithm continues to run with the next packet of data, and the final output is the cascade of A,b,c and D (ABCD each is 32 bits, plus just 128 bits).

The Java example of the MD5 algorithm is attached below. Because Java provides classes for the MD5 algorithm, it can be used directly.

Import java.security.messagedigest;import java.security.nosuchalgorithmexception;/* * MD5 algorithm */public class MD5 {//Global number Group private final static string[] strdigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C    "," D "," E "," F "};        Public MD5 () {}//return form is numeric with string private static string bytetoarraystring (byte bbyte) {int iRet = Bbyte;        System.out.println ("iret=" +iret);        if (IRet < 0) {IRet + = 256;        } int iD1 = IRET/16;        int iD2 = iRet% 16;    return STRDIGITS[ID1] + STRDIGITS[ID2];        }//Return form only for the number private static String Bytetonum (byte bbyte) {int iRet = Bbyte;        System.out.println ("iret1=" + iRet);        if (IRet < 0) {IRet + = 256;    } return string.valueof (IRet); }//Convert byte array to 16 binary string private static string Bytetostring (byte[] bbyte) {StringBuffer sbuffer = new StringBuffer        (); for (int i = 0; i < bbyte.length; I+ +) {sbuffer.append (bytetoarraystring (Bbyte[i]));    } return sbuffer.tostring ();        public static string Getmd5code (String strobj) {string resultstring = null;            try {resultstring = new String (strobj);            MessageDigest MD = messagedigest.getinstance ("MD5");        Md.digest () The function returns a byte array that holds the result of the hash value resultstring = bytetostring (Md.digest (Strobj.getbytes ()));        } catch (NoSuchAlgorithmException ex) {ex.printstacktrace ();    } return resultstring;        } public static void Main (string[] args) {MD5 getMD5 = new MD5 ();    System.out.println (Getmd5.getmd5code ("000000")); }}


MD5 Compression algorithm Experience

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.