MD5 Algorithm Implementation

Source: Internet
Author: User
Tags gz file

What is MD5??? The full name of---MD5 is message-digest algorithm 5

A typical application of MD5 is to generate an informational digest (message-digest) of a piece of information (message) to prevent tampering. For example, there are a lot of software under Unix that have a file name with the same filename and file extension. MD5 in this file, usually with a single line of text, roughly structured like this:

MD5 (tanajiya.tar.gz) = 0ca175b9c0f726a831d895e269332461

This is the digital signature of the tanajiya.tar.gz file. MD5 the entire file as a large text message, through its irreversible string transform algorithm, produces this unique MD5 message digest. If in the process of propagating this file later, regardless of the content of the file has changed in any form (including human modification or transmission error caused by the line instability during the download), as long as you re-calculate the file MD5, you will find that the information digest is not the same, so you can be sure that you get only an incorrect file. If there is a third-party certification body, with MD5 can also prevent the file author's "Repudiation", which is called Digital signature application.

MD5 is also widely used in encryption and decryption techniques. For example, in a UNIX system, a user's password is encrypted and stored in the file system using MD5 (or other similar algorithms). When the user logs in, the system calculates the password entered by the user as a MD5 value, and then compares the MD5 value stored in the file system to determine if the password entered is correct. Through such steps, the system can determine the legality of the user login system without knowing the user's password. This will not only prevent users ' passwords from being known to users with system administrator privileges, but also increase the difficulty of password cracking to a certain extent.

/*

Md5.c

This was for Encryt by Md5,just Simple implement it.

Comments: Can be used, and Baidu Encyclopedia of the final results of the same

Source: http://www.linuxidc.com/Linux/2012-07/65941.htm

Date: 2014.12.19

Principle: 1 padding: Populating data as n*512+448 (i.e. n*64+56)

2 algorithm calculation, the a->a,a is calculated by Ff,gg,hh,ii, a=a+a

3 Generate a 128-bit encryption algorithm for each block cascade that is calculated

*/

#include <stdio.h>

#include <string.h>

#include "md5.h" header file has been added to the program

#ifndef Md5_forencrpty_h

#define Md5_forencrpty_h

/*this is only 32bit*/

typedef unsigned int md5_int;

struct MD5_STRUCT

{

Md5_int A;

Md5_int B;

Md5_int C;

Md5_int D;

Md5_int Lenbuf;

Char buffer[128];

};

void Md5_init (struct md5_struct *ctx,char * buffer);

void md5_process (struct md5_struct * ctx);

char * Md5_fini (struct md5_struct *ctx,void *rebuf);

void Md5_buffer_full (struct md5_struct * ctx);

void Md5_print (struct md5_struct * ctx);

#endif

struct Md5_struct ctx;

/*

extern void Md5_init (struct md5_struct *ctx,char * buffer);

extern void md5_process (struct md5_struct * ctx);

extern char * Md5_fini (struct md5_struct *ctx,void * rebuf);

extern void Md5_buffer_full (struct md5_struct * ctx);

extern void Md5_print (struct md5_struct * ctx);

*/

/* Set data for fill buffer */

Md5_int fullbuffer[64]={0x80,0};

Md5_int m[16]={0,0};

/*

For Loop a\b\c\d ways

*/

#define F (x, Y, Z) (((&)) | ( (~x) & (z)))

#define G (z) ((x) & (z)) | ( (y) & (~z)))

#define H (x, Y, z) (() ^ (y) ^ (z))

#define I (x, z) ((y) ^ (() | ( ~Z)))

#define ROT (X,s) (x= (x<<s) | ( X>> (32-s)))

#define FF (a,b,c,d,j,s,t) {a=a+ (F (b,c,d) +m[j]+t); ROT (a,s); a=a+b;}

#define GG (a,b,c,d,j,s,t) {a=a+ (G (b,c,d) +m[j]+t); ROT (a,s); a=a+b;}

#define HH (a,b,c,d,j,s,t) {a=a+ (H (b,c,d) +m[j]+t); ROT (a,s); a=a+b;}

#define II (a,b,c,d,j,s,t) {a=a+ (I (b,c,d) +m[j]+t); ROT (a,s); a=a+b;}

/*

Init md5_struct CTX

*/

void Md5_init (struct md5_struct *ctx,char * buffer)

{

ctx->a=0x67452301;//a four-word buffer (a,b,c,d) to calculate the message digest

ctx->b=0xefcdab89;

ctx->c=0x98badcfe;

ctx->d=0x10325476;

Ctx->lenbuf=strlen (buffer);

memcpy (CTX->BUFFER,BUFFER,CTX->LENBUF);

memcpy (Void*dest,constvoid*src,size_tcount)

}

/*

Print CTX ' s message

*/

void Md5_print (struct md5_struct *ctx)

{

printf ("******************************\n");

printf ("ctx->a:%x\n", ctx->a);

printf ("ctx->b:%x\n", ctx->b);

printf ("ctx->c:%x\n", ctx->c);

printf ("ctx->d:%x\n", ctx->d);

printf ("ctx->lenbuf:%d\n", ctx->lenbuf);

printf ("ctx->buffer:%s\n", Ctx->buffer);

printf ("\n\r************print******************\n");

}

/*

Fill buffer to mod%64

*/

void Md5_buffer_full (struct md5_struct *ctx)

{

Md5_int sizebyte[2]={0,0};

Md5_int len=ctx->lenbuf;

Md5_int byte=len>56? (64-(len-56)): 56-len;

memcpy (&ctx->buffer[len],fullbuffer,byte);

sizebyte[0]+= (ctx->lenbuf<<3);

if (SIZEBYTE[0]<CTX->LENBUF)

sizebyte[1]++;

memcpy (&ctx->buffer[len+byte],&sizebyte,sizeof (sizebyte));

}

/*

Deal message

*/

void md5_process (struct md5_struct *ctx)

{

int i=0;

Int J;

Md5_int a=ctx->a;

Md5_int b=ctx->b;

Md5_int c=ctx->c;

Md5_int d=ctx->d;

for (i=0;i<=ctx->lenbuf;i+=64)//loop time

{

memcpy (m,ctx->buffer,sizeof (md5_int) *16);

/* Round 1 */

FF (a,b,c,d, 0, 7,0xd76aa478);

FF (D,a,b,c, 1,12,0xe8c7b756);

FF (C,d,a,b, 2,17,0x242070db);

FF (B,c,d,a, 3,22,0xc1bdceee);

FF (A,b,c,d, 4, 7,0XF57C0FAF);

FF (D,a,b,c, 5,12,0x4787c62a);

FF (C,d,a,b, 6,17,0xa8304613);

FF (B,c,d,a, 7,22,0xfd469501);

FF (A,b,c,d, 8, 7,0X698098D8);

FF (D,a,b,c, 9,12,0X8B44F7AF);

FF (C,D,A,B,10,17,0XFFFF5BB1);

FF (B,C,D,A,11,22,0X895CD7BE);

FF (a,b,c,d,12, 7,0x6b901122);

FF (d,a,b,c,13,12,0xfd987193);

FF (c,d,a,b,14,17,0xa679438e);

FF (b,c,d,a,15,22,0x49b40821);

/* Round 2 */

GG (a,b,c,d, 1, 5,0xf61e2562);

GG (D,a,b,c, 6, 9,0xc040b340);

GG (C,D,A,B,11,14,0X265E5A51);

GG (B,c,d,a, 0,20,0XE9B6C7AA);

GG (A,b,c,d, 5, 5,0xd62f105d);

GG (d,a,b,c,10, 9,0x02441453);

GG (c,d,a,b,15,14,0xd8a1e681);

GG (B,c,d,a, 4,20,0XE7D3FBC8);

GG (A,b,c,d, 9, 5,0x21e1cde6);

GG (d,a,b,c,14, 9,0XC33707D6);

GG (C,d,a,b, 3,14,0xf4d50d87);

GG (B,c,d,a, 8,20,0x455a14ed);

GG (a,b,c,d,13, 5,0xa9e3e905);

GG (D,a,b,c, 2, 9,0XFCEFA3F8);

GG (C,d,a,b, 7,14,0X676F02D9);

GG (B,C,D,A,12,20,0X8D2A4C8A);

/* Round 3 */

HH (A,b,c,d, 5, 4,0xfffa3942);

HH (D,a,b,c, 8,11,0x8771f681);

HH (c,d,a,b,11,16,0x6d9d6122);

HH (b,c,d,a,14,23,0xfde5380c);

HH (a,b,c,d, 1, 4,0xa4beea44);

HH (D,a,b,c, 4,11,0x4bdecfa9);

HH (C,d,a,b, 7,16,0xf6bb4b60);

HH (B,C,D,A,10,23,0XBEBFBC70);

HH (a,b,c,d,13, 4,0X289B7EC6);

HH (D,a,b,c, 0,11,0XEAA127FA);

HH (C,d,a,b, 3,16,0xd4ef3085);

HH (B,c,d,a, 6,23,0X04881D05);

HH (A,b,c,d, 9, 4,0xd9d4d039);

HH (D,A,B,C,12,11,0XE6DB99E5);

HH (C,D,A,B,15,16,0X1FA27CF8);

HH (B,c,d,a, 2,23,0xc4ac5665);

/* Round 4 */

II (a,b,c,d, 0, 6,0xf4292244);

II (D,a,b,c, 7,10,0X432AFF97);

II (C,D,A,B,14,15,0XAB9423A7);

II (B,c,d,a, 5,21,0xfc93a039);

II (a,b,c,d,12, 6,0X655B59C3);

II (D,a,b,c, 3,10,0x8f0ccc92);

II (c,d,a,b,10,15,0xffeff47d);

II (B,c,d,a, 1,21,0X85845DD1);

II (A,b,c,d, 8, 6,0x6fa87e4f);

II (D,A,B,C,15,10,0XFE2CE6E0);

II (C,d,a,b, 6,15,0xa3014314);

II (B,C,D,A,13,21,0X4E0811A1);

II (A,b,c,d, 4, 6,0xf7537e82);

II (d,a,b,c,11,10,0xbd3af235);

II (C,d,a,b, 2,15,0X2AD7D2BB);

II (B,c,d,a, 9,21,0xeb86d391);

ctx->a+=a;

ctx->b+=b;

ctx->c+=c;

ctx->d+=d;

}

}

/*

Store result

*/

char * Md5_fini (struct md5_struct *ctx,void *rebuf)

{

int i=0;

memset (rebuf,0,16);

memcpy (& (unsigned char *) rebuf) [0],&ctx->a,sizeof (Ctx->a));

memcpy (& (unsigned char *) rebuf) [4],&ctx->b,sizeof (Ctx->b));

memcpy (& (unsigned char *) rebuf) [8],&ctx->c,sizeof (Ctx->c));

memcpy (& (unsigned char *) rebuf) [12],&ctx->d,sizeof (Ctx->d));

/* Print MD5 result */

Md5_print (CTX);

printf ("MD5:");

while (I<16)

{

printf ("%02x", ((unsigned char *) rebuf) [i++]);

}

printf ("\n*****nj****************************\n");

return rebuf;

}

int main (int argc,char * * argv)

{

unsigned char rebuf[16];

unsigned char message[64];

printf ("Please enter your message for encrypt:");

scanf ("%s", message);

Md5_init (&ctx,message);

Md5_buffer_full (&CTX);

Md5_process (&CTX);

Md5_fini (&CTX,REBUF);

return 0;

}

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.