md5加密算法c语言版

from: http://blog.sina.com.cn/s/blog_693de6100101kcu6.html

注:以下是md5加密算法c语言版(16/32位)
---------------------------------------------------

网上找到的md5函数的实现,包括三个文件,依次如下:

 

头文件 TL_MD5.h

 1 [cpp] 
 2 #ifndef MD5_H  
 3 #define MD5_H  
 4   
 5 typedef struct  
 6 {  
 7     unsigned int count[2];  
 8     unsigned int state[4];  
 9     unsigned char buffer[64];     
10 }MD5_CTX;  
11   
12   
13 #define F(x,y,z) ((x & y) | (~x & z))  
14 #define G(x,y,z) ((x & z) | (y & ~z))  
15 #define H(x,y,z) (x^y^z)  
16 #define I(x,y,z) (y ^ (x | ~z))  
17 #define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))  
18 #define FF(a,b,c,d,x,s,ac) \  
19 { \  
20     a += F(b,c,d) + x + ac; \  
21     a = ROTATE_LEFT(a,s); \  
22     a += b; \  
23 }  
24 #define GG(a,b,c,d,x,s,ac) \  
25 { \  
26     a += G(b,c,d) + x + ac; \  
27     a = ROTATE_LEFT(a,s); \  
28     a += b; \  
29 }  
30 #define HH(a,b,c,d,x,s,ac) \  
31 { \  
32     a += H(b,c,d) + x + ac; \  
33     a = ROTATE_LEFT(a,s); \  
34     a += b; \  
35 }  
36 #define II(a,b,c,d,x,s,ac) \  
37 { \  
38     a += I(b,c,d) + x + ac; \  
39     a = ROTATE_LEFT(a,s); \  
40     a += b; \  
41 }                                              
42 void MD5Init(MD5_CTX *context);  
43 void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen);  
44 void MD5Final(MD5_CTX *context,unsigned char digest[16]);  
45 void MD5Transform(unsigned int state[4],unsigned char block[64]);  
46 void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len);  
47 void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len);  
48   
49 #endif  

C/C++语言实现文件 TL_MD5.c 或者 TL_MD5.cpp

  1 [cpp] 
  2 #include <string.h>
  3 #include "md5.h"  
  4   
  5 unsigned char PADDING[]={0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  
  6 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  
  7 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  
  8 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};  
  9   
 10 void MD5Init(MD5_CTX *context)  
 11 {  
 12     context->count[0] = 0;  
 13     context->count[1] = 0;  
 14     context->state[0] = 0x67452301;  
 15     context->state[1] = 0xEFCDAB89;  
 16     context->state[2] = 0x98BADCFE;  
 17     context->state[3] = 0x10325476;  
 18 }  
 19 void MD5Update(MD5_CTX *context,unsigned char *input,unsigned int inputlen)  
 20 {  
 21     unsigned int i = 0,index = 0,partlen = 0;  
 22     index = (context->count[0] >> 3) & 0x3F;  
 23     partlen = 64 - index;  
 24     context->count[0] += inputlen << 3;  
 25     if(context->count[0] < (inputlen << 3))  
 26         context->count[1]++;  
 27     context->count[1] += inputlen >> 29;  
 28   
 29     if(inputlen >= partlen)  
 30     {  
 31         memcpy(&context->buffer[index],input,partlen);  
 32         MD5Transform(context->state,context->buffer);  
 33         for(i = partlen;i+64 <= inputlen;i+=64)  
 34             MD5Transform(context->state,&input[i]);  
 35         index = 0;          
 36     }    
 37     else  
 38     {  
 39         i = 0;  
 40     }  
 41     memcpy(&context->buffer[index],&input[i],inputlen-i);  
 42 }  
 43 void MD5Final(MD5_CTX *context,unsigned char digest[16])  
 44 {  
 45     unsigned int index = 0,padlen = 0;  
 46     unsigned char bits[8];  
 47     index = (context->count[0] >> 3) & 0x3F;  
 48     padlen = (index < 56)?(56-index):(120-index);  
 49     MD5Encode(bits,context->count,8);  
 50     MD5Update(context,PADDING,padlen);  
 51     MD5Update(context,bits,8);  
 52     MD5Encode(digest,context->state,16);  
 53 }  
 54 void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len)  
 55 {  
 56     unsigned int i = 0,j = 0;  
 57     while(j < len)  
 58     {  
 59         output[j] = input[i] & 0xFF;    
 60         output[j+1] = (input[i] >> 8) & 0xFF;  
 61         output[j+2] = (input[i] >> 16) & 0xFF;  
 62         output[j+3] = (input[i] >> 24) & 0xFF;  
 63         i++;  
 64         j+=4;  
 65     }  
 66 }  
 67 void MD5Decode(unsigned int *output,unsigned char *input,unsigned int len)  
 68 {  
 69     unsigned int i = 0,j = 0;  
 70     while(j < len)  
 71     {  
 72         output[i] = (input[j]) |  
 73             (input[j+1] << 8) |  
 74             (input[j+2] << 16) |  
 75             (input[j+3] << 24);  
 76         i++;  
 77         j+=4;   
 78     }  
 79 }  
 80 void MD5Transform(unsigned int state[4],unsigned char block[64])  
 81 {  
 82     unsigned int a = state[0];  
 83     unsigned int b = state[1];  
 84     unsigned int c = state[2];  
 85     unsigned int d = state[3];  
 86     unsigned int x[64];  
 87     MD5Decode(x,block,64);  
 88     FF(a, b, c, d, x[ 0], 7, 0xd76aa478);   
 89     FF(d, a, b, c, x[ 1], 12, 0xe8c7b756);   
 90     FF(c, d, a, b, x[ 2], 17, 0x242070db);   
 91     FF(b, c, d, a, x[ 3], 22, 0xc1bdceee);   
 92     FF(a, b, c, d, x[ 4], 7, 0xf57c0faf);   
 93     FF(d, a, b, c, x[ 5], 12, 0x4787c62a);   
 94     FF(c, d, a, b, x[ 6], 17, 0xa8304613);   
 95     FF(b, c, d, a, x[ 7], 22, 0xfd469501);   
 96     FF(a, b, c, d, x[ 8], 7, 0x698098d8);   
 97     FF(d, a, b, c, x[ 9], 12, 0x8b44f7af);   
 98     FF(c, d, a, b, x[10], 17, 0xffff5bb1);   
 99     FF(b, c, d, a, x[11], 22, 0x895cd7be);   
100     FF(a, b, c, d, x[12], 7, 0x6b901122);   
101     FF(d, a, b, c, x[13], 12, 0xfd987193);   
102     FF(c, d, a, b, x[14], 17, 0xa679438e);   
103     FF(b, c, d, a, x[15], 22, 0x49b40821);   
104   
105       
106     GG(a, b, c, d, x[ 1], 5, 0xf61e2562);   
107     GG(d, a, b, c, x[ 6], 9, 0xc040b340);   
108     GG(c, d, a, b, x[11], 14, 0x265e5a51);   
109     GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa);   
110     GG(a, b, c, d, x[ 5], 5, 0xd62f105d);   
111     GG(d, a, b, c, x[10], 9,  0x2441453);   
112     GG(c, d, a, b, x[15], 14, 0xd8a1e681);   
113     GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8);   
114     GG(a, b, c, d, x[ 9], 5, 0x21e1cde6);   
115     GG(d, a, b, c, x[14], 9, 0xc33707d6);   
116     GG(c, d, a, b, x[ 3], 14, 0xf4d50d87);   
117     GG(b, c, d, a, x[ 8], 20, 0x455a14ed);   
118     GG(a, b, c, d, x[13], 5, 0xa9e3e905);   
119     GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8);   
120     GG(c, d, a, b, x[ 7], 14, 0x676f02d9);   
121     GG(b, c, d, a, x[12], 20, 0x8d2a4c8a);   
122   
123       
124     HH(a, b, c, d, x[ 5], 4, 0xfffa3942);   
125     HH(d, a, b, c, x[ 8], 11, 0x8771f681);   
126     HH(c, d, a, b, x[11], 16, 0x6d9d6122);   
127     HH(b, c, d, a, x[14], 23, 0xfde5380c);   
128     HH(a, b, c, d, x[ 1], 4, 0xa4beea44);   
129     HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9);   
130     HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60);   
131     HH(b, c, d, a, x[10], 23, 0xbebfbc70);   
132     HH(a, b, c, d, x[13], 4, 0x289b7ec6);   
133     HH(d, a, b, c, x[ 0], 11, 0xeaa127fa);   
134     HH(c, d, a, b, x[ 3], 16, 0xd4ef3085);   
135     HH(b, c, d, a, x[ 6], 23,  0x4881d05);   
136     HH(a, b, c, d, x[ 9], 4, 0xd9d4d039);   
137     HH(d, a, b, c, x[12], 11, 0xe6db99e5);   
138     HH(c, d, a, b, x[15], 16, 0x1fa27cf8);   
139     HH(b, c, d, a, x[ 2], 23, 0xc4ac5665);   
140   
141       
142     II(a, b, c, d, x[ 0], 6, 0xf4292244);   
143     II(d, a, b, c, x[ 7], 10, 0x432aff97);   
144     II(c, d, a, b, x[14], 15, 0xab9423a7);   
145     II(b, c, d, a, x[ 5], 21, 0xfc93a039);   
146     II(a, b, c, d, x[12], 6, 0x655b59c3);   
147     II(d, a, b, c, x[ 3], 10, 0x8f0ccc92);   
148     II(c, d, a, b, x[10], 15, 0xffeff47d);   
149     II(b, c, d, a, x[ 1], 21, 0x85845dd1);   
150     II(a, b, c, d, x[ 8], 6, 0x6fa87e4f);   
151     II(d, a, b, c, x[15], 10, 0xfe2ce6e0);   
152     II(c, d, a, b, x[ 6], 15, 0xa3014314);   
153     II(b, c, d, a, x[13], 21, 0x4e0811a1);   
154     II(a, b, c, d, x[ 4], 6, 0xf7537e82);   
155     II(d, a, b, c, x[11], 10, 0xbd3af235);   
156     II(c, d, a, b, x[ 2], 15, 0x2ad7d2bb);   
157     II(b, c, d, a, x[ 9], 21, 0xeb86d391);   
158     state[0] += a;  
159     state[1] += b;  
160     state[2] += c;  
161     state[3] += d;  
162 }  

md5函数测试代码文件 

 1 [cpp] 
 2 #include   
 3 #include   
 4 #include   
 5 #include "md5.h"  
 6   
 7 int main(int argc, char *argv[])  
 8 {  
 9     MD5_CTX md5;  
10     MD5Init(&md5);           
11     int i;  
12     unsigned char encrypt[] ="admin";//21232f297a57a5a743894a0e4a801fc3  
13     unsigned char decrypt[16];      
14     MD5Update(&md5,encrypt,strlen((char *)encrypt));  
15     MD5Final(&md5,decrypt);   
16     printf("加密前:%s\n加密后16位:",encrypt);  
17     for(i=4;i<12;i++)  
18     {  
19         printf("02x",decrypt[i]);  //02x前需要加上 %  
20     }  
21        
22     printf("\n加密前:%s\n加密后32位:",encrypt);  
23     for(i=0;i<16;i++)  
24     {  
25         printf("02x",decrypt[i]);  //02x前需要加上 %  
26     }  
27   
28     getchar();  
29   
30     return 0;  
31 }  

 

MD5加密常用字符

7a57a5a743894a0e admin(16位MD5小写加密)
972130B75066C825 ADMIN(16位MD5大写加密)
21232f297a57a5a743894a0e4a801fc3 admin(32位MD5小写加密)
73ACD9A5972130B75066C82595A1FAE3 ADMIN(32位MD5大写加密)
49ba59abbe56e057 123456(16位MD5小写加密)
e10adc3949ba59abbe56e057f20f883e 123456(32位MD5小写加密)
469e80d32c0559f8 admin888(16位MD5小写加密)
2299413865C28A35 ADMIN888(16位MD5大写加密)
7fef6171469e80d32c0559f88b377245 admin888(32位MD5小写加密)
A1F63A962299413865C28A3593D359B0 ADMIN888(32位MD5大写加密) 

 

md5知识扫盲

MD5:是一个散列算法,但非加密算法(因为没有密钥)
MD5是一种单向散列函数,单向散列函数的作用是将任何长度的一段数据散列成固定长度。常用于生成消息认证码等等,可以与非对称算法一起用于数字签名。
标准见http://www.ietf.org/rfc/rfc1321.txt

在线MD5计算 http://www.cmd5.com/

posted @ 2015-04-03 10:48  cronus象牙塔  阅读(2704)  评论(0编辑  收藏  举报