ff(a,b,c,d,mj,s,ti)表示a=b((af(b,c,d)mjti)<<<s)
gg(a,b,c,d,mj,s,ti)表示a=b((ag(b,c,d)mjti)<<<s)
hh(a,b,c,d,mj,s,ti)表示a=b((ah(b,c,d)mjti)<<<s)
ii(a,b,c,d,mj,s,ti)表示a=b((ai(b,c,d)mjti)<<<s)
4)四轮运算
第一轮
a=ff(a,b,c,d,m0,7,0xd76aa478)
b=ff(d,a,b,c,m1,12,0xe8c7b756)
c=ff(c,d,a,b,m2,17,0x242070db)
d=ff(b,c,d,a,m3,22,0xc1bdceee)
a=ff(a,b,c,d,m4,7,0xf57c0faf)
b=ff(d,a,b,c,m5,12,0x4787c62a)
c=ff(c,d,a,b,m6,17,0xa8304613)
d=ff(b,c,d,a,m7,22,0xfd469501)
a=ff(a,b,c,d,m8,7,0x698098d8)
b=ff(d,a,b,c,m9,12,0x8b44f7af)
c=ff(c,d,a,b,m10,17,0xffff5bb1)
d=ff(b,c,d,a,m11,22,0x895cd7be)
a=ff(a,b,c,d,m12,7,0x6b901122)
b=ff(d,a,b,c,m13,12,0xfd987193)
c=ff(c,d,a,b,m14,17,0xa679438e)
d=ff(b,c,d,a,m15,22,0x49b40821)
第二轮
a=gg(a,b,c,d,m1,5,0xf61e2562)
b=gg(d,a,b,c,m6,9,0xc040b340)
c=gg(c,d,a,b,m11,14,0x265e5a51)
d=gg(b,c,d,a,m0,20,0xe9b6c7aa)
a=gg(a,b,c,d,m5,5,0xd62f105d)
b=gg(d,a,b,c,m10,9,0x02441453)
c=gg(c,d,a,b,m15,14,0xd8a1e681)
d=gg(b,c,d,a,m4,20,0xe7d3fbc8)
a=gg(a,b,c,d,m9,5,0x21e1cde6)
b=gg(d,a,b,c,m14,9,0xc33707d6)
c=gg(c,d,a,b,m3,14,0xf4d50d87)
d=gg(b,c,d,a,m8,20,0x455a14ed)
a=gg(a,b,c,d,m13,5,0xa9e3e905)
b=gg(d,a,b,c,m2,9,0xfcefa3f8)
c=gg(c,d,a,b,m7,14,0x676f02d9)
d=gg(b,c,d,a,m12,20,0x8d2a4c8a)
第三轮
a=hh(a,b,c,d,m5,4,0xfffa3942)
b=hh(d,a,b,c,m8,11,0x8771f681)
c=hh(c,d,a,b,m11,16,0x6d9d6122)
d=hh(b,c,d,a,m14,23,0xfde5380c)
a=hh(a,b,c,d,m1,4,0xa4beea44)
b=hh(d,a,b,c,m4,11,0x4bdecfa9)
c=hh(c,d,a,b,m7,16,0xf6bb4b60)
d=hh(b,c,d,a,m10,23,0xbebfbc70)
a=hh(a,b,c,d,m13,4,0x289b7ec6)
b=hh(d,a,b,c,m0,11,0xeaa127fa)
c=hh(c,d,a,b,m3,16,0xd4ef3085)
d=hh(b,c,d,a,m6,23,0x04881d05)
a=hh(a,b,c,d,m9,4,0xd9d4d039)
b=hh(d,a,b,c,m12,11,0xe6db99e5)
c=hh(c,d,a,b,m15,16,0x1fa27cf8)
d=hh(b,c,d,a,m2,23,0xc4ac5665)
第四轮
a=ii(a,b,c,d,m0,6,0xf4292244)
b=ii(d,a,b,c,m7,10,0x432aff97)
c=ii(c,d,a,b,m14,15,0xab9423a7)
d=ii(b,c,d,a,m5,21,0xfc93a039)
a=ii(a,b,c,d,m12,6,0x655b59c3)
b=ii(d,a,b,c,m3,10,0x8f0ccc92)
c=ii(c,d,a,b,m10,15,0xffeff47d)
d=ii(b,c,d,a,m1,21,0x85845dd1)
a=ii(a,b,c,d,m8,6,0x6fa87e4f)
b=ii(d,a,b,c,m15,10,0xfe2ce6e0)
c=ii(c,d,a,b,m6,15,0xa3014314)
d=ii(b,c,d,a,m13,21,0x4e0811a1)
a=ii(a,b,c,d,m4,6,0xf7537e82)
b=ii(d,a,b,c,m11,10,0xbd3af235)
c=ii(c,d,a,b,m2,15,0x2ad7d2bb)
d=ii(b,c,d,a,m9,21,0xeb86d391)
5)每轮循环后,将a,b,c,d分别加上a,b,c,d,然后进入下一循环。
下面是c语言的一个实现:
头文件md5.h
-
#ifndef md5_h
-
#define md5_h
-
-
typedef struct {
-
int state[4]; /* state (abcd) */
-
int count[2]; /* 记录处理bit数 */
-
unsigned char buffer[64]; /* input buffer */
-
} md5_ctx;
-
-
-
#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
-
-
#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)))
-
-
//将x循环右移n位
-
#define rotate_left(x,n) (((x) << (n)) | ((x) >> (32-(n))))
-
-
#define ff(a,b,c,d,x,s,ac) \
-
do { \
-
a = f(b,c,d) x ac; \
-
a = rotate_left(a,s); \
-
a = b; \
-
} while(0)
-
-
#define gg(a,b,c,d,x,s,ac) \
-
do { \
-
a = g(b,c,d) x ac; \
-
a = rotate_left(a,s); \
-
a = b; \
-
} while(0)
-
-
#define hh(a,b,c,d,x,s,ac) \
-
do { \
-
a = h(b,c,d) x ac; \
-
a = rotate_left(a,s); \
-
a = b; \
-
} while(0)
-
-
#define ii(a,b,c,d,x,s,ac) \
-
do { \
-
a = i(b,c,d) x ac; \
-
a = rotate_left(a,s); \
-
a = b; \
-
} while(0)
-
-
-
void md5init(md5_ctx *context);
-
void md5update(md5_ctx *context,unsigned char *input,unsigned int inputlen);
-
void md5final(md5_ctx *context,unsigned char digest[16]);
-
void md5transform(unsigned int state[4],unsigned char block[64]);
-
void md5encode(unsigned char *output,unsigned int *input,unsigned int len);
-
void md5decode(unsigned int *output,unsigned char *input,unsigned int len);
-
-
#endif
主要实现md5.c
-
#include <memory.h>
-
#include "md5.h"
-
-
unsigned char padding[]={ // 16 bety one line, 64 bety in total
-
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //填充数据第一个bit为1,其余为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
-
};
-
-
void md5init(md5_ctx *context)
-
{
-
context->count[0] = 0;
-
context->count[1] = 0;
-
context->state[0] = 0x67452301;
-
context->state[1] = 0xefcdab89;
-
context->state[2] = 0x98badcfe;
-
context->state[3] = 0x10325476;
-
}
-
-
/*
-
* 处理输入数据(同一批数据处理,可以多次调用)
-
*
-
* 1. 按64字节一个分组,循环处理输入数据;
-
* 2. 如果上次处理有剩余数据(不足一个分组的数据被保留),新输入数据拼接其后,组成一个新分组优先处理;
-
* 3. 如果处理后有不足一个分组的数据剩余,那么把这部分数据保留到缓冲区中,待后续数据或最终padding处理。
-
*/
-
void md5update(md5_ctx *context, unsigned char *input, unsigned int inputlen)
-
{
-
unsigned int i = 0;
-
unsigned int index = 0;
-
unsigned int partlen = 0;
-
-
-
index = (context->count[0] >> 3) & 0x3f; // 转换为字节数并对64取模,等价于 (count[0] >> 3) % 64
-
partlen = 64 - index; //待补齐的字节数(上次处理不足一个分组的部分被保留未处理,本次先补齐一个组)
-
-
context->count[0] = inputlen << 3; //新输入bit数
-
-
if(context->count[0] < (inputlen << 3)) //count[0]溢出,记录到count[1]中
-
context->count[1];
-
-
context->count[1] = inputlen >> 29; //新输入bit数溢出,记录到count[2]中
-
-
if(inputlen >= partlen) // 至少可以凑足一个分组
-
{
-
// 优先使用缓冲中的数据拼接一个分组,并处理该分组
-
memcpy(&context->buffer[index],input,partlen);
-
md5transform(context->state,context->buffer);
-
-
// 按64字节一个分组循环处理输入数据
-
for(i = partlen;i64 <= inputlen;i=64)
-
md5transform(context->state,&input[i]);
-
-
index = 0; //缓冲区偏移index置0(缓冲区已经被处理)
-
}
-
else
-
{
-
i = 0;
-
}
-
-
// 将不足一个分组的数据保存到缓冲区中,等待后续数据补齐或最终padding处理
-
memcpy(&context->buffer[index],&input[i],inputlen-i);
-
}
-
-
/*
-
* 处理缓冲区中的剩余数据,输出最终的数据特征码到digest
-
*/
-
void md5final(md5_ctx *context, unsigned char digest[16])
-
{
-
unsigned int index = 0;
-
unsigned int padlen = 0;
-
unsigned char bits[8];
-
-
index = (context->count[0] >> 3) & 0x3f;
-
-
/*
-
* 最后一个分组:(部分原数据) (填充数据) (长度信息[8字节])
-
* 所以剩余部分原始数据长度如果超过56字节,那么填充数据和长度信息在本分组中无法容纳
-
* 那么需要再额外填充一个分组。
-
*/
-
padlen = (index < 56) ? (56-index) : (120-index);
-
md5encode(bits, context->count, 8);
-
md5update(context, padding, padlen);
-
md5update(context, bits, 8);
-
md5encode(digest,context->state,16);
-
}
-
-
/*
-
* 把输入的整形数组转换为字符数组
-
*
-
* todo:
-
* 是否需要考虑大小端?
-
*/
-
void md5encode(unsigned char *output,unsigned int *input,unsigned int len)
-
{
-
unsigned int i = 0,j = 0;
-
while(j < len)
-
{
-
output[j] = input[i] & 0xff;
-
output[j1] = (input[i] >> 8) & 0xff;
-
output[j2] = (input[i] >> 16) & 0xff;
-
output[j3] = (input[i] >> 24) & 0xff;
-
i;
-
j=4;
-
}
-
}
-
-
/*
-
* 把输入的char类型的数据转换为int类型,要求len是4的倍数
-
*
-
* todo:
-
* 是否需要考虑大小端?
-
*/
-
void md5decode(unsigned int *output,unsigned char *input,unsigned int len)
-
{
-
unsigned int i = 0,j = 0;
-
while(j < len)
-
{
-
output[i] = (input[j]) |
-
(input[j1] << 8) |
-
(input[j2] << 16) |
-
(input[j3] << 24);
-
i;
-
j=4;
-
}
-
}
-
-
-
/*
-
* 处理一个分组
-
*/
-
void md5transform(unsigned int state[4],unsigned char block[64])
-
{
-
unsigned int a = state[0];
-
unsigned int b = state[1];
-
unsigned int c = state[2];
-
unsigned int d = state[3];
-
unsigned int x[64];
-
-
md5decode(x,block,64);
-
-
/* round 1*/
-
ff(a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */
-
ff(d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */
-
ff(c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */
-
ff(b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */
-
ff(a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */
-
ff(d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */
-
ff(c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */
-
ff(b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */
-
ff(a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */
-
ff(d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */
-
ff(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
-
ff(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
-
ff(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
-
ff(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
-
ff(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
-
ff(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
-
-
/* round 2 */
-
gg(a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */
-
gg(d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */
-
gg(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
-
gg(b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */
-
gg(a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */
-
gg(d, a, b, c, x[10], 9, 0x2441453); /* 22 */
-
gg(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
-
gg(b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */
-
gg(a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */
-
gg(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
-
gg(c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */
-
gg(b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */
-
gg(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
-
gg(d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */
-
gg(c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */
-
gg(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
-
-
/* round 3 */
-
hh(a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */
-
hh(d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */
-
hh(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
-
hh(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
-
hh(a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */
-
hh(d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */
-
hh(c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */
-
hh(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
-
hh(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
-
hh(d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */
-
hh(c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */
-
hh(b, c, d, a, x[ 6], 23, 0x4881d05); /* 44 */
-
hh(a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */
-
hh(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
-
hh(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
-
hh(b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */
-
-
/* round 4 */
-
ii(a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */
-
ii(d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */
-
ii(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
-
ii(b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */
-
ii(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
-
ii(d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */
-
ii(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
-
ii(b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */
-
ii(a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */
-
ii(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
-
ii(c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */
-
ii(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
-
ii(a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */
-
ii(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
-
ii(c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */
-
ii(b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */
-
-
state[0] = a;
-
state[1] = b;
-
state[2] = c;
-
state[3] = d;
-
}
下面是测试main函数,main.c
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include "md5.h"
-
-
/*
-
md5 test suite:
-
md5 ("") = d41d8cd98f00b204e9800998ecf8427e
-
md5 ("a") = 0cc175b9c0f1b6a831c399e269772661
-
md5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
-
md5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
-
md5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
-
md5 ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789") = d174ab98d277d9f5a5611c2c9f419d9f
-
md5 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = 57edf4a22be3c955ac49da2e2107b67a
-
*/
-
int main(int argc, char *argv[])
-
{
-
int i;
-
unsigned char encrypt[] ="a";
-
unsigned char encrypt2[] ="bc";
-
//unsigned char encrypt[] ="admin";//21232f297a57a5a743894a0e4a801fc3
-
unsigned char decrypt[16];
-
md5_ctx md5;
-
-
md5init(&md5);
-
-
md5update(&md5,encrypt,strlen((char *)encrypt));
-
md5update(&md5,encrypt2,strlen((char *)encrypt2));
-
-
md5final(&md5, decrypt);
-
-
printf("加密前:%s\n加密后:",encrypt);
-
for(i=0;i<16;i)
-
{
-
printf("x",decrypt[i]);
-
}
-
-
getchar();
-
-
return 0;
-
}