⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 md5hmac.cpp

📁 MD5算法 实现加密解密过程并对正确性设计验证途径。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		printfaint(ROTATE_LEFT(a+G(b,c,d)+x+ac, s));
		pstroutput+=sprintf(pstroutput,"b+ROTATE_LEFT(a+G(b,c,d)+x+ac, s):\t");
		printfaint(b+ROTATE_LEFT(a+G(b,c,d)+x+ac, s));
	}
	else if (nround <49)
	{
		printfaint(x);
		pstroutput+=sprintf(pstroutput,"H(参数2,参数3,参数4):\t\t\t");
		printfaint(H(b,c,d));
		pstroutput+=sprintf(pstroutput,"H(参数2,参数3,参数4)+x:\t\t\t");
		printfaint(H(b,c,d)+x);
		pstroutput+=sprintf(pstroutput,"参数ac:\t\t\t\t\t");
		printfaint(ac);
		pstroutput+=sprintf(pstroutput,"H(参数2,参数3,参数4)+x+ac:\t\t");
		printfaint(H(b,c,d)+x+ac);
		pstroutput+=sprintf(pstroutput,"s:\t");
		pstroutput+=sprintf(pstroutput,"%d\n",s);
		pstroutput+=sprintf(pstroutput,"ROTATE_LEFT(a+H(b,c,d)+x+ac, s):\t");
		printfaint(ROTATE_LEFT(a+H(b,c,d)+x+ac, s));
		pstroutput+=sprintf(pstroutput,"b+ROTATE_LEFT(a+H(b,c,d)+x+ac, s):\t");
		printfaint(b+ROTATE_LEFT(a+H(b,c,d)+x+ac, s));
	}
	else 
	{
		printfaint(x);
		pstroutput+=sprintf(pstroutput,"I(参数2,参数3,参数4):\t\t\t");
		printfaint(I(b,c,d));
		pstroutput+=sprintf(pstroutput,"I(参数2,参数3,参数4)+x:\t\t\t");
		printfaint(I(b,c,d)+x);
		pstroutput+=sprintf(pstroutput,"参数ac:\t\t\t\t\t");
		printfaint(ac);
		pstroutput+=sprintf(pstroutput,"I(参数2,参数3,参数4)+x+ac:\t\t");
		printfaint(I(b,c,d)+x+ac);
		pstroutput+=sprintf(pstroutput,"s:\t");
		pstroutput+=sprintf(pstroutput,"%d\n",s);
		pstroutput+=sprintf(pstroutput,"ROTATE_LEFT(a+I(b,c,d)+x+ac, s):\t");
		printfaint(ROTATE_LEFT(a+I(b,c,d)+x+ac, s));
		pstroutput+=sprintf(pstroutput,"b+ROTATE_LEFT(a+I(b,c,d)+x+ac, s):\t");
		printfaint(b+ROTATE_LEFT(a+I(b,c,d)+x+ac, s));
	}
	//	
	
}

void MD5Transform (UINT4 state[4], unsigned char block[64]) 
{ 
	
	UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; 
	//将64字节转化为到16个元素的整数组,按反序还原成U整数再赋值
	pstroutput+=sprintf(pstroutput,"\n");
	pstroutput+=sprintf(pstroutput,"状态1:\t\t\t\t\t");
	printfaint(state[0]);
	pstroutput+=sprintf(pstroutput,"状态2:\t\t\t\t\t");
	printfaint(state[1]);
	pstroutput+=sprintf(pstroutput,"状态3:\t\t\t\t\t");
	printfaint(state[2]);
	pstroutput+=sprintf(pstroutput,"状态4:\t\t\t\t\t");
	printfaint(state[3]);
	Decode (x, block, 64); 
	
	PRINT (a, b, c, d, x[ 0], S11, 0xd76aa478,1); /* 1 */ 	FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ 
	PRINT(d, a, b, c, x[ 1], S12, 0xe8c7b756,2); /* 2 */ 	FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ 
	PRINT(c, d, a, b, x[ 2], S13, 0x242070db,3); /* 3 */ 	FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ 
	PRINT(b, c, d, a, x[ 3], S14, 0xc1bdceee,4); /* 4 */ 	FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ 
	PRINT(a, b, c, d, x[ 4], S11, 0xf57c0faf,5); /* 5 */ 	FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ 
	PRINT(d, a, b, c, x[ 5], S12, 0x4787c62a,6); /* 6 */ 	FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ 
	PRINT(c, d, a, b, x[ 6], S13, 0xa8304613,7); /* 7 */ 	FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ 
	PRINT(b, c, d, a, x[ 7], S14, 0xfd469501,8); /* 8 */ 	FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ 
	PRINT(a, b, c, d, x[ 8], S11, 0x698098d8,9); /* 9 */ 	FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ 
	PRINT(d, a, b, c, x[ 9], S12, 0x8b44f7af,10); /* 10 */ 	FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ 
	PRINT(c, d, a, b, x[10], S13, 0xffff5bb1,11); /* 11 */ 	FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ 
	PRINT(b, c, d, a, x[11], S14, 0x895cd7be,12); /* 12 */ 	FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ 
	PRINT(a, b, c, d, x[12], S11, 0x6b901122,13); /* 13 */ 	FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ 
	PRINT(d, a, b, c, x[13], S12, 0xfd987193,14); /* 14 */ 	FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ 
	PRINT(c, d, a, b, x[14], S13, 0xa679438e,15); /* 15 */ 	FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ 
	PRINT(b, c, d, a, x[15], S14, 0x49b40821,16); /* 16 */ 	FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ 
	
	/* 第二轮循环 */ 
	PRINT(a, b, c, d, x[ 1], S21, 0xf61e2562,17); /* 17 */ 	GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ 
	PRINT(d, a, b, c, x[ 6], S22, 0xc040b340,18); /* 18 */ 	GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ 
	PRINT(c, d, a, b, x[11], S23, 0x265e5a51,19); /* 19 */ 	GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ 
	PRINT(b, c, d, a, x[ 0], S24, 0xe9b6c7aa,20); /* 20 */ 	GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ 
	PRINT(a, b, c, d, x[ 5], S21, 0xd62f105d,21); /* 21 */ 	GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ 
	PRINT(d, a, b, c, x[10], S22,  0x2441453,22); /* 22 */ 	GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */ 
	PRINT(c, d, a, b, x[15], S23, 0xd8a1e681,23); /* 23 */ 	GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ 
	PRINT(b, c, d, a, x[ 4], S24, 0xe7d3fbc8,24); /* 24 */	GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ 
	PRINT(a, b, c, d, x[ 9], S21, 0x21e1cde6,25); /* 25 */ 	GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ 
	PRINT(d, a, b, c, x[14], S22, 0xc33707d6,26); /* 26 */ 	GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ 
	PRINT(c, d, a, b, x[ 3], S23, 0xf4d50d87,27); /* 27 */ 	GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ 
	PRINT(b, c, d, a, x[ 8], S24, 0x455a14ed,28); /* 28 */ 	GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ 
	PRINT(a, b, c, d, x[13], S21, 0xa9e3e905,29); /* 29 */ 	GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ 
	PRINT(d, a, b, c, x[ 2], S22, 0xfcefa3f8,30); /* 30 */ 	GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ 
	PRINT(c, d, a, b, x[ 7], S23, 0x676f02d9,31); /* 31 */ 	GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ 
	PRINT(b, c, d, a, x[12], S24, 0x8d2a4c8a,32); /* 32 */ 	GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ 
	
	/* 第三轮循环 */ 
	PRINT(a, b, c, d, x[ 5], S31, 0xfffa3942,33); /* 33 */ 	HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ 
	PRINT(d, a, b, c, x[ 8], S32, 0x8771f681,34); /* 34 */ 	HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ 
	PRINT(c, d, a, b, x[11], S33, 0x6d9d6122,35); /* 35 */ 	HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ 
	PRINT(b, c, d, a, x[ 14], S34, 0xfde5380c,36); /* 36 */ HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ 
	PRINT(a, b, c, d, x[ 1], S31, 0xa4beea44,37); /* 37 */ 	HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ 
	PRINT(d, a, b, c, x[ 4], S32, 0x4bdecfa9,38); /* 38 */ 	HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ 
	PRINT(c, d, a, b, x[ 7], S33, 0xf6bb4b60,39); /* 39 */ 	HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ 
	PRINT(b, c, d, a, x[10], S34, 0xbebfbc70,40);; /* 40 */ HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ 
	PRINT(a, b, c, d, x[13], S31, 0x289b7ec6,41);; /* 41 */ HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ 
	PRINT(d, a, b, c, x[ 0], S32, 0xeaa127fa,42);; /* 42 */ HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ 
	PRINT(c, d, a, b, x[ 3], S33, 0xd4ef3085,43);; /* 43 */ HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ 
	PRINT(b, c, d, a, x[ 6], S34,  0x4881d05,44);; /* 44 */ HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */ 
	PRINT(a, b, c, d, x[ 9], S31, 0xd9d4d039,45);; /* 45 */ HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ 
	PRINT(d, a, b, c, x[12], S32, 0xe6db99e5,46);; /* 46 */ HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ 
	PRINT(c, d, a, b, x[15], S33, 0x1fa27cf8,47);; /* 47 */ HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ 
	PRINT(b, c, d, a, x[ 2], S34, 0xc4ac5665,48);; /* 48 */ HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ 
	
	/* 第四轮循环 */ 
	PRINT(a, b, c, d, x[ 0], S41, 0xf4292244,49);; /* 49 */ 	II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ 
	PRINT(d, a, b, c, x[ 7], S42, 0x432aff97,50);; /* 50 */ 	II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ 
	PRINT(c, d, a, b, x[14], S43, 0xab9423a7,51);; /* 51 */ 	II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ 
	PRINT(b, c, d, a, x[ 5], S44, 0xfc93a039,52);; /* 52 */ 	II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ 
	PRINT(a, b, c, d, x[12], S41, 0x655b59c3,53);; /* 53 */ 	II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ 
	PRINT(d, a, b, c, x[ 3], S42, 0x8f0ccc92,54);; /* 54 */ 	II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ 
	PRINT(c, d, a, b, x[10], S43, 0xffeff47d,55);; /* 55 */ 	II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ 
	PRINT(b, c, d, a, x[ 1], S44, 0x85845dd1,56);; /* 56 */ 	II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ 
	PRINT(a, b, c, d, x[ 8], S41, 0x6fa87e4f,57);; /* 57 */ 	II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ 
	PRINT(d, a, b, c, x[15], S42, 0xfe2ce6e0,58);; /* 58 */ 	II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ 
	PRINT(c, d, a, b, x[ 6], S43, 0xa3014314,59);; /* 59 */ 	II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ 
	PRINT(b, c, d, a, x[13], S44, 0x4e0811a1,60);; /* 60 */ 	II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ 
	PRINT(a, b, c, d, x[ 4], S41, 0xf7537e82,61);; /* 61 */ 	II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ 
	PRINT(d, a, b, c, x[11], S42, 0xbd3af235,62);; /* 62 */ 	II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ 
	PRINT(c, d, a, b, x[ 2], S43, 0x2ad7d2bb,63);; /* 63 */ 	II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ 
	PRINT(b, c, d, a, x[ 9], S44, 0xeb86d391,64);; /* 64 */ 	II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ 
	
	pstroutput+=sprintf(pstroutput,"\n");
	pstroutput+=sprintf(pstroutput,"a:\t\t\t\t\t");
	printfaint(a);
	pstroutput+=sprintf(pstroutput,"b:\t\t\t\t\t");
	printfaint(b);
	pstroutput+=sprintf(pstroutput,"c:\t\t\t\t\t");
	printfaint(c);
	pstroutput+=sprintf(pstroutput,"d:\t\t\t\t\t");
	printfaint(d);
	state[0] += a; 
	state[1] += b; 
	state[2] += c; 
	state[3] += d; 
	pstroutput+=sprintf(pstroutput,"\n");
	pstroutput+=sprintf(pstroutput,"状态0=状态0+a:\t\t\t\t");
	printfaint(state[0]);
	pstroutput+=sprintf(pstroutput,"状态1=状态1+b:\t\t\t\t");
	printfaint(state[1]);
	pstroutput+=sprintf(pstroutput,"状态2=状态3+c:\t\t\t\t");
	printfaint(state[2]);
	pstroutput+=sprintf(pstroutput,"状态3=状态3+d:\t\t\t\t");
	printfaint(state[3]);//16个素的整数组为重置零	
	MD5_memset ((POINTER)x, 0, sizeof (x)); 
	
} 

//开始进行MD5计算 
void MD5Init (MD5_CTX *context ) 
{ 
	context->count[0] = context->count[1] = 0; //两计数器者初化0 64	位长度位
	//在这里定义四个常数,也就是我们刚才讲到的四个特征数. 
	
	context->state[0] = 0x67452301; 
	context->state[1] = 0xefcdab89; 
	context->state[2] = 0x98badcfe; 
	context->state[3] = 0x10325476; 
	for(int i=0 ;i<4;i++)
	{
		unsigned int uintTempSwap=ByteSwap(context->state[i]);
		unsigned int uintTempSawpLen=sizeof(uintTempSwap);
		//		printf("context->state[%d]:\t",i);
		pstroutput+=sprintf(pstroutput,"\n\t\t");
		pstroutput+=sprintf(pstroutput,"context->state[%d]:\t",i);
		
		OutputBitOfBytes((unsigned char *)(&uintTempSwap),uintTempSawpLen);
		//printf("\n");
	}
} 

void MD5Update (MD5_CTX *context,                                
				unsigned char *input,                           
				unsigned int inputLen ) //所以实际上可哈希长度2^32-1字节
				//2^40bits
                
{ 
	unsigned int i, index, nRequireLen; 
	//count[0]是低位,index是context->buffer的当前指针,指向字节>>3是/8将位转为字节
	index = (unsigned int)((context->count[0] >> 3) & 0x3F); 
	// index:0x3F=00111111=63 
	//index:& 0x3F=%64 模运算的另种表示
	// index:首次调用时为0 
	//	printf("index:\t %d\n",index);
	pstroutput+=sprintf(pstroutput,"\nindex:\t %d\n",index);
	
	//下句好象初看上去恒不成立,count[0]>=0 实际上加法运算含%2^32=0x 1 0000 0000
	//0x2 000 0001=2^29+1=inputLen;inputLen << 3=8(*2^3 %2^32);count[0]=8 ;
	//inputLen >>29 =1(/2^29 %2^32);count[1]=1;长度位即此两值;
	//实际上前者位数模2^32的余数。字节数/2^29的倍数;两者并未真实反映实长;
	//大于2^29部分去了,再右移3位得余数
	//count[1]>0只能说明inputLen >2^29=512MB 左移面9位得倍数
	//一个数乘8 模2^32 后
	//在C;C++实现时不超2^32能反映真实长度
	//context->count[0]=0 第一次调用恒不成立;
	//第二次调用时;inputLen<64 (inputLen<<3 ) <512
	//要count[0]+(inputLen<<3 ) < (inputLen<<3 ) 只有 2^32-count[0] <512 
	//即count[0]>0xFFFF FE00
	//这时记录长度已无意义
	context->count[0] += ((UINT4)inputLen << 3);
	//twx注释
	/*	if (
	(	context->count[0] += ((UINT4)inputLen << 3)	) < ((UINT4)inputLen << 3)) 
	context->count[1]++; 
	*/
	//context->count[0]首次调用时:如果inputLen<=2^29则记录了位数
	//附加调用时,长度位已编码固定了,也累积记录了全部位数
	//context->count[0] +=(inputLen*2^3)%2^32 以以位的形式来记录
	//count[0]是低位
	//	printf("context->count[0]:\t %d bits \n",context->count[0]);
	pstroutput+=sprintf(pstroutput,"context->count[0]:\t %d bits \n",context->count[0]);
	//	printf("context->count[1]:\t %d\n",context->count[1]);
	pstroutput+=sprintf(pstroutput,"context->count[1]:\t %d\n",context->count[1]);
	
	context->count[1] += ((UINT4)inputLen >> 29); //context->count[1] 存字数 量纲2^29
	//隐含%2^32
	//	printf("context->count[1]:\t %d\n",context->count[1]);
	pstroutput+=sprintf(pstroutput,"context->count[1]:\t %d\n",context->count[1]);
	
	nRequireLen = 64 - index; //附加位时调用,表示还要nRequireLen字节才能构成一分组,
	//	printf("nRequireLen = 64 - index:\t %d\n",nRequireLen);
	pstroutput+=sprintf(pstroutput,"nRequireLen = 64 - index:\t %d\n",nRequireLen);
	//	printf("inputLen :\t %d\n",inputLen);	
	pstroutput+=sprintf(pstroutput,"inputLen :\t %d\n",inputLen);
	if (inputLen >= nRequireLen) { //如果输入不比需要的小,则能划成分组 
		//对"abc"或不足分组时进不来,
		//从输入中复制所需长度到缓存,
		//		printf("inputLen >= nRequireLen is true \n");
		pstroutput+=sprintf(pstroutput,"inputLen >= nRequireLen is true \n");
		MD5_memcpy((POINTER)&context->buffer[index], (POINTER)input, nRequireLen); 
		for(int k=0 ;k<64;k++)
		{
			//	unsigned int uintTempSwap=ByteSwap(context->state[i]);
			if((k%2==0) ){// printf("\n");
				pstroutput+=sprintf(pstroutput,"\n\t\t\t\t\t");}
			
			unsigned int uintTempSawpLen=sizeof(context->buffer[k]);
			//	pstroutput+=sprintf(pstroutput,);
			//			printf("buffer[%d]:\t",k);
			pstroutput+=sprintf(pstroutput,"buffer[%d]:\t",k);
			OutputBitOfBytes((unsigned char *)(&context->buffer[k]),uintTempSawpLen);
		}
		//MD5变换
		MD5Transform (context->state, context->buffer); 
		//输入长度如比所需要长,则继续处理下分组
		//i为长消息中的位置64k k>=1
		for (i = nRequireLen; i + 63 < inputLen; i += 64) 
			MD5Transform (context->state, &input[i]); 
		
		index = 0; 
	} 
	else //不够一个分组 //i为长消息中的位置
	{
		//	printf("inputLen >= nRequireLen is False \n");
		pstroutput+=sprintf(pstroutput,"inputLen >= nRequireLen is False \n");
		i = 0; 
	}
	
	//附加调用时,将输入累积到分组中的第buffer[index]位置
	//首次调用时,将不足一分组的字节累累积到缓存
	MD5_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i], 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -