MD5 Algorithm Implementation

Source: Internet
Author: User
Tags bitwise rounds

A brief description of the MD5 algorithm is:

MD5 handles the input information in 512-bit groupings (512-bit grouping?). Remove 512 bits of data per processing? ),

Each grouping is divided into 16 32-bit sub-groupings (16 times 32 is just 512),

After the processing of some columns (how to deal with it?) ), the output of the algorithm consists of four 32-bit groupings,

Cascading these 4 32-bit groupings will generate a 128-bit hash value (then the hash value is the last encryption result).

In the MD5 algorithm, it is necessary to fill the information first, so that the result of the bit length to 512 redundancy is equal to 448.

Therefore, the bit length of the information (BITS length) is extended to n*512+448, that is, n*64+56 bytes (Bytes), and n is a positive integer.

The Fill method is as follows:

Fill in the information with an infinite number of 0, until the above conditions are met to stop filling with 0 information. Then, after this result, append a 64-bit binary representation of the pre-fill information length.

(The length of the message here should be a bit length, right?) )。 With this two-step process, the information byte length =n*512+448+64= (n+1) *512, that is, the length is exactly 512 integer times.

This is also to meet the information length in the subsequent processing requirements.

MD5 has 4 32 As Integer parameters called link variables (Chaining Variable), respectively:

a=0x01234567,

B=0x89abcdef,

C=0xfedcba98,

d=0x76543210,

(These four numbers are indicated by the big-endian Method!) The value given for each variable is a high byte stored in the low memory address, and the low byte is stored in the memory high address, that is, the big endian byte order.

The values of variables a, B, C, D in the program are 0x67452301,0xefcdab89,0x98badcfe,0x10325476, Mactan, which is a bit confusing here.

When the four link variables are set, the four-wheel loop operation of the algorithm begins. The number of times the loop is 512 bits of information in the information group.

(That is, the number of data bits after the fill is an integer multiple of 512, and then it takes four rounds to take the 512 bits out of each order).

Copy the above 4 link variables into another 4 variables: A to A, B to B,c to C,d to D.

There are four rounds in the main loop (MD4 only rounds, tricycles?) ), each cycle is very similar.

The first round operates 16 times. Each operation does a nonlinear function operation on the 3 in a, B, C, and D, then adds the resulting result to the fourth variable, a sub-group of text, and a constant.

The resulting result is then looped to the right by moving an indefinite number, plus one of a, B, C, or D. Finally, replace one of a, B, C or D with this result.

Here are the four non-linear functions (one per round) used in each operation.

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)

Description of these four functions: if the corresponding bits of x, Y, and Z are independent and homogeneous, then each bit of the result should also be independent and uniform.

F is a function of bitwise arithmetic. That is, if x, then y, otherwise Z.

H is a bitwise odd-even operator.

Suppose MJ represents the J sub-group of messages (from 0 to 15),

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) represents a = B + ((A + G (b, C, D) + Mj + ti) << s),

HH (A, B, C, D, MJ, S, ti) indicates a = B + ((A + H (b, C, D) + Mj + ti) << s),

II (A, B, C, D, MJ, S, ti) represents a = B + ((A + I (b, C, D) + Mj + ti) << s),

(The couple looked quite symmetrical)

(<< is cyclic left shift, not left shift)

These four rounds (round 16 steps, take 32-digit processing, altogether 64 steps) are:

First round

Second round

Third round

Fourth round

The constant TI can be selected as follows:

In step I, TI is an integral part of 4294967296*abs (sin (i)), the unit of I is radians (4294967296 equals 2 of 32 times).

After all of these have been completed, add a, B, C and D, respectively, to A, B, C, D. Then the algorithm continues to run with the next packet of data, and the final output will be cascading for a, B, C, and D.

In Java, Java. Security. The calculation of MD5 is already defined in MessageDigest, and a MD5 128-bit integer can be obtained simply by invoking it.

This 128-bit (16-byte) is then converted to a hexadecimal representation.

package Com.java.md5;public class MD5 {public static String getMD5 (byte[] source) {string s = Null;char hexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F '};try {java.security.MessageDigest MD = Java.security.MessageDigest.getInstance ("MD5"); md.update (source); byte tmp[] = Md.digest (); char str[] = new Char[16*2] ; int k = 0;for (int i = 0; i <; i++) {byte byte0 = tmp[i];str[k++] = hexdigits[byte0 >>> 4 & 0xf];str[k + +] = hexdigits[byte0 & 0xf];} s = new String (str);} catch (Exception e) {e.printstacktrace ();} return s;} public static void Main (string[] args) {String a = ' I love you '; byte[] b = a.getbytes (); for (int i=0; i<b.length; i++) S Ystem.out.print (B[i] + ""); String str = MD5.GETMD5 (b); System.out.println ("a =" + A + "\ n" + "str =" + str + "\ n");}} 

MD5 Algorithm Implementation

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.