Introduction to the MD5 method and MD5 in C,

Source: Internet
Author: User

Introduction to the MD5 method and MD5 in C,

MD5 Overview:
The full name of MD5 is Message-Digest Algorithm 5, which was invented by MIT's computer science lab and RSA Data Security Inc in Early 1990s and developed by MD2, MD3, and MD4. 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, you cannot convert an MD5 value back to the original string. In terms of mathematical principle, it is because there are infinite numbers of original strings, this is a bit like a mathematical function without an inverse function.

MD5 Algorithm:

Step 1:Add Filling 
Increase the padding value so that the Data Length (in bit) mode is 512 and 448. If the data length is equal to the modulo 512 to 448, increase the number of filled BITs by 512, that is, the number of filled bits is 1-512. The first bit is 1, and all others are 0.
Step 2:Fill Length 
Convert the data length to a 64-bit value. If the length exceeds the range of the Data Length expressed by 64-bit, the value is reserved for the last 64-bit value and is added to the end of the previously filled data, make the final data an integer multiple of bits. That is, the 16-fold integer of 32bit. In RFC1321, 32bit is called a word.
Step 3:Initialize variable:
Four variables, A, B, C, and D, are used. Initialization:
A: 01 23 45 67
B: 89 AB cd ef
C: fe dc ba 98
D: 76 54 32 10
Step 4:Data Processing:
Define four auxiliary functions:
F (X, Y, Z) = XY v not (X) Z
G (X, Y, Z) = XZ v Y not (Z)
H (X, Y, Z) = X xor Y xor Z
I (X, Y, Z) = Y xor (X v not (Z ))
Where: XY indicates bitwise AND, X v Y indicates bitwise OR, not (X) indicates bitwise inversion. Xor indicates bitwise xor.
X, Y, and Z in the function are 32bit. Defines an array to be used: T (I), I value 1-64, T (I) is equal to 4294967296 times of abs (sin (I, I is a radian.
Assume that the data length after the first three steps is 32*16 * Nbit.
Step 5:Output:
The obtained ABCD is the output result of BITs in total. A is low, and D is high.

Implementation of MD5 in ASP.net (C:

    1. // String to be md5 encrypted
    2. String test = "123abc ";
    3. // Obtain the encryption service
    4. System. Security. Cryptography. MD5CryptoServiceProvider md5CSP = new System. Security. Cryptography. MD5CryptoServiceProvider ();
    5. // Obtain the field to be encrypted and convert it to a Byte [] Array
    6. Byte [] testEncrypt = System. Text. Encoding. Unicode. GetBytes (test );
    7. // Encrypt Byte [] Arrays
    8. Byte [] resultEncrypt = md5CSP. ComputeHash (testEncrypt );
    9. // Convert the encrypted array into a field (normal encryption)
    10. String testResult = System. Text. Encoding. Unicode. GetString (resultEncrypt );
    11. // Encrypted as a password
    12. String Encrypt PWD = System. Web. Security. FormsAuthentication. HashPasswordForStoringInConfigFile (test, "MD5 ");

Related Article

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.