Simple Example of MD5 encryption algorithm: md5 Encryption Algorithm

Source: Internet
Author: User
Tags md5 encryption

Simple Example of MD5 encryption algorithm: md5 Encryption Algorithm
A simple example of the MD5 encryption algorithm is now available for websites on the network. If the functions are slightly improved, you must register them, provide personal privacy information such as user name, user password, email, or even phone number, and detailed address before you can enjoy some special information or services provided by the website. To increase security, it is necessary to encrypt private data in the database.
The MD5 Algorithm, namely "Message-Digest Algorithm 5 (Information-Digest Algorithm)", is a one-way encryption Algorithm developed by MD2, MD3, and MD4, that is, the HASH algorithm, R. rivest was developed in the early 1990s S. Currently, MD5 has been widely used in project practice.
There are two types of encryption algorithms: one-way encryption algorithm and two-way encryption algorithm. Bidirectional encryption is the most commonly used encryption algorithm. It encrypts plaintext data that can be directly understood into ciphertext data that cannot be directly understood. When necessary, some algorithms can be used to decrypt the encrypted ciphertext data into the original plaintext data. One-way encryption is the opposite. It can only encrypt the plaintext data, but cannot decrypt the encrypted ciphertext data into the original plaintext data.
The MD5 algorithm is a one-way encryption algorithm. It has two important features: First, any two segments of plaintext data, encrypted ciphertext data must be different, and second, any segment of plaintext data, after encryption, the ciphertext data is always the same.
Create a console application. Smallville is used to encrypt text.

Using System; using System. collections. generic; using System. linq; using System. text; using System. security. cryptography; namespace MD5Test {class Program {// public static string GetMd5Str (string myString) {MD5 MD5 = new MD5CryptoServiceProvider (); // obtain the character array byte [] fromData = System. text. encoding. unicode. getBytes (myString); // get the byte [] toData = md5.ComputeHash (fromData); string byteStr = null; for (int I = 0; I <toData. length; I ++) {// returns the character array to a string in hexadecimal notation without leading "0x" byteStr + = toData [I]. toString ("x");} return byteStr;} static void Main (string [] args) {string md5Str = "smallville"; string md5NewStr = GetMd5Str (md5Str); Console. writeLine (md5NewStr); Console. readLine ();}}}
PS: The System. Security. Cryptography namespace must be referenced. Output result:

The MD5 encryption and decryption algorithm is in urgent need, and the source code implemented by C ++ is highly appreciated.

Do you need code or relevant explanations?

---------------------------------
Code:

Two files:
--------------------------
1. md5.h:

# Pragma once

Typedef unsigned long int UINT32;
Typedef unsigned short int UINT16;

/* MD5 context .*/
Typedef struct {
UINT32 state [4];/* state (ABCD )*/
UINT32 count [2];/* number of bits, modulo 2 ^ 64 (lsb first )*/
Unsigned char buffer [64];/* input buffer */
} MD5_CTX;

Void MD5Init (MD5_CTX *);
Void MD5Update (MD5_CTX *, unsigned char *, unsigned int );
Void MD5Final (unsigned char [16], MD5_CTX *);

--------------------------
2. md5.cpp:

# Include "md5.h"

# Include "memory. h"

# Define S11 7
# Define S12 12
# Define S13 17
# Define S14 22
# Define S21 5
# Define S22 9
# Define S23 14
# Define S24 20
# Define S31 4
# Define S32 11
# Define S33 16
# Define S34 23
# Define S41 6
# Define S42 10
# Define S43 15
# Define S44 21

Static void MD5Transform (UINT32 a [4], unsigned char B [64]);
Static void Encode (unsigned char *, UINT32 *, unsigned int );
Static void Decode (UINT32 *, unsigned char *, unsigned int );

Static unsigned char PADDING [64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

# Define F (x, y, z) (x) & (y) | ((~ X) & (z )))
# Define G (x, y, z) (x) & (z) | (y )&(~ Z )))
# Define H (x, y, z) (x) ^ ...... the remaining full text>

How to decrypt an MD5 Algorithm

This section describes the basic information of the MD5 encryption Algorithm. 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, developed by MD2, MD3, and MD4.
Message-Digest refers to the Hash transformation of a Message, which is to convert a byte string of any length into a long big integer. Note that I use the word "Byte string" instead of "string" because this conversion is only related to the value of the byte and has nothing to do with the character set or encoding method.

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, it is also impossible to convert an MD5 value back to the original string. In terms of mathematical principle, it is because there are infinite numbers of original strings, which is a bit like a mathematical function without an inverse function.

A typical application of MD5 is to generate fingerprint (fingerprint) for a Message (byte string) to prevent "tampering ". For example, you write your statement in a readme.txt file and generate an MD5 value for this readme.txt file and record it. Then you can spread the file to others. If someone else modifies any content in the file, you will find it when re-calculating the MD5 value for this file. If there is another third-party authentication organization, MD5 can also prevent the file author's "credit". This is called a digital signature application.

MD5 is also widely used in encryption and decryption technology. In many operating systems, users' passwords are stored in MD5 values (or similar algorithms, the system calculates the password entered by the user into an MD5 value, and then compares it with the MD5 value saved in the system. The system does not "know" what the user password is.

Some hackers crack this password in a way called "Running Dictionary. There are two ways to get the dictionary: one is the string table that is collected daily and the other is generated by means of arrangement and combination, use the MD5 program to calculate the MD5 value of these dictionary items, and then use the target MD5 value for retrieval in this dictionary.

Even if the maximum length of the password is 8, the password can only contain letters and numbers, a total of 26 + 26 + 10 = 62 characters, the number of items in the dictionary is P (62,1) + P (62,2 ).... + P () is already a very astronomical number. to store this dictionary, you need a TB-level disk group. In addition, this method has a premise, it is only possible to obtain the MD5 value of the password of the target account.

In many e-commerce and community applications, managing users' accounts is the most commonly used basic function. Although many Application servers provide these basic components, however, many application developers prefer to use relational databases to manage users for higher management flexibility. The lazy way is that users' passwords are stored in the database directly after being converted in plain text or simply, therefore, the passwords of these users are not confidential for software developers or system administrators. The purpose of this article is to introduce the implementation of the MD5 Java Bean, at the same time, an example of using MD5 to process the user's Account password is provided. This method makes it impossible for administrators and programmers to see the user's password, even though they can initialize them. However, it is important to protect user password settings.
 

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.