C # MD5 Algorithm

Source: Internet
Author: User
Tags rounds

1. Origin
The full name of MD5 is message-Digest algorithm 5 (Information-AbstractAlgorithmAt the beginning of 1990s, MIT Laboratory
Developed by Ronald L. Rivest of for computer science and RSA Data Security Inc,
Developed by md2, md3, and md4. Http://www.ietf.org/rfc/rfc1321.txt, is the most comprehensive document,
Submitted by Ronald L. Rivest to Ieft on April 9, August 1992.

2. Purpose
MD5 is used to generate a message digest for a piece of information.
Uniqueness. It can be used as a digital signature. Used to verify the validity of the file (whether there is lost or damaged data), for the user
Password Encryption: Calculate the hash value in the hash function.

3. Features
Enter a byte string of any length to generate a 128-bit integer. Due to some irreversible features of the algorithm
It provides better security. In addition, you do not have to pay any copyright fee for using the MD5 algorithm.

4. Description
Uniqueness and irreversible are not absolute. Theoretically, the analysis is a multi-to-one relationship, but two different pieces of information are generated.
The probability of the same abstract is very small. Irreversible means that the calculation amount and computing time required to push the input from the output are too large.
The typical method requires too much storage space.

5. Algorithm Description

Algorithm input is a byte string, each of which is 8 bits.
The algorithm is executed in the following steps:

Step 1: complement:
The MD5 algorithm first supplements the input data so that the length of the data (in bytes) is 56 for the 64-bit result.
That is, the data is extended to Len = K * 64 + 56 bytes, and K is an integer.
Complement Method: Fill in 1, and then fill 0 to meet the above requirements. It is equivalent to supplementing a byte 0x80 and then supplementing the value.
0 bytes. In this step, the total number of supplemented bytes is 0 ~ 63.

Step 2: append the Data Length:
Use a 64-bit integer to indicate the original length (in BIT) of the data, and sort the 8 bytes of the number in front of the low position,
The order after the position is high is appended to the data after the position is filled. In this case, the total length after the data is filled is:
Len = K * 64 + 56 + 8 = (k + 1) * 64 bytes.

※Note that the 64-bit integer is the original length of the input data rather than the length after the byte is filled. I have planted a heel here.

Step 3: Initialize the MD5 parameter:
There are four 32-bit integer variables (A, B, C, D) used to calculate the information digest, each variable is initialized to the following
The value in hexadecimal notation, with the low byte at the beginning.
Word A: 01 23 45 67
Word B: 89 AB CD ef
Word C: Fe DC Ba 98
Word D: 76 54 32 10
※Note that the low-level bytes refer to the memory byte arrangement on the little endian platform,
InProgramTo write:
A = 0x67452301
B = 0xefcdab89
C = 0x98badcfe
D = 0x10325476

Step 4: Define four basic MD5 bitwise operation functions:
X, Y, and Z are 32-bit integers.
F (x, y, z) = (X and Y) or (not (X) and Z)
G (x, y, z) = (X and Z) or (Y and not (z ))
H (x, y, z) = x XOR y XOR Z
I (x, y, z) = y XOR (X or not (z ))

Define four functions for four-wheel transformation.
If MJ is set, it indicates the J sub-group of the message (from 0 to 15). If <s indicates that the cycle shifts the S bit left, the following four operations are performed:
Ff (a, B, c, d, MJ, S, Ti) indicates a = B + (a + (f (B, c, d) + MJ + Ti) <s)
GG (a, B, c, d, MJ, S, Ti) indicates 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) indicates a = B + (a + (I (B, C, D) + MJ + Ti) <s)

Step 5: Convert the input data.
Processing Data. n is the total number of bytes. 64 bytes are used as a group. Each group performs a loop and each cycle performs four rounds of operations.
The 64 bytes to be converted are represented by a 16 32-bit integer array M [0... 15. The array T [1... 64] represents a group of constants,
T [I] is a 32-bit integer of 4294967296 * ABS (sin (I). The unit of I is radian and the value of I ranges from 1 to 64.
The specific process is as follows:

/* Set the main cycle variable */
For I = 0 to N/16-1 do

/* Store the original data in array X of 16 elements every cycle .*/
For J = 0 to 15 do
Set X [J] to M [I * 16 + J].
End/end the loop on J

/* Save a as AA, B as BB, C as CC, and D as DD.
*/
AA =
BB = B
Cc = C
Dd = d

/* 1st rounds */
/* Use [abcd k s I] to indicate the following operations:
A = B + (a + F (B, c, d) + X [k] + T [I]) <s ).*/
/* Do the following 16 operations .*/
[ABCD 0 7 1] [dabc 1 12 2] [cdab 2 17 3] [BCDA 3 22 4]
[ABCD 4 7 5] [dabc 5 12 6] [cdab 6 17 7] [BCDA 7 22 8]
[ABCD 8 7 9] [dabc 9 12 10] [cdab 10 17 11] [BCDA 11 22 12]
[ABCD 12 7 13] [dabc 13 12 14] [cdab 14 17 15] [BCDA 15 22 16]

/* 2nd rounds **/
/* Use [abcd k s I] to indicate the following operations:
A = B + (a + g (B, c, d) + X [k] + T [I]) <s ).*/
/* Do the following 16 operations .*/
[ABCD 1 5 17] [dabc 6 9 18] [cdab 11 14 19] [BCDA 0 20 20]
[ABCD 5 5 21] [dabc 10 9 22] [cdab 15 14 23] [BCDA 4 20 24]
[ABCD 9 5 25] [dabc 14 9 26] [cdab 3 14 27] [BCDA 8 20 28]
[ABCD 13 5 29] [dabc 2 9 30] [cdab 7 14 31] [BCDA 12 20 32]

/* 3rd rounds */
/* Use [abcd k s I] to indicate the following operations:
A = B + (a + H (B, c, d) + X [k] + T [I]) <s ).*/
/* Do the following 16 operations .*/
[ABCD 5 4 33] [dabc 8 11 34] [cdab 11 16 35] [BCDA 14 23 36]
[ABCD 1 4 37] [dabc 4 11 38] [cdab 7 16 39] [BCDA 10 23 40]
[ABCD 13 4 41] [dabc 0 11 42] [cdab 3 16 43] [BCDA 6 23 44]
[ABCD 9 4 45] [dabc 12 11 46] [cdab 15 16 47] [BCDA 2 23 48]

/* 4th rounds */
/* Use [abcd k s I] to indicate the following operations:
A = B + (a + I (B, C, D) + X [k] + T [I]) <s ).*/
/* Do the following 16 operations .*/
[ABCD 0 6 49] [dabc 7 10 50] [cdab 14 15 51] [BCDA 5 21 52]
[ABCD 12 6 53] [dabc 3 10 54] [cdab 10 15 55] [BCDA 1 21 56]
[ABCD 8 6 57] [dabc 15 10 58] [cdab 6 15 59] [BCDA 13 21 60]
[ABCD 4 6 61] [dabc 11 10 62] [cdab 2 15 63] [BCDA 9 21 64]

/* Perform the following operations */
A = a + AA
B = B + BB
C = C + CC
D = d + dd

Next I/* terminate the loop on I */

Step 6: output the result.
A, B, C, and D are continuously stored in 16 bytes, 128 bits. Output the 16 bytes in hexadecimal order.

Finally, after implementing the algorithm using the program language, you can enter the following information to perform a simple test on the program,
Check whether the program has any errors.
MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
MD5 ("A") = 0cc175b9c0f1b6a831c399e269772661
MD5 ("ABC") = 900150983cd24fb0d6963f7d28e17f72
MD5 ("Message Digest") = f96b697d7cb7938d525a2f31aaf161d0
MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
MD5 ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789") =
D174ab98d277d9f5a5611c2c9f419d9f
MD5 ("123456789012345678901234567890123456789012345678901234567890123456789"
01234567890 ") = 57edf4a22be3c955ac49da2e21_b67a

C # program of MD5 Algorithm

 

Code

 ///     <Summary>  
/// MD5 Encryption
/// </Summary>
/// <Param name = "password"> </param>
/// <Returns> </returns>
Public Static String Md5encrypt ( String Password)
{
String Strpassword = "" ;
MD5 MD5;
Utf8encoding textconverter;
Textconverter = New Utf8encoding ();
MD5 = New Md5cryptoserviceprovider ();
Byte [] Arrpassword = Md5.computehash (textconverter. getbytes (password ));

Strpassword = Convert. tobase64string (arrpassword );

Return Strpassword;
}

 

 

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.