#include
#include "md5c.h"unsigned char PADDING[]={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};vo id MD5 Init(MD5_CTX*context)
{cont ext->co unt[0]=0;conte xt->c ount[1]=0;conte xt->state[0]=0 x67452301;conte xt->state[1]=0 xEF CDAB 89;conte xt->s tate[2]=0x98B ADCF E;conte xt->state[3]=0 x 10325476;
}vo id MD5 Update(MD5_CTX*context,unsigne d char*input,uns igned int inputlen){unsigned int i=0,index=0,partlen=0;index=(context->count[0]>>3)&0x3F;partlen=64- index;conte xt->c ount[0]+=inp utle n<<3;if(conte xt->c ount[0]c o unt[1]++;conte xt->count[1]+=inp utle n>>29;if(inputlen>=partlen)
{me mcpy(&conte xt->buffer[inde x],input,p artle n);
MD5 Trans form(co ntext->state,co ntext->buffer);fo r(i=partle n;i+64<=inp utlen;i+=64)
MD5 Tra ns form(co ntext->stat e,&input[i]);ind e x=0;
}else
{i=0;
}memcpy(&context->buffer[index],&input[i],inputlen-i);
}void MD5Final(MD5_CTX*context,unsigned char digest[16])
{uns igned int index=0,padlen=0;uns igned char b its[8];index=(context->count[0]>>3)&0x3F;
padlen=(index<56)?(56-index):(120-index);
MD5 Enc ode(bits,c onte xt->count,8);
MD5Upd ate(c onte xt,PADDING,padle n);
MD5Up date(conte xt,b its,8);
MD 5 Enco de(d igest,conte xt->s tate,16);
}void MD5 Encode(unsigned char *output,unsigned int*input,unsigned int len){unsigned int i=0,j=0;while(j
{outp ut[j]=input[i]&0 xFF;output[j+1]=(inp ut[i]>>8)&0xFF;outp ut[j+2]=(inp ut[i]>>16)&0xFF;outp ut[j+3]=(inp ut[i]>>24)&0xF F;i++;j+=4;
}
}vo id MD5Deco de(unsigne d int*output,unsigned char*input,unsigned int len){uns igne d int i=0,j=0;while(j
{output[i]=(inp ut[j]) |
(inp ut[j+1]<<8) |
(input[j+2]<<16) |
(input[j+3]<<24);i++;j+=4;
}
}vo id MD5 Trans form(unsigned int state[4],uns igned char b lock[64])
{uns igned int a=state[0];uns ig ned int b=state[1];uns igne d int c=state[2];uns i gned int d=state[3];uns ign ed int x[64];
MD5Decode(x,block,64);
FF(a,b,c,d,x[0],7,0xd76aa478);
FF(d,a,b,c,x[ 1], 12,0xe8c7b756);
FF(c,d,a,b,x[2], 17,0x242070db);
FF(b,c,d,a,x[3],22,0xc 1bdceee);
FF(a,b,c,d,x[4],7,0xf57c0faf);
FF(d,a,b,c,x[5], 12,0x4787c62a);
FF(c,d,a,b,x[6], 17,0xa8304613);F F(b,c,d,a,x[7],22,0 x fd469501);FF(a,b,c,d,x[8],7,0x698098d8);FF(d,a,b,c,x[9], 12,0x8b44f7af);FF(c,d,a,b,x[10], 17,0xffff5bb1);FF(b,c,d,a,x[1 1],22,0x895cd7be);FF(a,b,c,d,x[12],7,0x6b901122);FF(d,a,b,c,x[13], 12,0xfd987193);FF(c,d,a,b,x[14], 17,0xa679438e);FF(b,c,d,a,x[15],22,0x49b40821);GG(a,b,c,d,x[ 1],5,0xf61 e2562);GG(d,a,b,c,x[6],9,0xc040b340);GG(c,d,a,b,x[11], 14,0x265e5a51);GG(b,c,d,a,x[0],20,0xe9b6c7aa);GG(a,b,c,d,x[5],5,0xd62 f105d);GG(d,a,b,c,x[10],9, 0 x2441453);GG(c,d,a,b,x[15], 14,0xd8a1 e681);GG(b,c,d,a,x[4],20,0xe7d3fbc8);GG(a,b,c,d,x[9],5,0x21 e 1 cde6);GG(d, a,b,c,x[14],9,0xc33707d6);GG(c,d,a,b,x[3], 14,0xf4d50d87);GG(b,c,d,a,x[8],20,0x455a14 ed);GG(a,b, c,d,x[13],5,0xa9e3e905);GG(d,a,b,c,x[2],9,0xfcefa3 f8);GG(c,d, a,b,x[7], 14,0x676 f02d9);GG(b,c,d,a,x[12],20,0x8 d2 a4c 8 a);HH(a,b,c,d,x[5],4,0xfffa3942);HH(d,a,b, c,x[8], 11,0x8771 f681);HH(c,d,a,b,x[11], 16,0x6d9d6122);HH(b,c,d,a,x[14],23,0xfde5380 c);HH(a,b, c,d,x[ 1],4,0xa4beea44);HH(d,a,b,c,x[4], 11,0x4bdec fa9);HH(c,d,a,b,x[7], 16,0xf6bb 4b60);HH(b,c,d,a,x[10],23,0xbebfbc70);HH(a,b,c,d,x[13],4,0x289b7ec6);HH(d, a,b,c,x[0], 11,0xe a a 127fa);HH(c,d,a,b,x[3], 16,0xd4e f3085);HH(b,c,d,a,x[6],23, 0x4881 d05);HH(a,b,c,d,x[9],4,0xd9d4d039);HH(d,a,b,c,x[12], 11,0xe6db99e5);HH(c,d,a,b,x[15], 16,0x1fa27cf8);HH(b, c,d,a,x[2],23,0xc4 ac5665);
II(a,b,c,d,x[0],6,0xf4292244);
II(d,a,b,c,x[7], 10,0x432aff97);
II(c,d,a,b,x[14], 15,0xab9423a7);
II(b,c,d,a,x[5],21,0xfc93 a039);
II(a,b,c,d,x[12],6,0x655b59c3);
II(d,a,b,c,x[3], 10,0x8 f0ccc92);
II(c,d,a,b,x[10], 15,0xffeff47d);
II(b,c,d,a,x[ 1],21,0x85845dd 1);
II(a,b,c,d,x[ 8],6,0x6fa87e4f);
II(d,a,b,c,x[15], 10,0xfe2ce6e0);
II(c,d,a,b,x[6], 15,0xa3014314);
II(b, c,d,a,x[13],21,0x4e0811a1);
II(a,b,c,d,x[4],6,0xf7537e82);
II(d,a,b,c,x[11], 10,0xbd3af235);
II(c,d,a,b,x[2], 15,0x2 ad7d2bb);
II(b,c,d,a,x[9],21,0xeb86d391);st ate[0]+=a;s tate[1]+=b;s tate[2]+=c;stat e[3]+=d;
}
//下边是头文件
#ifndefMD5_H
#define MD5_H
typedef struct
{uns igned int count[2];uns ign ed int state[4];unsigned char buffer[64];
}MD5_CTX;
#define F(x,y,z) ((x&y) | (~x&z))
#define G(x,y,z) ((x&z) | (y&~z))
#define H(x,y,z) (x^y^z)
#define I(x,y,z) (y^(x|~z))
#define ROTATE_LEFT(x,n) ((x<>(32-n)))
#define FF(a,b,c,d,x,s,ac) {a+=F(b,c,d)+x+ac;a=ROTATE_LEFT(a,s);a+=b;}#define GG(a,b,c,d,x,s,ac) {a+=G(b,c,d)+x+ac;a=RO TATE_LEFT(a,s);a+=b; }
#define HH(a,b,c,d,x,s,ac) {a+=H(b,c,d)+x+ac; a=RO TATE_LEF T(a,s);a+=b; }#define II(a,b,c,d,x,s,ac) {a+=I(b,c,d)+x+ac;a=ROTATE_LEFT(a,s);a+=b; }vo id MD5 Init(MD5_CTX*context);void MD5Update(MD5_CTX*context,unsigned char *input,unsigned int inputlen);void MD5Final(MD5_CTX*context,unsigned char digest[16]);vo id MD 5Trans form(uns igned int state[4],uns igne d char b lo ck[64]);void MD5 Encode(unsigned char *output,unsigned int*input,unsigned int len);void MD5Dec ode(uns igned int*output,uns igned char*input,unsigned int len);#end i f
//调用方法int main(int argc,char*argv[])
{unsigned char encrypt[]="ad";//21232f297a57a5a743894a0e4a801 fc3uns igne d char encrypt1 []="min";unsigned char decrypt[16];
MD5_CTX md5;
MD5 Init(&md5);
MD 5Update(&md5,encrypt,strl en((c har*)encrypt));
MD5Up date(&md5,encrypt1,strlen((char*)encryp t1));
MD5F inal(&md5,decrypt);
}