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

📄 md5hmac.cpp

📁 MD5算法 实现加密解密过程并对正确性设计验证途径。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// Md5HMAC.cpp : Defines the entry point for the console application.
//
// md5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>


#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <windows.h>

#include <LIMITS.H>
#include <malloc.h>
#include "fstream.h"


#ifndef PROTOTYPES 
#define PROTOTYPES 0 
#endif 
typedef unsigned char *POINTER; 
typedef unsigned short int UINT2; 
typedef unsigned long int UINT4; 
#if PROTOTYPES 
#define PROTO_LIST(list) list 
#else 
#define PROTO_LIST(list) () 
#endif 

char* pstroutput=(char *)malloc(1024*1024*100);//记录所有的字符输出
char* pstroutput0=pstroutput;//记下初始位置。

//md5.h

int byteBit[] =
{
	0200, 0100, 040, 020, 010, 04, 02, 01 //128,64,32,16,8,4,2,1以8进制表示的。
}; /* byteBit[] */

#define OUTPUT_HEXA  1 //为1时输出十六进制格式,为0输出二进制格式
void OutputBitOfByte(unsigned char bytec)
{
	
	for(int i=0;i<8;i++) 
	{//printf("%d",((byteBit[i] & bytec)>0 ? 1:0));
		pstroutput+=sprintf(pstroutput,"%d",((byteBit[i] & bytec)>0 ? 1:0));}
	
	//	printf("\n");
}
void OutputBitOfBytes(unsigned char * pbytec,unsigned int &nBytes)
{
	if(!OUTPUT_HEXA){//二进制输出
		for(int i=0;i<nBytes;i++) 
		{
			OutputBitOfByte(*pbytec);pbytec++;
			//printf("\t");
			pstroutput+=sprintf(pstroutput,"\t");
			
		}
	}
	else
	{
		for(int i=0;i<nBytes;i++) 
		{
			//printf("%02X",*pbytec);
			pstroutput+=sprintf(pstroutput,"%02X",*pbytec);
			pbytec++;
			//	printf("\t");
			//	pstroutput+=sprintf(pstroutput,"\t");
			
		}
		
	}
}
unsigned long ByteSwap(unsigned long x)
{
	//一个整型有4个字节,0字节与3字节互换
	char  tmp;
	char  *cp;
	
	cp	  = (char *)&x;
	tmp   = cp[3];
	cp[3] = cp[0];
	cp[0] = tmp;
	//0,1字节互换
	tmp   = cp[2];
	cp[2] = cp[1];
	cp[1] = tmp;
	
	return x;
} /* ByteSwap() */


typedef struct { 
	UINT4 state[4];  //存初始向量128bit      
	UINT4 count[2];      
	unsigned char buffer[64];   //存处理分组512bit                   
} MD5_CTX; 

void MD5Init PROTO_LIST ((MD5_CTX *)); 
void MD5Update PROTO_LIST 
((MD5_CTX *, unsigned char *, unsigned int)); 
void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *)); 

//md5.c
/* bit 0 is left-most in byte */



//定义S盒
//第1轮S盒
#define S11 7 
#define S12 12 
#define S13 17 
#define S14 22 
//第2轮S盒
#define S21 5 
#define S22 9 
#define S23 14 
#define S24 20 
//第3轮S盒
#define S31 4 
#define S32 11 
#define S33 16 
#define S34 23 
//第4轮S盒
#define S41 6 
#define S42 10 
#define S43 15 
#define S44 21 

void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64])); 
void Encode PROTO_LIST 
((unsigned char *, UINT4 *, unsigned int)); 
void Decode PROTO_LIST 
((UINT4 *, unsigned char *, unsigned int)); 
void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int)); 
void MD5_memset PROTO_LIST ((POINTER, int, unsigned int)); 
//填充10000000共512bit
unsigned char PADDING[64] = { 
	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 
}; 

// 定义F G H I 为四个函数 

#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) << (n)) | ((x) >> (32-(n)))) 
unsigned int gnBytes=4;
//#define PRINT(x) {OutputBitOfBytes(unsigned char * (&x),unsigned int &gnBytes);}
//a=a+F(b,c,d)+x+ac							//移位						 //+b
//宏定义相当于地址传递。
#define FF(a, b, c, d, x, s, ac) { (a)+= F ((b), (c), (d)) + (x) + (UINT4)(ac); (a)= ROTATE_LEFT ((a), (s)); (a)+= (b); } 
#define GG(a, b, c, d, x, s, ac) { (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } 
#define HH(a, b, c, d, x, s, ac) { (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } 
#define II(a, b, c, d, x, s, ac) {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } 
void MD5_memcpy (POINTER output, 
				 POINTER input, 
				 unsigned int len) 
				 
{ 
	unsigned int i; 
	
	for (i = 0; i < len; i++) output[i] = input[i]; 
}
//将整型按字符型取出字节,并按内存的原有形式,即反序,
//第一整数为秒钟,第二个分钟,这也是一种反序,
//将字节挨个取出
void Encode (unsigned char *output,
			 UINT4 *input, 
			 unsigned int len)  
{ 
	unsigned int i, j; 
	//将64bit 长度位转换成 字节串;如0x01020304 ;0x05060708将变成"04030201 08070605"
	//刚好是内存中的反序形式
	for (i = 0, j = 0; j < len; i++, j += 4) { 
		output[j] = (unsigned char)(input[i] & 0xff); //个位
		output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); //十位
		output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);//百位 
		output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); //千位
	} 
} 

void Decode (UINT4 *output, 
			 unsigned char *input,
			 unsigned int len ) 
{ 
	unsigned int i, j; 
	//将64字节转化为到16个元素的整数组,按反序还原成U整数再赋值
	for (i = 0, j = 0; j < len; i++, j += 4) 
		output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | 
		(((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24); 
}
void MD5_memset (POINTER output,
				 int value, 
				 unsigned int len) 
{ 
	unsigned int i; 
	
	for (i = 0; i < len; i++) 
		((char *)output)[i] = (char)value; 
}
void printfaint(unsigned int a)
{
	//printf("\n");
	//pstroutput+=sprintf(pstroutput,"\n");
	unsigned int uintTempSwap=ByteSwap(a);
	unsigned int uintTempSawpLen=sizeof(uintTempSwap);
	OutputBitOfBytes((unsigned char *)(&uintTempSwap),uintTempSawpLen);
	//printf("\n");
	pstroutput+=sprintf(pstroutput,"\n");
}
void PRINT(unsigned int a,unsigned int b,unsigned int c,unsigned int d,
		   unsigned int x,const int s,const unsigned int ac,int nround)
{
	//	printf("\n");
	pstroutput+=sprintf(pstroutput,"\n");
	pstroutput+=sprintf(pstroutput,"round \t%d\n",nround);
	//	printf("参数1:\t");
	pstroutput+=sprintf(pstroutput,"参数1:\t\t\t\t\t");
	printfaint(a);
	//	printf("参数2:\t");
	pstroutput+=sprintf(pstroutput,"参数2:\t\t\t\t\t");
	printfaint(b);
	//	printf("参数3:\t");
	pstroutput+=sprintf(pstroutput,"参数3:\t\t\t\t\t");
	printfaint(c);
	//	printf("参数4:\t");
	pstroutput+=sprintf(pstroutput,"参数4:\t\t\t\t\t");
	printfaint(d);
	//	printf("参数x:\t");
	pstroutput+=sprintf(pstroutput,"参数x:\t\t\t\t\t");
	if(nround <17)
	{
		printfaint(x);
		pstroutput+=sprintf(pstroutput,"F(参数2,参数3,参数4):\t\t\t");
		printfaint(F(b,c,d));
		pstroutput+=sprintf(pstroutput,"F(参数2,参数3,参数4)+x:\t\t\t");
		printfaint(F(b,c,d)+x);
		pstroutput+=sprintf(pstroutput,"参数ac:\t\t\t\t\t");
		printfaint(ac);
		pstroutput+=sprintf(pstroutput,"F(参数2,参数3,参数4)+x+ac:\t\t");
		printfaint(F(b,c,d)+x+ac);
		pstroutput+=sprintf(pstroutput,"s:\t");
		pstroutput+=sprintf(pstroutput,"%d\n",s);
		pstroutput+=sprintf(pstroutput,"ROTATE_LEFT(a+F(b,c,d)+x+ac, s):\t");
		printfaint(ROTATE_LEFT(a+F(b,c,d)+x+ac, s));
		pstroutput+=sprintf(pstroutput,"b+ROTATE_LEFT(a+F(b,c,d)+x+ac, s):\t");
		printfaint(b+ROTATE_LEFT(a+F(b,c,d)+x+ac, s));
	}
	else if (nround <33)
	{
		printfaint(x);
		pstroutput+=sprintf(pstroutput,"G(参数2,参数3,参数4):\t\t\t");
		printfaint(G(b,c,d));
		pstroutput+=sprintf(pstroutput,"G(参数2,参数3,参数4)+x:\t\t\t");
		printfaint(G(b,c,d)+x);
		pstroutput+=sprintf(pstroutput,"参数ac:\t\t\t\t\t");
		printfaint(ac);
		pstroutput+=sprintf(pstroutput,"G(参数2,参数3,参数4)+x+ac:\t\t");
		printfaint(G(b,c,d)+x+ac);
		pstroutput+=sprintf(pstroutput,"s:\t");
		pstroutput+=sprintf(pstroutput,"%d\n",s);
		pstroutput+=sprintf(pstroutput,"ROTATE_LEFT(a+G(b,c,d)+x+ac, s):\t");

⌨️ 快捷键说明

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