您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 国内外标准规范 > c语言实现MD5文件校验
#includewindows.h#includewincon.h#includeconio.h#includestdio.h#includestring.h#includestdlib.hCONSOLE_SCREEN_BUFFER_INFOcsbiInfo;externWINBASEAPIHWNDWINAPIGetConsoleWindow();#defineF(x,y,z)(((x)&(y))|((~x)&(z)))#defineG(x,y,z)(((x)&(z))|((y)&(~z)))#defineH(x,y,z)((x)^(y)^(z))#defineI(x,y,z)((y)^((x)|(~z)))#defineRL(x,y)(((x)(y))|((x)(32-(y))))//x向左循环移y位#definePP(x)(x24)|((x8)&0xff0000)|((x8)&0xff00)|(x24)//将x高低位互换,例如PP(aabbccdd)=ddccbbaa#defineFF(a,b,c,d,x,s,ac)a=b+(RL((a+F(b,c,d)+x+ac),s))#defineGG(a,b,c,d,x,s,ac)a=b+(RL((a+G(b,c,d)+x+ac),s))#defineHH(a,b,c,d,x,s,ac)a=b+(RL((a+H(b,c,d)+x+ac),s))#defineII(a,b,c,d,x,s,ac)a=b+(RL((a+I(b,c,d)+x+ac),s))unsignedA,B,C,D,a,b,c,d,i,len,flen[2],x[16];//i临时变量,len文件长,flen[2]为位二进制表示的文件初始长度//x[16]intfile_choose=0;//文件导入初始状态//全局变量charfilename2[30]=MD5_result.txt;//默认文件位置voidmenu();voidclear();voidjs();voidmd5();voidsave();charfilename[100];//文件名FILE*fp;intmain(intargc,char*argv[]){intd1,d2,d,x,xy=0,i,k=0,s[100],ch,dir=40;structre{inttop;intleft;intright;intbottom;}rt[7];RECTrect;HWNDhwnd;//窗口句柄HDChdc,dc;//画图设备HBRUSHhbr,hbr2;COORDpos3_1={1,4},pos2_1={1,4},pos2_2={1,5},pos2_3={1,6},pos2_4={1,7};INPUT_RECORDmouseRec;DWORDstate=0,res=0;POINTpt;HPENpn;HANDLEhOut,hIn;COLORREFyellow,red;yellow=RGB(0,0,255);red=RGB(0,255,0);hwnd=GetConsoleWindow();hdc=GetDC(hwnd);hbr=CreateSolidBrush(red);dc=GetDC(hwnd);SelectObject(dc,hbr);for(rt[1].left=150,i=2;i=3;i++)rt[i].left=rt[i-1].left+110;for(rt[1].right=210,i=2;i=3;i++)rt[i].right=rt[i-1].right+110;for(i=0;i=3;i++)rt[i].top=40;for(i=0;i=3;i++)rt[i].bottom=60;hOut=GetStdHandle(STD_OUTPUT_HANDLE);hIn=GetStdHandle(STD_INPUT_HANDLE);pn=CreatePen(PS_SOLID,1,RGB(0,255,0));GetClientRect(hwnd,&rt);SelectObject(hdc,pn);SetTextColor(hdc,RGB(255,255,0));SetBkMode(hdc,TRANSPARENT);MoveToEx(hdc,40,35,NULL);LineTo(hdc,540,35);hbr=CreateSolidBrush(yellow);SelectObject(dc,hbr);for(i=1;i=6;i++)Rectangle(dc,rt[i].left,rt[i].top,rt[i].right,rt[i].bottom);SetTextColor(hdc,RGB(255,0,0));TextOut(hdc,250,10,TEXT(文件完整校验),12);//输出文本GetClientRect(hwnd,&rt);SelectObject(hdc,pn);//选画笔SetTextColor(hdc,RGB(0,255,255));////设颜色TextOut(hdc,150,40,TEXT(导入文件),8);//输出文本TextOut(hdc,260,40,TEXT(存储MD5),8);//输出文本TextOut(hdc,370,40,TEXT(退出),8);//输出文本GetClientRect(hwnd,&rt);//得到控制台客户区的大小SelectObject(hdc,pn);//选画笔SetTextColor(hdc,RGB(0,255,0));////设颜色TextOut(hdc,460,370,TEXT(CopyRight:小帅哥),17);//输出文本ShowWindow(hwnd,SW_SHOW);for(;;)//循环{//读取输入的信息ReadConsoleInput(hIn,&mouseRec,1,&res);Sleep(100);if(mouseRec.EventType==MOUSE_EVENT){if(mouseRec.Event.MouseEvent.dwButtonState==FROM_LEFT_1ST_BUTTON_PRESSED){GetCursorPos(&pt);ScreenToClient(hwnd,&pt);if(pt.xrt[1].left&&pt.xrt[1].right&&pt.yrt[1].top&&pt.yrt[1].bottom){SetConsoleCursorPosition(hOut,pos3_1);clear();SetConsoleCursorPosition(hOut,pos3_1);if(k15&&k=9)k++;elsek=9;SetConsoleTextAttribute(hOut,k);if(k%2==0)hbr=CreateSolidBrush(RGB(k*5,0,k*10));elsehbr=CreateSolidBrush(RGB(0,k*10,0));dc=GetDC(hwnd);//画刷SelectObject(dc,hbr);Rectangle(dc,0,60,650,350);js();printf(计算完成!);continue;}elseif(pt.xrt[2].left&&pt.xrt[2].right&&pt.yrt[2].top&&pt.yrt[2].bottom){SetConsoleCursorPosition(hOut,pos3_1);clear();SetConsoleCursorPosition(hOut,pos3_1);if(k15&&k=9)k++;elsek=9;SetConsoleTextAttribute(hOut,k);if(k%2==0)hbr=CreateSolidBrush(RGB(0,0,k*10));elsehbr=CreateSolidBrush(RGB(k*10,0,0));dc=GetDC(hwnd);//画刷SelectObject(dc,hbr);Rectangle(dc,0,60,650,350);save();printf(存储完成!);continue;}elseif(pt.xrt[3].left&&pt.xrt[3].right&&pt.yrt[3].top&&pt.yrt[3].bottom){break;}};}}ReleaseDC(hwnd,hdc);return0;}voidclear(){inti;HANDLEhOut;COORDpos={1,4};SetConsoleCursorPosition(hOut,pos);//设置光标位置for(i=0;i=15;i++)printf(\n);}voidjs(){//while(1)//{printf(请键入文件位置:);fflush(stdin);gets(filename);//用get函数,避免scanf以空格分割数据,if(filename[0]==34)filename[strlen(filename)-1]=0,strcpy(filename,filename+1);//支持文件拖曳,但会多出双引号,这里是处理多余的双引号if(!strcmp(filename,exit))exit(0);//输入exit退出if(!(fp=fopen(filename,rb))){printf(Cannotopenthisfile!\n);//continue;}//以二进制打开文件fseek(fp,0,SEEK_END);//文件指针转到文件末尾if((len=ftell(fp))==-1){printf(Sorry!Cannotcalculatefileswhichlargerthan2GB!\n);fclose(fp);printf(pleaseclosethesoft,thenopenitagain!);//continue;}//ftell函数返回long,最大为GB,超出返回-1rewind(fp);//文件指针复位到文件头A=0x67452301,B=0xefcdab89,C=0x98badcfe,D=0x10325476;//初始化链接变量flen[1]=len/0x20000000;//flen单位是bitflen[0]=(len%0x20000000)*8;memset(x,0,64);//初始化x数组为:void*memset(void*s,charch,unsignedn);将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值,//块的大小由第三个参数指定,这个函数通常为新申请的内存做初始化工作fread(&x,4,16,fp);//以字节为一组,读取组数据for(i=0;ilen/64;i++)//为什么要除以{//循环运算直至文件结束md5();memset(x,0,64);fread(&x,4,16,fp);}((char*)x)[len%64]=128;//文件结束补,补操作,128二进制即if(len%6455)md5(),memset(x,0,64);memcpy(x+14,flen,8);//文件末尾加入原文件的bit长度md5();fclose(fp);printf(MD5Code:%08x%08x%08x%08x\n,PP(A),PP(B),PP(C),PP(D));//高低位逆反输出//break;//}}voidsave(){FILE*fp2;fp2=fopen(filename2,a);fprintf(fp2,%s,文件路径);fprintf(fp2,%s\n,filename);fprintf(fp2,MD5Code:%08x%08x%08x%08x\n,PP(A),PP(B),PP(C),PP(D));fprintf(fp2,\n);fclose(fp
本文标题:c语言实现MD5文件校验
链接地址:https://www.777doc.com/doc-5675293 .html