MD5 C#加密 源码
- 格式:pdf
- 大小:73.11 KB
- 文档页数:4
常⽤加密算法MD5、SHA-2和AES源码分享(CC++) 最近了解了⼀些加密算法,学习整理⼀些⽬前⽐较常⽤的三种加密⽅式(散列、对称、⾮对称)的相关经典算法(MD5、SHA-2、AES、RSA),这次分享的是MD5、SHA-2和ASE的纯C源码,另外两个我找时间整理好后会在贴出来。
⼤概介绍下这三种算法,详细的相关内容可以在⽹上搜索。
MD5是⼀种散列算法可以将任何数据散列成128位,速度很快,⼀般感觉⽤于数据⽂件校验⽐较多(安全性相对较低⽽且散列位数128位还是有概率碰撞,虽然极低极低,所以也有了另外这个SHA-2散列算法)。
AES是⼀种⾮常安全的对称加密,数据传输加密都可以⽤,然后有128、192、256位密钥,当然密钥越长加密解密越慢,看情况使⽤。
三种算法的核⼼算法代码都是我在⽹上查找的: MD5是基于Ron Rivest(RSA的那个R..)的算法,实现是由Colin Plumb 在1993年实现的。
AES核⼼算法源码() SHA2核⼼算法源码() 我对其进⾏了整理和封装,最⼤程度⽅便使⽤ MD5因为不牵扯密钥和解密所以超简单,两个接⼝⼀个散列⽂件⼀个散列数据: void MD5File(const char *filename, unsigned char *digest); void MD5Data(const unsigned char *data, unsigned int len, unsigned char *digest); AES⾸先得确认密钥长度,我这个是256位测试的,我代码⾥已经定义好相关三个宏AES_128、AES_192、AES_256,⾃⾏替换代⼊,接⼝我分别封装了加密解密数据和⽂件⼀共四个接⼝: int aes_cipher_data(uint8_t *in, size_t in_len, uint8_t *out, uint8_t *key, size_t key_len); int aes_decipher_data(uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len, uint8_t *key, size_t key_len); int aes_cipher_file(const char *in_filename, const char *out_filename, uint8_t *key, size_t key_len); int aes_decipher_file(const char *in_filename, const char *out_filename, uint8_t *key, size_t key_len); 因为aes加密需要进⾏补位之前没有做,我这次更新添加了PKCS5Padding补位⽅法,填充的原则是:数据长度除16,余数不为16,需要补满16个字节,填充(16-len)个(16-len),等于0额外填充16个16。
#include <windows.h>#include <wincon.h>#include<conio.h>#include<stdio.h>#include<string.h>#include<stdlib.h>CONSOLE_SCREEN_BUFFER_INFO csbiInfo;extern WINBASEAPI HWND WINAPI GetConsoleWindow();#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 RL(x, y) (((x) << (y)) | ((x) >> (32 - (y)))) //x向左循环移y位#define PP(x) (x<<24)|((x<<8)&0xff0000)|((x>>8)&0xff00)|(x>>24) //将x高低位互换,例如PP(aabbccdd)=ddccbbaa#define FF(a, b, c, d, x, s, ac) a = b + (RL((a + F(b,c,d) + x + ac),s))#define GG(a, b, c, d, x, s, ac) a = b + (RL((a + G(b,c,d) + x + ac),s))#define HH(a, b, c, d, x, s, ac) a = b + (RL((a + H(b,c,d) + x + ac),s))#define II(a, b, c, d, x, s, ac) a = b + (RL((a + I(b,c,d) + x + ac),s))unsigned A,B,C,D,a,b,c,d,i,len,flen[2],x[16]; //i临时变量,len文件长,flen[2]为位二进制表示的文件初始长度//x[16]int file_choose=0;//文件导入初始状态//全局变量char filename2[30]="MD5_result.txt";//默认文件位置void menu();void clear();void js();void md5();void save();char filename[100]; //文件名FILE *fp;int main(int argc, char* argv[]){int d1,d2,d,x, xy=0,i, k=0, s[100],ch,dir=40;struct re{int top;int left;int right;int bottom;}rt[7];RECT rect;HWND hwnd;//窗口句柄HDC hdc,dc;//画图设备HBRUSH hbr,hbr2;COORD pos3_1 = {1, 4},pos2_1 = {1, 4},pos2_2 = {1, 5},pos2_3 = {1, 6},pos2_4 = {1, 7};INPUT_RECORD mouseRec;DWORD state = 0, res=0;POINT pt;HPEN pn;HANDLE hOut, hIn;COLORREF yellow,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("Copy Right:小帅哥"),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.x>rt[1].left && pt.x<rt[1].right &&pt.y>rt[1].top && pt.y<rt[1].bottom){SetConsoleCursorPosition(hOut, pos3_1);clear();SetConsoleCursorPosition(hOut, pos3_1);if(k<15&&k>=9) k++;else k=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;}else if(pt.x>rt[2].left && pt.x<rt[2].right &&pt.y>rt[2].top && pt.y<rt[2].bottom){SetConsoleCursorPosition(hOut, pos3_1);clear();SetConsoleCursorPosition(hOut, pos3_1);if(k<15&&k>=9) k++;else k=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;}else if(pt.x>rt[3].left && pt.x<rt[3].right &&pt.y>rt[3].top && pt.y<rt[3].bottom){break;}};}}ReleaseDC(hwnd,hdc);return 0;}void clear(){int i;HANDLE hOut;COORD pos={1,4};SetConsoleCursorPosition(hOut, pos); // 设置光标位置for(i=0;i<=15;i++)printf( " \n"); }void js(){//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("Can not open this file!\n");// continue;} //以二进制打开文件fseek(fp, 0, SEEK_END); //文件指针转到文件末尾if((len=ftell(fp))==-1){printf("Sorry! Can not calculate files which larger than 2 GB!\n");fclose(fp);printf("please close the soft,then open it again!");// 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, char ch, unsigned n);将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值,//块的大小由第三个参数指定,这个函数通常为新申请的内存做初始化工作fread(&x,4,16,fp); //以字节为一组,读取组数据for(i=0;i<len/64;i++) //为什么要除以{ //循环运算直至文件结束md5();memset(x,0,64);fread(&x,4,16,fp);}((char*)x)[len%64]=128; //文件结束补,补操作,128二进制即if(len%64>55)md5(),memset(x,0,64);memcpy(x+14,flen,8); //文件末尾加入原文件的bit长度md5();fclose(fp);printf("MD5 Code:%08x%08x%08x%08x\n",PP(A),PP(B),PP(C),PP(D)); //高低位逆反输出// break;//}}void save(){FILE *fp2;fp2=fopen(filename2,"a");fprintf(fp2,"%s","文件路径");fprintf(fp2,"%s\n",filename);fprintf(fp2,"MD5 Code: %08x%08x%08x%08x\n",PP(A),PP(B),PP(C),PP(D));fprintf(fp2,"\n");fclose(fp2);}void md5(){ //MD5核心算法,供轮a=A,b=B,c=C,d=D;/**//* 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, 0x02441453); /**//* 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, 0x04881d05); /**//* 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 */A += a;B += b;C += c;D += d;}。
md5生成规则
MD5是一种常用的散列函数,用于将任意长度的数据映射为固定长度的128位哈希值。
以下是MD5生成规则的简要说明:
1. 填充:MD5算法首先需要对数据进行填充,使其长度达到一个特定的长度,即512的整数倍。
填充是通过在数据后面添加一个1位和足够数量的0位来实现的。
2. 初始化缓冲区:MD5算法使用一个64位的缓冲区,分为四个16位部分,用于存储中间结果和最终结果。
3. 处理分组数据:MD5算法将填充后的数据分成512位的小组,每个小组再分为16个子小组,每个子小组32位。
然后,算法对每个32位的子小组进行一系列的位操作和模加运算,以产生一个16位的进位值。
4. 循环运算:MD5算法使用了一个非线性函数F,该函数由多个查找表组成,用于加速计算。
算法通过循环迭代的方式对每个512位分组进行操作,每个分组经历16个子循环,每个子循环包括32次迭代。
5. 链接变量:MD5算法使用四个32位的链接变量(A、B、C、D),它们在算法开始时被初始化为特定的值。
这些变量在算法的执行过程中会发生变化,并最终影响最终的哈希值。
6. 输出:经过一系列的位操作和模加运算后,MD5算法产生了四个32位的输出,将这四个数连接起来就得到了最终的128位哈希值。
以上是MD5生成规则的简要说明,具体实现细节较为复杂,涉及到大量的位操作和模加运算。
如需了解更多关于MD5算法的信息,建议查阅相关的技术文档或咨询专业的技术人员。
MD5加密C语言实现MD5 (Message Digest Algorithm 5) 是一种常用的密码散列函数,用于将数据加密为128位长度的摘要。
在C语言中,可以通过一系列步骤来实现MD5加密算法。
1.准备工作:首先需要包含一些C标准头文件和预定义常量。
在C语言中,可以使用以下代码来实现:```c#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdint.h>#define HASH_LENGTH 16```2.定义数据结构:MD5算法涉及到一个64字节的消息块和一个4字节的消息摘要块。
在C语言中,可以使用以下代码来定义这些结构:```ctypedef structuint8_t data[64];uint32_t datalen;uint32_t bitlen[2];uint32_t state[4];}MD5_CTX;typedef uint8_t (*hash_function)(uint8_t *);```3.定义常量和函数:MD5算法使用到一些常量和函数。
在C语言中,可以使用以下代码来定义这些常量和函数:```cconst uint32_t k[64] =// more constants ...};const uint32_t r[64] =7,12,17,22,7,12,17,22,// more constants ...};void md5_transform(MD5_CTX *ctx, uint8_t data[]);void md5_init(MD5_CTX *ctx)ctx->datalen = 0;ctx->bitlen[0] = 0;ctx->bitlen[1] = 0;ctx->state[1] = 0xEFCDAB89;ctx->state[2] = 0x98BADCFE;void md5_update(MD5_CTX *ctx, uint8_t data[], uint32_t len) for (uint32_t i = 0; i < len; i++)ctx->data[ctx->datalen] = data[i];ctx->datalen++;if (ctx->datalen == 64)md5_transform(ctx, ctx->data);ctx->bitlen[0] += 512;ctx->bitlen[1] += (ctx->bitlen[0] < 512);ctx->datalen = 0;}}void md5_final(MD5_CTX *ctx, uint8_t hash[])uint32_t i = ctx->datalen;if (ctx->datalen < 56)ctx->data[i++] = 0x80;while (i < 56)ctx->data[i++] = 0x00;}} elsectx->data[i++] = 0x80;while (i < 64)ctx->data[i++] = 0x00;}md5_transform(ctx, ctx->data);memset(ctx->data, 0, 56);}ctx->bitlen[0] += ctx->datalen * 8;ctx->bitlen[1] += (ctx->bitlen[0] < ctx->datalen * 8); ctx->data[63] = ctx->bitlen[0] & 0xff;ctx->data[62] = (ctx->bitlen[0] >> 8) & 0xff;ctx->data[61] = (ctx->bitlen[0] >> 16) & 0xff;ctx->data[60] = (ctx->bitlen[0] >> 24) & 0xff;ctx->data[59] = ctx->bitlen[1] & 0xff;ctx->data[58] = (ctx->bitlen[1] >> 8) & 0xff;ctx->data[57] = (ctx->bitlen[1] >> 16) & 0xff;ctx->data[56] = (ctx->bitlen[1] >> 24) & 0xff;md5_transform(ctx, ctx->data);for (i = 0; i < 4; i++)hash[i] = (ctx->state[0] >> (i * 8)) & 0xff;hash[i + 4] = (ctx->state[1] >> (i * 8)) & 0xff;hash[i + 8] = (ctx->state[2] >> (i * 8)) & 0xff;hash[i + 12] = (ctx->state[3] >> (i * 8)) & 0xff;}void md5_transform(MD5_CTX *ctx, uint8_t data[])uint32_t a, b, c, d, f, g, temp;uint32_t m[16], i, j;for (i = 0, j = 0; i < 16; i++, j += 4)m[i] = (data[j]) + (data[j + 1] << 8) + (data[j + 2] << 16) + (data[j + 3] << 24);}a = ctx->state[0];b = ctx->state[1];c = ctx->state[2];d = ctx->state[3];for (i = 0; i < 64; i++)if (i < 16)f=(b&c),((~b)&d);g=i;} else if (i < 32)f=(d&b),((~d)&c);g=(5*i+1)%16;} else if (i < 48)f=b^c^d;g=(3*i+5)%16;} elsef=c^(b,(~d));g=(7*i)%16;}temp = d;d=c;c=b;b = b + leftrotate((a + f + k[i] + m[g]), r[i]);a = temp;}ctx->state[0] += a;ctx->state[1] += b;ctx->state[2] += c;ctx->state[3] += d;```4.实现加密函数:最后,可以编写一个简单的调用MD5算法的加密函数。
C语言实现MD5算法MD5(Message-Digest Algorithm 5)是一种常用的哈希函数算法,广泛用于验证数据完整性、密码存储和数字证书等领域。
下面是使用C语言实现MD5算法的代码。
这段代码包含了MD5算法的各个步骤,包括初始化MD5结构体、填充数据、更新状态、计算摘要等。
```c#include <stdio.h>#include <stdint.h>#include <string.h>//定义MD5常量#define B 0xEFCDAB89#define C 0x98BADCFE//循环左移宏定义#define LEFT_ROTATE(x, n) (((x) << (n)) , ((x) >> (32-(n)))) //填充消息void padMessage(uint8_t *message, uint32_t length)//计算需要填充的字节数uint32_t padLength = (length % sizeof(uint32_t) == 56) ? 64 : 56;padLength = padLength - (length % sizeof(uint32_t));//填充1位1message[length++] = 0x80;//填充0位for (uint32_t i = 0; i < padLength; i++) message[length++] = 0x00;}//在消息末尾添加原始消息的长度(以位表示)for (uint32_t i = 0; i < sizeof(uint32_t); i++) message[length++] = (length << 3) >> (i * 8); }//初始化MD5结构体void initMD5(uint32_t *state)state[0] = A;state[1] = B;state[2] = C;state[3] = D;//更新状态void updateState(uint32_t *state, uint32_t *M)uint32_t A = state[0], B = state[1], C = state[2], D = state[3];//定义MD5循环运算函数#define MD5_FUNCTION(a, b, c, d, k, s, i) \a=b+LEFT_ROTATE((a+F(b,c,d)+M[k]+T[i]),s)//迭代压缩消息MD5_FUNCTION(A,B,C,D,0,7,1);MD5_FUNCTION(D,A,B,C,1,12,2);MD5_FUNCTION(C,D,A,B,2,17,3);MD5_FUNCTION(B,C,D,A,3,22,4);MD5_FUNCTION(A,B,C,D,4,7,5);MD5_FUNCTION(D,A,B,C,5,12,6);MD5_FUNCTION(C,D,A,B,6,17,7);MD5_FUNCTION(B,C,D,A,7,22,8);MD5_FUNCTION(A,B,C,D,8,7,9);MD5_FUNCTION(D,A,B,C,9,12,10);MD5_FUNCTION(C,D,A,B,10,17,11);MD5_FUNCTION(B,C,D,A,11,22,12);MD5_FUNCTION(A,B,C,D,12,7,13);MD5_FUNCTION(C,D,A,B,14,17,15); MD5_FUNCTION(B,C,D,A,15,22,16); MD5_FUNCTION(A,B,C,D,1,5,17); MD5_FUNCTION(D,A,B,C,6,9,18); MD5_FUNCTION(C,D,A,B,11,14,19); MD5_FUNCTION(B,C,D,A,0,20,20); MD5_FUNCTION(A,B,C,D,5,5,21); MD5_FUNCTION(D,A,B,C,10,9,22); MD5_FUNCTION(C,D,A,B,15,14,23); MD5_FUNCTION(B,C,D,A,4,20,24); MD5_FUNCTION(A,B,C,D,9,5,25); MD5_FUNCTION(D,A,B,C,14,9,26); MD5_FUNCTION(C,D,A,B,3,14,27); MD5_FUNCTION(B,C,D,A,8,20,28); MD5_FUNCTION(A,B,C,D,13,5,29); MD5_FUNCTION(D,A,B,C,2,9,30); MD5_FUNCTION(C,D,A,B,7,14,31); MD5_FUNCTION(B,C,D,A,12,20,32);MD5_FUNCTION(D,A,B,C,8,11,34); MD5_FUNCTION(C,D,A,B,11,16,35); MD5_FUNCTION(B,C,D,A,14,23,36); MD5_FUNCTION(A,B,C,D,1,4,37); MD5_FUNCTION(D,A,B,C,4,11,38); MD5_FUNCTION(C,D,A,B,7,16,39); MD5_FUNCTION(B,C,D,A,10,23,40); MD5_FUNCTION(A,B,C,D,13,4,41); MD5_FUNCTION(D,A,B,C,0,11,42); MD5_FUNCTION(C,D,A,B,3,16,43); MD5_FUNCTION(B,C,D,A,6,23,44); MD5_FUNCTION(A,B,C,D,9,4,45); MD5_FUNCTION(D,A,B,C,12,11,46); MD5_FUNCTION(C,D,A,B,15,16,47); MD5_FUNCTION(B,C,D,A,2,23,48); MD5_FUNCTION(A,B,C,D,0,6,49); MD5_FUNCTION(D,A,B,C,7,10,50); MD5_FUNCTION(C,D,A,B,14,15,51);MD5_FUNCTION(A,B,C,D,12,6,53); MD5_FUNCTION(D,A,B,C,3,10,54); MD5_FUNCTION(C,D,A,B,10,15,55); MD5_FUNCTION(B,C,D,A,1,21,56); MD5_FUNCTION(A,B,C,D,8,6,57); MD5_FUNCTION(D,A,B,C,15,10,58); MD5_FUNCTION(C,D,A,B,6,15,59); MD5_FUNCTION(B,C,D,A,13,21,60); MD5_FUNCTION(A,B,C,D,4,6,61); MD5_FUNCTION(D,A,B,C,11,10,62); MD5_FUNCTION(C,D,A,B,2,15,63); MD5_FUNCTION(B,C,D,A,9,21,64); #undef MD5_FUNCTION//更新状态state[0] += A;state[1] += B;state[2] += C;state[3] += D;//计算MD5摘要void md5(uint8_t *message, uint32_t length, uint32_t *digest) //初始化MD5结构体uint32_t state[4];initMD5(state);//填充消息padMessage(message, length);//计算消息分组数量uint32_t numOfBlocks = length / 64;//处理每个分组for (uint32_t i = 0; i < numOfBlocks; i++)uint32_t M[16];memcpy(M, message + (i * 64), 64);//更新状态updateState(state, M);}//获取MD5摘要memcpy(digest, state, 16);int mai//测试用例uint8_t message[] = "Hello, MD5!";uint32_t length = sizeof(message) - 1;//计算MD5摘要uint32_t digest[4];md5(message, length, digest);//输出摘要printf("MD5 Digest: ");for (int i = 0; i < 4; i++)printf("%02x", ((uint8_t*)digest)[i]);}printf("\n");return 0;```以上是使用C语言实现MD5算法的代码。
十六进制输入转化为md5 c代码以下是使用C语言实现将十六进制字符串转换为MD5的示例代码:```c#include <stdio.h>#include <string.h>#include <openssl/md5.h>// 将十六进制字符串转换为二进制数据unsigned char hex_to_bin(char hex) {if (hex >= '0' && hex <= '9') {return hex - '0';} else if (hex >= 'a' && hex <= 'f') {return hex - 'a' + 10;} else if (hex >= 'A' && hex <= 'F') {return hex - 'A' + 10;} else {return 0;}}// 将十六进制字符串转换为MD5void hex_to_md5(const char* hex, unsigned char* md5) { int i, j;unsigned char bin[16];for (i = 0, j = 0; i < strlen(hex); i += 2, j++) {bin[j] = (hex_to_bin(hex[i]) << 4) | hex_to_bin(hex[i + 1]);}MD5((unsigned char*)bin, 16, md5);}int main() {const char* hex = "abcdef123456";unsigned char md5[16];hex_to_md5(hex, md5);for (int i = 0; i < 16; i++) {printf("%02x", md5[i]);}printf("\n");return 0;}```在上面的代码中,我们首先定义了一个`hex_to_bin`函数,用于将十六进制字符转换为二进制数据。
C语⾔计算MD5 #include <stdio.h>#include <string.h>#include <stdint.h>#define u8 uint8_t#define u32 uint32_t#define u64 uint64_t#define MD5_DIGEST_SIZE 16#define MD5_HMAC_BLOCK_SIZE 64#define MD5_BLOCK_WORDS 16#define MD5_HASH_WORDS 4#define F1(x, y, z) (z ^ (x & (y ^ z)))#define F2(x, y, z) F1(z, x, y)#define F3(x, y, z) (x ^ y ^ z)#define F4(x, y, z) (y ^ (x | ~z))#define MD5STEP(f, w, x, y, z, in, s) (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x) struct md5_ctx {u32 hash[MD5_HASH_WORDS];u32 block[MD5_BLOCK_WORDS];u64 byte_count;};static void md5_transform(u32 *hash, u32 const *in) {u32 a, b, c, d;a = hash[0];b = hash[1];c = hash[2];d = hash[3];MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);hash[0] += a;hash[1] += b;hash[2] += c;hash[3] += d;}static void md5_transform_helper(struct md5_ctx *ctx) {md5_transform(ctx->hash, ctx->block);}static void md5_init(void *ctx) {struct md5_ctx *mctx = ctx;mctx->hash[0] = 0x67452301;mctx->hash[1] = 0xefcdab89;mctx->hash[2] = 0x98badcfe;mctx->hash[3] = 0x10325476;mctx->byte_count = 0;}static void md5_update(void *ctx, const u8 *data, unsigned int len) {struct md5_ctx *mctx = ctx;const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);mctx->byte_count += len;if (avail > len) {memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), data, len); return;}memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), data, avail); md5_transform_helper(mctx);data += avail;len -= avail;while (len >= sizeof(mctx->block)) {memcpy(mctx->block, data, sizeof(mctx->block));md5_transform_helper(mctx);data += sizeof(mctx->block);len -= sizeof(mctx->block);}memcpy(mctx->block, data, len);}static void md5_final(void *ctx, u8 *out) {struct md5_ctx *mctx = ctx;const unsigned int offset = mctx->byte_count & 0x3f;char *p = (char *)mctx->block + offset;int padding = 56 - (offset + 1);*p++ = 0x80;if (padding < 0) {memset(p, 0x00, padding + sizeof (u64));md5_transform_helper(mctx);p = (char *)mctx->block;padding = 56;}memset(p, 0, padding);mctx->block[14] = mctx->byte_count << 3;mctx->block[15] = mctx->byte_count >> 29;md5_transform(mctx->hash, mctx->block);memcpy(out, mctx->hash, sizeof(mctx->hash)); memset(mctx, 0, sizeof(*mctx));}int main(int argc, char *argv[]) {uint8_t buffer[32];uint8_t out[MD5_DIGEST_SIZE];strcpy(buffer, "hello");struct md5_ctx mctx;md5_init(&mctx);md5_update(&mctx, buffer, strlen("hello")); md5_final(&mctx, out);printf("hello md5 is:\n");for(int i = 0; i < MD5_DIGEST_SIZE; i++) {printf("%02X", out[i]);}printf("\n");return 0;}。
cmd5破解思路如果遇到MD5加密文件,而又不知道密码的。
请在数据库中换上这组加密的数据吧16位加密:469e80d32c0559f8 32位加密:7fef 6171469e80d32c0559f88b 377245 40位加密:469e80d32c0559f88b 3772452c0559f88b 377245(不常用)那么密码就是admin888一般网站后台管理员密码都是经过MD5加密的,差不多都是16位和32位的,如果第一次碰到40位的加密,还真反映不过来,其实都是一样的。
7a57a5a 743894a0e4a801fc 343894a0e4a801fc3原文是admin这个实际上还是MD5加密,不过稍微做了些变动:前16位,7a57a5a 743894a0e,是admin的16位MD5密文;后面的24位,注意看,是两个4a801fc3之间夹杂一个43894a0e,而4a801fc3是32位MD5密文的后8位,43894a0e则是16位MD5密文的后8位。
所以,这个密文可以这样看:先用16位MD5加密admin,再在后面添上32位MD5密文的后8位,最后把后16位密文(43894a0e4a801fc3)来个重复,就诞生了40位MD5加密密文了。
知道原理了,以后看见这种后面32位密文实际上是一段16位密文的重复的加密密文,直接把前16位拿出去破解就可以了,后面的统统没用。
这样加密的好处不言而喻:通常人们都认为MD5只有16位和32位两种加密方式,被这样一搞,尽管看上去极像MD5,但因为位数不同所以没法用在线和工具破解,也就认为这是另一种加密方式了,混淆视听的目的就达到了。
附admin的16位、32位密文:7a57a5a 743894a0e 21232f297a57a5a 743894a0e4a801fc3所以遇到40位的加密密文,那么复制其前16位来破解那么就成功了,成功率100%Md5密文破解(解密)可以说是网络攻击中的一个必不可少的环节是黑客工具中的一个重要"辅助工具"md5解密主要用于网络攻击在对网站等进行入侵过程有可能获得治理员或者其他用户的账号和密码值(md5加.Md5密文破解(解密)可以说是网络攻击中的一个必不可少的环节是黑客工具中的一个重要"辅助工具"md5解密主要用于网络攻击在对网站等进行入侵过程有可能获得治理员或者其他用户的账号和密码值(md5加密后的值)获得的密码值有两种情况一种是明文另外一种就是对明文进行了加密如果密码值是加密的这个时候就需要对密码值进行判定如果是采取md5加密则可以通过MD5Crack3等软件进行破解王小云教授的md5密码碰撞破解算法没有公布因此目前Md5解密方式主要采取暴力破解即软件通过算法生成字典然后使用md5函数加密该字典中的值形成密文接着跟需要破解的密文进行比较如果相同则认为破解成功目前网上有很多网站提供md5加密或者加密值查询将加密后的md5值输入到网站中如果网站数据库中存在该md5则该值对应的md5加密前的值就为密码本案例介绍如何使用MD5Crack3以及一些在线的网站来进行破解;MD5Crack3是阿呆写的一款md5密码破解软件其网站地址:目前已经发布了MD5Crack4.0版本也可以到我的blog(去下载(一)在线生成md5密码值1.有关md5加解密知识Md5密文破解(解密)可以说是网络攻击中的一个必不可少的环节是黑客工具中的一个重要"辅助工具"md5解密主要用于网络攻击在对网站等进行入侵过程有可能获得治理员或者其他用户的账号和密码值(md5加密后的值) 获得的密码值有两种情况一种是明文另外一种就是对明文进行了加密如果密码值是加密的这个时候就需要对密码值进行判定如果是采取md5加密则可以通过MD5Crack4等软件进行破解王小云教授的md5密码碰撞破解算法没有公布因此目前Md5解密方式主要采取暴力破解即软件通过算法生成字典然后使用md5函数加密该字典中的值形成密文接着跟需要破解的密文进行比较如果相同则认为破解成功目前网上有很多网站提供md5加密或者加密值查询将加密后的md5值输入到网站中如果网站数据库中存在该md5则该值对应的md5加密前的值就为密码2.通过cmd5网站生成md5密码在浏览器中输入地址""在输入框中输入想要加密的原始密码然后单击"md5加密或解密"按钮即可如图1所示原始密码为"goodman88"加密后的密码值为:MD5(goodman88,32)=d5a8e0b 115259023faa219f5b53ca522 MD5(goodman88,16)=15259023 faa219f5图1 md5加密作为实验数据我们在生成一组生日的md5密码如下:MD5(19801230,32)=2540bb62336a8eb3ebc1e42ee44c8e3dMD5(19801230,16)=336a8eb3ebc1e42e(二)在线破解md5密码值1.通过cmd5网站破解md5密码Md5密文破解(解密)可以说是网络攻击中的一个必不可少的环节是黑客工具中的一个重要"辅助工具"md5解密主要用于网络攻击在对网站等进行入侵过程有可能获得治理员或者其他用户的账号和密码值(md5加.在cmd5网站的输入框中输入刚才加密后的m d5 32值"d5a8e0b 115259023faa219f5b53ca522"然后单击"md5加密或解密"按钮即可如图2所示未能成功破解图2通过cmd5网站未能破解md5密码将第二个生日加密后的md5值"2540bb62336a8eb3ebc1e42ee44c8e3d"放入cmd5网站进行破解很快其结果就出来了如图3所示图3破解简单的数字密码2.在线md5破解网站收费破解高难度的md5密码值一些在线网站提供的md5密码破解只能破解已经收录和一些简单的密码对于稍微复杂一点的密码都不轻易被破解;而且对一些稍微有点难度的md5密码值如果数据库中有在线网站是要求付费的例如用一个复杂一点的md5值进行破解如图4所示提示找到但是要求进行付费图4要求付费才干查看md5密码值(三)使用字典暴力破解md5密码值1.再次生成md5密码值再在cmd5网站生成原密码为"jimmychu246"的md5密码值为:MD5(jimmychu246,32)=437f4fffb6b2e5aaca9fd1712b8ad282MD5(jimmychu246,16)=b6b2e5aaca9fd171直接运行md5crack4运行界面如图5所示图5 md5crack4程序主界面2.在md5crack4中验证md5值将需要破解的md5值(437f4fffb6b2e5aaca9fd1712b8ad282)粘贴到"破解单个密文(Single Cryptograph)"输入框中如图6所示如果该md5值是正确的则会在"破解单个密文"输入框下方显示黑色的"有效(valid)"两个字否则显示"valid"为灰色3.使用字典进行破解在"字符设置(Plaintext Setting)"中抉择"字典(Dictionary)"并在"N0.1"、"N0.2"以及"N0.3"中抉择三个不同的字典抉择完毕后单击"Start"按钮开始md5破解破解结束后会给出相应的提示如图7所示在本案例中使用字典破解成功在Result Md5密文破解(解密)可以说是网络攻击中的一个必不可少的环节是黑客工具中的一个重要"辅助工具"md5解密主要用于网络攻击在对网站等进行入侵过程有可能获得治理员或者其他用户的账号和密码值(md5加.中显示破解的密码为"jimmychu246"图7使用字典进行破解4."使用字符集(Char Muster)"中的数字进行破解将上面生成的数字md5值"336a8eb3ebc1e42e"放入单一md5密码破解输入框中选中"Char Muster"后依次可以抉择"Number"、"lowercase"、"majuscule"、"special char"以及"custom"进行破解在本例中使用数字进行破击因此"最小长度(Min Length)"中设置为"1""最大长度(Max Length)"中设置为"8"然后单击"开始"按钮使用数字进行md5破解尝试破解密码位数从1~9999999之间的所有数字组合如图8所示其密码值破解成功破解结果为"336a8eb3ebc1e42e---[19801230]"图8使用数字进行破解&说明(1)在md5crack4中还可以定义数字、大小字母、非凡字符的组合来进行破解(2)如果计算机配置比较好可以设置更多线程(3)如果自定义进行破解建议先抉择使用数字然后依次是数字、大小字母、非凡字符的组合破解时先易后难否则破解时间太长(4)在md5crack4还可以"使用插件"进行破解(5)在md5crack4中还可以设置软件显示的语言版本一共有中文简体和英语两个版本单击主界面中的设置(Option)即可进行设置如图9所示图9设置md5crack4 5.一次破解多个密码将需要破解的md5密码全部存放到一个txt文件中每一个密码独立一行然后在md5crack4中单击"破解多个密文"抉择刚才编辑的md5密码文件如图10所示抉择一种破解方式在本案例中抉择使用数字字典进行破解最后单击"开始"按钮开始破解图10破解多个md5密码值在md5crack4右下方会显示破解结果单击"日志"可以查看md5值校验等日志信息单击"结果"可以查看破解的结果如图11所示在结果中会将md5值与原始密码进行一一对应图11破解结果M Md5密文破解(解密)可以说是网络攻击中的一个必不可少的环节是黑客工具中的一个重要"辅助工具"md5解密主要用于网络攻击在对网站等进行入侵过程有可能获得治理员或者其他用户的账号和密码值(md5加.d5加解密是网络攻防中必需把握的知识本文介绍了使用md5cracker以及通过网站来对md5值进行破解对md5破解可以先在一些md5破解网站进行破解如果未能破解则可以在本地通过md5cracker进行破解。
MD5ChecksumDefines.h1.//Magic initialization constants2.#define MD5_INIT_STATE_0 0x674523013.#define MD5_INIT_STATE_1 0xefcdab894.#define MD5_INIT_STATE_2 0x98badcfe5.#define MD5_INIT_STATE_3 0x103254766.7.//Constants for Transform routine.8.#define MD5_S11 79.#define MD5_S12 1210.#define MD5_S13 1711.#define MD5_S14 2212.#define MD5_S21 513.#define MD5_S22 914.#define MD5_S23 1415.#define MD5_S24 2016.#define MD5_S31 417.#define MD5_S32 1118.#define MD5_S33 1619.#define MD5_S34 2320.#define MD5_S41 621.#define MD5_S42 1022.#define MD5_S43 1523.#define MD5_S44 2124.25.//Transformation Constants - Round 126.#define MD5_T01 0xd76aa478 //Transformation Constant 127.#define MD5_T02 0xe8c7b756 //Transformation Constant 228.#define MD5_T03 0x242070db //Transformation Constant 329.#define MD5_T04 0xc1bdceee //Transformation Constant 430.#define MD5_T05 0xf57c0faf //Transformation Constant 531.#define MD5_T06 0x4787c62a //Transformation Constant 632.#define MD5_T07 0xa8304613 //Transformation Constant 733.#define MD5_T08 0xfd469501 //Transformation Constant 834.#define MD5_T09 0x698098d8 //Transformation Constant 935.#define MD5_T10 0x8b44f7af //Transformation Constant 1036.#define MD5_T11 0xffff5bb1 //Transformation Constant 1137.#define MD5_T12 0x895cd7be //Transformation Constant 1238.#define MD5_T13 0x6b901122 //Transformation Constant 1339.#define MD5_T14 0xfd987193 //Transformation Constant 1440.#define MD5_T15 0xa679438e //Transformation Constant 1541.#define MD5_T16 0x49b40821 //Transformation Constant 1642.43.//Transformation Constants - Round 245.#define MD5_T18 0xc040b340 //Transformation Constant 1846.#define MD5_T19 0x265e5a51 //Transformation Constant 1947.#define MD5_T20 0xe9b6c7aa //Transformation Constant 2048.#define MD5_T21 0xd62f105d //Transformation Constant 2149.#define MD5_T22 0x02441453 //Transformation Constant 2250.#define MD5_T23 0xd8a1e681 //Transformation Constant 2351.#define MD5_T24 0xe7d3fbc8 //Transformation Constant 2452.#define MD5_T25 0x21e1cde6 //Transformation Constant 2553.#define MD5_T26 0xc33707d6 //Transformation Constant 2654.#define MD5_T27 0xf4d50d87 //Transformation Constant 2755.#define MD5_T28 0x455a14ed //Transformation Constant 2856.#define MD5_T29 0xa9e3e905 //Transformation Constant 2957.#define MD5_T30 0xfcefa3f8 //Transformation Constant 3058.#define MD5_T31 0x676f02d9 //Transformation Constant 3159.#define MD5_T32 0x8d2a4c8a //Transformation Constant 3260.61.//Transformation Constants - Round 362.#define MD5_T33 0xfffa3942 //Transformation Constant 3363.#define MD5_T34 0x8771f681 //Transformation Constant 3464.#define MD5_T35 0x6d9d6122 //Transformation Constant 3565.#define MD5_T36 0xfde5380c //Transformation Constant 3666.#define MD5_T37 0xa4beea44 //Transformation Constant 3767.#define MD5_T38 0x4bdecfa9 //Transformation Constant 3868.#define MD5_T39 0xf6bb4b60 //Transformation Constant 3969.#define MD5_T40 0xbebfbc70 //Transformation Constant 4070.#define MD5_T41 0x289b7ec6 //Transformation Constant 4171.#define MD5_T42 0xeaa127fa //Transformation Constant 4272.#define MD5_T43 0xd4ef3085 //Transformation Constant 4373.#define MD5_T44 0x04881d05 //Transformation Constant 4474.#define MD5_T45 0xd9d4d039 //Transformation Constant 4575.#define MD5_T46 0xe6db99e5 //Transformation Constant 4676.#define MD5_T47 0x1fa27cf8 //Transformation Constant 4777.#define MD5_T48 0xc4ac5665 //Transformation Constant 4878.79.//Transformation Constants - Round 480.#define MD5_T49 0xf4292244 //Transformation Constant 4981.#define MD5_T50 0x432aff97 //Transformation Constant 5082.#define MD5_T51 0xab9423a7 //Transformation Constant 5183.#define MD5_T52 0xfc93a039 //Transformation Constant 5284.#define MD5_T53 0x655b59c3 //Transformation Constant 5385.#define MD5_T54 0x8f0ccc92 //Transformation Constant 5486.#define MD5_T55 0xffeff47d //Transformation Constant 5587.#define MD5_T56 0x85845dd1 //Transformation Constant 5689.#define MD5_T58 0xfe2ce6e0 //Transformation Constant 5890.#define MD5_T59 0xa3014314 //Transformation Constant 5991.#define MD5_T60 0x4e0811a1 //Transformation Constant 6092.#define MD5_T61 0xf7537e82 //Transformation Constant 6193.#define MD5_T62 0xbd3af235 //Transformation Constant 6294.#define MD5_T63 0x2ad7d2bb //Transformation Constant 6395.#define MD5_T64 0xeb86d391 //Transformation Constant 6496.97.98.//Null data (except for first BYTE) used to finalise the checksum calculation99.static unsigned char PADDING[64] = {100. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 101. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0103.};CMD5Checksum.h[cpp]1.#if !defined(AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_)2.#define AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_3.4.#if _MSC_VER > 10005.#pragma once6.#endif // _MSC_VER > 10007./*****************************************************************************************8.9.10.*****************************************************************************************/11.class CMD5Checksum12.{13.public:14.static CString GetMD5OfString(CString strString);15.//interface functions for the RSA MD5 calculation16.static CString GetMD5(const CString& strFilePath);17.18.protected:19.//constructor/destructor20. CMD5Checksum();21.virtual ~CMD5Checksum() {};22.23.//RSA MD5 implementation24.void Transform(BYTE Block[64]);25.void Update(BYTE* Input, ULONG nInputLen);26. CString Final();27.inline DWORD RotateLeft(DWORD x, int n);28.inline void FF( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);29.inline void GG( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);30.inline void HH( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);31.inline void II( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T);32.33.//utility functions34.inline void DWordToByte(BYTE* Output, DWORD* Input, UINT nLength);35.inline void ByteToDWord(DWORD* Output, BYTE* Input, UINT nLength);36.37.private:38.BYTE m_lpszBuffer[64]; //input buffer39.ULONG m_nCount[2]; //number of bits, modulo 2^64 (lsb first)40.ULONG m_lMD5[4]; //MD5 checksum41.};42.43.#endif // !defined(AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_)MD5Checksum.cpp[cpp]1.#include "stdafx.h"2.#include "MD5Checksum.h"3.#include "MD5ChecksumDefines.h"4.5.#ifdef _DEBUG6.#undef THIS_FILE7.static char THIS_FILE[]=__FILE__;8.#define new DEBUG_NEW9.#endif10.11.12./*****************************************************************************************13.*************/15.CString CMD5Checksum::GetMD5(const CString& strFilePath)16.{17.try18. {19. CFile file;20.if(file.Open(strFilePath,CFile::modeRead)==0)21.return _T("");22.23. CMD5Checksum MD5Checksum; //checksum object24.int nLength = 0; //number of bytes read from the file25.const int nBufferSize = 1024; //checksum the file in blocks of 1024bytes26.BYTE Buffer[nBufferSize]; //buffer for data read from the file27.28.//checksum the file in blocks of 1024 bytes29.while ((nLength = file.Read( Buffer, nBufferSize )) > 0 )30. {31. MD5Checksum.Update( Buffer, nLength );32. }33.34. file.Close();35.36.//finalise the checksum and return it37.return MD5Checksum.Final();38. }39.40.//report any file exceptions in debug mode only41.catch (CFileException* e )42. {43. TRACE0("CMD5Checksum::GetMD5: CFileException caught");44.throw e;45. }46.}47.48./*****************************************************************************************49.FUNCTION: CMD5Checksum::RotateLeft50.DETAILS: private51.DESCRIPTION: Rotates the bits in a 32 bit DWORD left by a specified amount52.RETURNS: The rotated DWORD53.ARGUMENTS: DWORD x : the value to be rotated54.int n : the number of bits to rotate by*************/56.DWORD CMD5Checksum::RotateLeft(DWORD x, int n)57.{58.//check that DWORD is 4 bytes long - true in Visual C++ 6 and 32 bit Windows59. ASSERT( sizeof(x) == 4 );60.61.//rotate and return x62.return (x << n) | (x >> (32-n));63.}64.65.66./*****************************************************************************************67.FUNCTION: CMD5Checksum::FF68.DETAILS: protected69.DESCRIPTION: Implementation of basic MD5 transformation algorithm70.RETURNS: none71.ARGUMENTS: DWORD &A, B, C, D : Current (partial) checksum72.DWORD X : Input data73.DWORD S : MD5_SXX Transformation constant74.DWORD T : MD5_TXX Transformation constant75.NOTES: None76.*****************************************************************************************/77.void CMD5Checksum::FF( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)78.{79.DWORD F = (B & C) | (~B & D);80. A += F + X + T;81. A = RotateLeft(A, S);82. A += B;83.}84.85.86./*****************************************************************************************87.FUNCTION: CMD5Checksum::GG88.DETAILS: protected89.DESCRIPTION: Implementation of basic MD5 transformation algorithm90.RETURNS: none91.ARGUMENTS: DWORD &A, B, C, D : Current (partial) checksum92.DWORD X : Input data93.DWORD S : MD5_SXX Transformation constant94.DWORD T : MD5_TXX Transformation constant95.NOTES: None96.*****************************************************************************************/97.void CMD5Checksum::GG( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)98.{99.DWORD G = (B & D) | (C & ~D);100. A += G + X + T;101. A = RotateLeft(A, S);102. A += B;103.}104.105.106./************************************************************************** ***************107.FUNCTION: CMD5Checksum::HH108.DETAILS: protected109.DESCRIPTION: Implementation of basic MD5 transformation algorithm110.RETURNS: none111.ARGUMENTS: DWORD &A, B, C, D : Current (partial) checksum112.DWORD X : Input data113.DWORD S : MD5_SXX Transformation constant114.DWORD T : MD5_TXX Transformation constant115.NOTES: None116.*************************************************************************** **************/117.void CMD5Checksum::HH( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)118.{119.DWORD H = (B ^ C ^ D);120. A += H + X + T;121. A = RotateLeft(A, S);122. A += B;123.}124.125.126./************************************************************************** ***************127.FUNCTION: CMD5Checksum::II128.DETAILS: protected129.DESCRIPTION: Implementation of basic MD5 transformation algorithm130.RETURNS: none131.ARGUMENTS: DWORD &A, B, C, D : Current (partial) checksum132.DWORD X : Input data133.DWORD S : MD5_SXX Transformation constant134.DWORD T : MD5_TXX Transformation constant135.NOTES: None136.*************************************************************************** **************/137.void CMD5Checksum::II( DWORD& A, DWORD B, DWORD C, DWORD D, DWORD X, DWORD S, DWORD T)138.{139.DWORD I = (C ^ (B | ~D));140. A += I + X + T;141. A = RotateLeft(A, S);142. A += B;143.}144.145.146./************************************************************************** ***************147.FUNCTION: CMD5Checksum::ByteToDWord148.DETAILS: private149.DESCRIPTION: Transfers the data in an 8 bit array to a 32 bit array150.RETURNS: void151.ARGUMENTS: DWORD* Output : the 32 bit (unsigned long) destination array 152.BYTE* Input : the 8 bit (unsigned char) source array153.UINT nLength : the number of 8 bit data items in the source array154.NOTES: Four BYTES from the input array are transferred to each DWORD entr y155.of the output array. The first BYTE is transferred to the bits (0-7)156.of the output DWORD, the second BYTE to bits 8-15 etc.157.The algorithm assumes that the input array is a multiple of 4 bytes long 158.so that there is a perfect fit into the array of 32 bit words.159.*************************************************************************** **************/160.void CMD5Checksum::ByteToDWord(DWORD* Output, BYTE* Input, UINT nLength) 161.{162.//entry invariants163. ASSERT( nLength % 4 == 0 );164. ASSERT( AfxIsValidAddress(Output, nLength/4, TRUE) );165. ASSERT( AfxIsValidAddress(Input, nLength, FALSE) );166.167.//initialisations168.UINT i=0; //index to Output array169.UINT j=0; //index to Input array170.171.//transfer the data by shifting and copying172.for ( ; j < nLength; i++, j += 4)173. {174. Output[i] = (ULONG)Input[j] |175. (ULONG)Input[j+1] << 8 |176. (ULONG)Input[j+2] << 16 |177. (ULONG)Input[j+3] << 24;178. }179.}180.181./************************************************************************** ***************182.FUNCTION: CMD5Checksum::Transform183.DETAILS: protected184.DESCRIPTION: MD5 basic transformation algorithm; transforms 'm_lMD5' 185.RETURNS: void186.ARGUMENTS: BYTE Block[64]187.NOTES: An MD5 checksum is calculated by four rounds of 'Transformation'. 188.The MD5 checksum currently held in m_lMD5 is merged by the189.transformation process with data passed in 'Block'.190.*************************************************************************** **************/191.void CMD5Checksum::Transform(BYTE Block[64])192.{193.//initialise local data with current checksum194.ULONG a = m_lMD5[0];195.ULONG b = m_lMD5[1];196.ULONG c = m_lMD5[2];197.ULONG d = m_lMD5[3];198.199.//copy BYTES from input 'Block' to an array of ULONGS 'X'200.ULONG X[16];201. ByteToDWord( X, Block, 64 );202.203.//Perform Round 1 of the transformation204. FF (a, b, c, d, X[ 0], MD5_S11, MD5_T01);205. FF (d, a, b, c, X[ 1], MD5_S12, MD5_T02);206. FF (c, d, a, b, X[ 2], MD5_S13, MD5_T03);207. FF (b, c, d, a, X[ 3], MD5_S14, MD5_T04);208. FF (a, b, c, d, X[ 4], MD5_S11, MD5_T05);209. FF (d, a, b, c, X[ 5], MD5_S12, MD5_T06);210. FF (c, d, a, b, X[ 6], MD5_S13, MD5_T07);211. FF (b, c, d, a, X[ 7], MD5_S14, MD5_T08);212. FF (a, b, c, d, X[ 8], MD5_S11, MD5_T09); 213. FF (d, a, b, c, X[ 9], MD5_S12, MD5_T10); 214. FF (c, d, a, b, X[10], MD5_S13, MD5_T11); 215. FF (b, c, d, a, X[11], MD5_S14, MD5_T12); 216. FF (a, b, c, d, X[12], MD5_S11, MD5_T13); 217. FF (d, a, b, c, X[13], MD5_S12, MD5_T14); 218. FF (c, d, a, b, X[14], MD5_S13, MD5_T15); 219. FF (b, c, d, a, X[15], MD5_S14, MD5_T16); 220.221.//Perform Round 2 of the transformation 222. GG (a, b, c, d, X[ 1], MD5_S21, MD5_T17); 223. GG (d, a, b, c, X[ 6], MD5_S22, MD5_T18); 224. GG (c, d, a, b, X[11], MD5_S23, MD5_T19); 225. GG (b, c, d, a, X[ 0], MD5_S24, MD5_T20); 226. GG (a, b, c, d, X[ 5], MD5_S21, MD5_T21); 227. GG (d, a, b, c, X[10], MD5_S22, MD5_T22); 228. GG (c, d, a, b, X[15], MD5_S23, MD5_T23); 229. GG (b, c, d, a, X[ 4], MD5_S24, MD5_T24); 230. GG (a, b, c, d, X[ 9], MD5_S21, MD5_T25); 231. GG (d, a, b, c, X[14], MD5_S22, MD5_T26); 232. GG (c, d, a, b, X[ 3], MD5_S23, MD5_T27); 233. GG (b, c, d, a, X[ 8], MD5_S24, MD5_T28); 234. GG (a, b, c, d, X[13], MD5_S21, MD5_T29); 235. GG (d, a, b, c, X[ 2], MD5_S22, MD5_T30); 236. GG (c, d, a, b, X[ 7], MD5_S23, MD5_T31); 237. GG (b, c, d, a, X[12], MD5_S24, MD5_T32); 238.239.//Perform Round 3 of the transformation 240. HH (a, b, c, d, X[ 5], MD5_S31, MD5_T33); 241. HH (d, a, b, c, X[ 8], MD5_S32, MD5_T34); 242. HH (c, d, a, b, X[11], MD5_S33, MD5_T35); 243. HH (b, c, d, a, X[14], MD5_S34, MD5_T36); 244. HH (a, b, c, d, X[ 1], MD5_S31, MD5_T37); 245. HH (d, a, b, c, X[ 4], MD5_S32, MD5_T38); 246. HH (c, d, a, b, X[ 7], MD5_S33, MD5_T39); 247. HH (b, c, d, a, X[10], MD5_S34, MD5_T40); 248. HH (a, b, c, d, X[13], MD5_S31, MD5_T41); 249. HH (d, a, b, c, X[ 0], MD5_S32, MD5_T42); 250. HH (c, d, a, b, X[ 3], MD5_S33, MD5_T43); 251. HH (b, c, d, a, X[ 6], MD5_S34, MD5_T44); 252. HH (a, b, c, d, X[ 9], MD5_S31, MD5_T45); 253. HH (d, a, b, c, X[12], MD5_S32, MD5_T46); 254. HH (c, d, a, b, X[15], MD5_S33, MD5_T47); 255. HH (b, c, d, a, X[ 2], MD5_S34, MD5_T48);256.257.//Perform Round 4 of the transformation258. II (a, b, c, d, X[ 0], MD5_S41, MD5_T49);259. II (d, a, b, c, X[ 7], MD5_S42, MD5_T50);260. II (c, d, a, b, X[14], MD5_S43, MD5_T51);261. II (b, c, d, a, X[ 5], MD5_S44, MD5_T52);262. II (a, b, c, d, X[12], MD5_S41, MD5_T53);263. II (d, a, b, c, X[ 3], MD5_S42, MD5_T54);264. II (c, d, a, b, X[10], MD5_S43, MD5_T55);265. II (b, c, d, a, X[ 1], MD5_S44, MD5_T56);266. II (a, b, c, d, X[ 8], MD5_S41, MD5_T57);267. II (d, a, b, c, X[15], MD5_S42, MD5_T58);268. II (c, d, a, b, X[ 6], MD5_S43, MD5_T59);269. II (b, c, d, a, X[13], MD5_S44, MD5_T60);270. II (a, b, c, d, X[ 4], MD5_S41, MD5_T61);271. II (d, a, b, c, X[11], MD5_S42, MD5_T62);272. II (c, d, a, b, X[ 2], MD5_S43, MD5_T63);273. II (b, c, d, a, X[ 9], MD5_S44, MD5_T64);274.275.//add the transformed values to the current checksum276. m_lMD5[0] += a;277. m_lMD5[1] += b;278. m_lMD5[2] += c;279. m_lMD5[3] += d;280.}281.282.283./************************************************************************** ***************284.CONSTRUCTOR: CMD5Checksum285.DESCRIPTION: Initialises member data286.ARGUMENTS: None287.NOTES: None288.*************************************************************************** **************/289.CMD5Checksum::CMD5Checksum()290.{291.// zero members292. memset( m_lpszBuffer, 0, 64 );293. m_nCount[0] = m_nCount[1] = 0;294.295.// Load magic state initialization constants296. m_lMD5[0] = MD5_INIT_STATE_0;297. m_lMD5[1] = MD5_INIT_STATE_1;298. m_lMD5[2] = MD5_INIT_STATE_2;299. m_lMD5[3] = MD5_INIT_STATE_3;300.}301.302./************************************************************************** ***************303.FUNCTION: CMD5Checksum::DWordToByte304.DETAILS: private305.DESCRIPTION: Transfers the data in an 32 bit array to a 8 bit array306.RETURNS: void307.ARGUMENTS: BYTE* Output : the 8 bit destination array308.DWORD* Input : the 32 bit source array309.UINT nLength : the number of 8 bit data items in the source array310.NOTES: One DWORD from the input array is transferred into four BYTES 311.in the output array. The first (0-7) bits of the first DWORD are312.transferred to the first output BYTE, bits bits 8-15 are transferred from 313.the second BYTE etc.314.315.The algorithm assumes that the output array is a multiple of 4 bytes long 316.so that there is a perfect fit of 8 bit BYTES into the 32 bit DWORDs.317.*************************************************************************** **************/318.void CMD5Checksum::DWordToByte(BYTE* Output, DWORD* Input, UINT nLength ) 319.{320.//entry invariants321. ASSERT( nLength % 4 == 0 );322. ASSERT( AfxIsValidAddress(Output, nLength, TRUE) );323. ASSERT( AfxIsValidAddress(Input, nLength/4, FALSE) );324.325.//transfer the data by shifting and copying326.UINT i = 0;327.UINT j = 0;328.for ( ; j < nLength; i++, j += 4)329. {330. Output[j] = (UCHAR)(Input[i] & 0xff );331. Output[j+1] = (UCHAR)((Input[i] >> 8) & 0xff);332. Output[j+2] = (UCHAR)((Input[i] >> 16) & 0xff);333. Output[j+3] = (UCHAR)((Input[i] >> 24) & 0xff);334. }335.}336.337.338./************************************************************************** ***************339.FUNCTION: CMD5Checksum::Final340.DETAILS: protected341.DESCRIPTION: Implementation of main MD5 checksum algorithm; ends the checks um calculation.342.RETURNS: CString : the final hexadecimal MD5 checksum result343.ARGUMENTS: None344.NOTES: Performs the final MD5 checksum calculation ('Update' does most of the work,345.this function just finishes the calculation.)346.*************************************************************************** **************/347.CString CMD5Checksum::Final()348.{349.//Save number of bits350.BYTE Bits[8];351. DWordToByte( Bits, m_nCount, 8 );352.353.//Pad out to 56 mod 64.354.UINT nIndex = (UINT)((m_nCount[0] >> 3) & 0x3f);355.UINT nPadLen = (nIndex < 56) ? (56 - nIndex) : (120 - nIndex);356. Update( PADDING, nPadLen );357.358.//Append length (before padding)359. Update( Bits, 8 );360.361.//Store final state in 'lpszMD5'362.const int nMD5Size = 16;363. unsigned char lpszMD5[ nMD5Size ];364. DWordToByte( lpszMD5, m_lMD5, nMD5Size );365.366.//Convert the hexadecimal checksum to a CString367. CString strMD5;368.for ( int i=0; i < nMD5Size; i++)369. {370. CString Str;371.if (lpszMD5[i] == 0)372. {373. Str = CString("00");374. }375.else if (lpszMD5[i] <= 15)376. {377. Str.Format(_T("0%X"),lpszMD5[i]);378. }379.else381. Str.Format(_T("%X"),lpszMD5[i]);382. }383.384. ASSERT( Str.GetLength() == 2 );385. strMD5 += Str;386. }387. ASSERT( strMD5.GetLength() == 32 );388.return strMD5;389.}390.391.392./************************************************************************** ***************393.FUNCTION: CMD5Checksum::Update394.DETAILS: protected395.DESCRIPTION: Implementation of main MD5 checksum algorithm396.RETURNS: void397.ARGUMENTS: BYTE* Input : input block398.UINT nInputLen : length of input block399.NOTES: Computes the partial MD5 checksum for 'nInputLen' bytes of data in 'Input'400.*************************************************************************** **************/401.void CMD5Checksum::Update( BYTE* Input, ULONG nInputLen )402.{403.//Compute number of bytes mod 64404.UINT nIndex = (UINT)((m_nCount[0] >> 3) & 0x3F);405.406.//Update number of bits407.if ( ( m_nCount[0] += nInputLen << 3 ) < ( nInputLen << 3) )408. {409. m_nCount[1]++;410. }411. m_nCount[1] += (nInputLen >> 29);412.413.//Transform as many times as possible.414.UINT i=0;415.UINT nPartLen = 64 - nIndex;416.if (nInputLen >= nPartLen)417. {418. memcpy( &m_lpszBuffer[nIndex], Input, nPartLen );419. Transform( m_lpszBuffer );420.for (i = nPartLen; i + 63 < nInputLen; i += 64)422. Transform( &Input[i] );423. }424. nIndex = 0;425. }426.else427. {428. i = 0;429. }430.431.// Buffer remaining input432. memcpy( &m_lpszBuffer[nIndex], &Input[i], nInputLen-i);433.}434.435.436.437.CString CMD5Checksum::GetMD5OfString(CString strString)438.{439.try440. {441. CMD5Checksum MD5Checksum; //checksum object442.int nLength = strString.GetLength(); //number of bytes read f rom the file443.//const int nBufferSize = 1024; //checksum the file in blocks of 1024 bytes444.BYTE *Buffer; //buffer for data read from the file445. Buffer=(BYTE*)(strString.GetBuffer(nLength));446.//checksum the file in blocks of 1024 bytes447.//while ((nLength = File.Read( Buffer, nBufferSize )) > 0 )448.//{449. MD5Checksum.Update( Buffer, nLength );450.//}451.//finalise the checksum and return it452.return MD5Checksum.Final();453. }454.455.//report any file exceptions in debug mode only456.catch (CFileException* e )457. {458. TRACE0("CMD5Checksum::GetMD5: CFileException caught");459.throw e;460. }461.}调用的地方:[cpp]1.m_strFileMD5 = CMD5Checksum::GetMD5(m_strFilePath);。
c 获取文件md5的3种方法获取文件的MD5值有多种方法,以下是其中三种常见的方法:1. 使用命令行工具:在命令行中可以使用专门的工具来计算文件的MD5值。
在Windows系统中,可以使用certutil命令,语法如下:certutil -hashfile 文件路径 MD5。
在Linux或者Mac系统中,可以使用md5sum命令,语法如下:md5sum 文件路径。
2. 使用编程语言的库:许多编程语言都提供了计算MD5值的库,比如在Python中,可以使用hashlib库来计算文件的MD5值。
示例代码如下:python.import hashlib.def get_file_md5(file_path):md5 = hashlib.md5()。
with open(file_path, 'rb') as f:for chunk in iter(lambda: f.read(4096), b''): md5.update(chunk)。
return md5.hexdigest()。
file_path = '文件路径'。
print(get_file_md5(file_path))。
3. 使用MD5计算工具:还有许多第三方的MD5计算工具可以帮助获取文件的MD5值,这些工具通常提供了图形化界面,用户可以通过简单的操作获取文件的MD5值。
常见的MD5计算工具有HashCalc、WinMD5等。
这些方法都可以帮助你获取文件的MD5值,选择适合自己的方法来进行操作即可。
希望这些信息对你有所帮助。
md5crack使用方法MD5Crack是一款用于破解MD5加密的工具,它可以帮助用户快速破解MD5加密的密码。
下面是MD5Crack的使用方法。
第一步:下载MD5Crack用户可以在互联网上搜索MD5Crack并下载安装包,也可以在GitHub上找到MD5Crack的源代码并自行编译。
第二步:打开MD5Crack打开MD5Crack后,用户需要输入要破解的MD5加密字符串。
用户可以手动输入,也可以从文件中导入。
第三步:选择破解方式MD5Crack提供了多种破解方式,包括暴力破解、字典破解、混合破解等。
用户可以根据自己的需求选择不同的破解方式。
暴力破解:MD5Crack会尝试所有可能的字符组合,直到找到正确的密码。
这种方式需要耗费大量时间和计算资源。
字典破解:MD5Crack会使用一个包含常用密码的字典文件进行破解。
用户可以自己编写字典文件,也可以从互联网上下载。
混合破解:MD5Crack会结合暴力破解和字典破解两种方式进行破解。
这种方式可以提高破解速度和成功率。
第四步:开始破解选择好破解方式后,用户可以点击“开始破解”按钮开始破解。
MD5Crack会根据用户选择的破解方式进行破解,并在破解成功后显示密码。
总结:MD5Crack是一款功能强大的MD5破解工具,它可以帮助用户快速破解MD5加密的密码。
用户只需要输入要破解的MD5加密字符串,选择破解方式,点击“开始破解”按钮即可开始破解。
但需要注意的是,MD5Crack只能破解MD5加密的密码,对于其他加密方式无能为力。
同时,使用MD5Crack进行破解需要遵守法律法规,不得用于非法用途。
c语言md5加密函数C语言MD5加密函数MD5(Message Digest Algorithm 5)是一种广泛使用的密码散列函数,常用于数据加密和数据完整性验证。
在C语言中,我们可以通过编写MD5加密函数来实现对数据的加密操作。
本文将介绍如何使用C语言编写一个简单的MD5加密函数,并详细解释其原理和步骤。
一、MD5加密原理MD5加密算法基于消息摘要算法,它将任意长度的消息作为输入,通过一系列复杂的数学运算,生成一个固定长度的密文。
MD5算法的核心思想是将输入的消息进行分块处理,并对每个分块进行位运算和逻辑运算,最终得到一个128位的摘要。
MD5算法具有以下特点:1. 不可逆性:无法从加密后的密文推导出原始消息。
2. 唯一性:不同的输入会产生不同的摘要。
3. 完整性:对于相同的输入,产生的摘要是稳定的。
二、MD5加密步骤MD5算法的加密过程包括以下几个步骤:1. 填充消息:将消息的位数填充为448的倍数,填充方式为在消息末尾添加一个1和若干个0。
2. 添加长度:将填充后的消息长度添加到消息末尾,以64位二进制数表示。
3. 初始化缓冲区:初始化四个32位的缓冲区A、B、C、D,用于存储最终的摘要。
4. 分块处理:将填充后的消息分为若干个512位的分组,对每个分组进行处理。
5. 迭代压缩:对每个分组进行64轮的迭代压缩操作,更新缓冲区的值。
6. 输出结果:最后将缓冲区的值按照小端序输出,得到128位的摘要。
三、C语言实现MD5加密函数以下是一个简单的C语言实现MD5加密函数的示例代码:#include <stdio.h>#include <string.h>#include <stdint.h>// 定义MD5加密函数void md5_encrypt(const uint8_t *message, uint32_t len, uint8_t *digest) {// 初始化缓冲区uint32_t A = 0x67452301;uint32_t B = 0xEFCDAB89;uint32_t C = 0x98BADCFE;uint32_t D = 0x10325476;// 填充消息// ...// 添加长度// ...// 分块处理// ...// 迭代压缩// ...// 输出结果// ...}int main() {// 测试示例uint8_t message[] = "Hello, MD5!";uint8_t digest[16] = {0};md5_encrypt(message, strlen(message), digest); // 输出结果for (int i = 0; i < 16; i++) {printf("%02x", digest[i]);}printf("\n");return 0;}四、总结通过上述的示例代码,我们可以看到使用C语言编写MD5加密函数并不复杂。
c语言md5加密函数【最新版】目录1.MD5 加密算法简介2.C 语言中的 MD5 加密函数3.MD5 加密函数的使用示例正文【1.MD5 加密算法简介】MD5 加密算法是一种广泛应用的哈希函数,它可以将任意长度的明文转换为固定长度的密文。
MD5 加密算法的主要特点是加密速度快、安全性高,适用于各种数据加密和完整性校验场景。
【2.C 语言中的 MD5 加密函数】在 C 语言中,我们可以使用第三方库如 crypto++或自行编写 MD5 加密函数。
这里以 crypto++库为例,介绍如何在 C 语言中使用 MD5 加密函数。
首先,需要安装 crypto++库。
在 Linux 系统下,可以使用以下命令进行安装:```bashsudo apt-get install libcrypto++-dev```然后,在 C 代码中引入 crypto++库:```cpp#include <iostream>#include <crypto++/md5.h>```接下来,可以编写一个 MD5 加密函数:```cppstd::string md5_encrypt(const std::string& input) {std::string output;CryptoPP::MD5 md5;md5.Update(input.c_str(), input.size());md5.Final(output.begin(), output.end());return output;}```【3.MD5 加密函数的使用示例】以下是一个使用上述 MD5 加密函数的简单示例:```cppint main() {std::string input = "Hello, world!";std::string encrypted = md5_encrypt(input);std::cout << "原始字符串:" << input << std::endl;std::cout << "加密后的字符串:" << encrypted << std::endl; return 0;}```运行上述代码,可以得到加密后的字符串。
⽤c获取⽂件MD5值的实现⽅法⽹上有 md5.c , md5.h, 但是⾥⾯只有 MD5Init(), MD5Update(), MD5Final() 三个函数,只可以直接对字符进⾏操作, ⽽没有直接求⽂件md5的接⼝. 以下是我的实现, 可计算32位和16位的md5值.复制代码代码如下:#include <stdio.h>#include <stdlib.h>#include <string.h>#include "md5.h"char *MD5_file (char *path, int md5_len){FILE *fp = fopen (path, "rb");MD5_CTX mdContext;int bytes;unsigned char data[1024];char *file_md5;int i;if (fp == NULL) {fprintf (stderr, "fopen %s failed\n", path);return NULL;}MD5Init (&mdContext);while ((bytes = fread (data, 1, 1024, fp)) != 0){MD5Update (&mdContext, data, bytes);}MD5Final (&mdContext);file_md5 = (char *)malloc((md5_len + 1) * sizeof(char));if(file_md5 == NULL){fprintf(stderr, "malloc failed.\n");return NULL;}memset(file_md5, 0, (md5_len + 1));if(md5_len == 16){for(i=4; i<12; i++){sprintf(&file_md5[(i-4)*2], "%02x", mdContext.digest[i]);}}else if(md5_len == 32){for(i=0; i<16; i++){sprintf(&file_md5[i*2], "%02x", mdContext.digest[i]);}}else{fclose(fp);free(file_md5);return NULL;}int main(int argc, char *argv[]){char *md5;md5 = MD5_file("temp", 16);printf("16: %s\n", md5);free(md5);md5 = MD5_file("temp", 32);printf("32: %s\n", md5);free(md5);return 0;}以下是 md5.c的源码复制代码代码如下:#include "md5.h"/************************************************************************* md5.c **** RSA Data Security, Inc. MD5 Message Digest Algorithm ** ** Created: 2/17/90 RLR **** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version ** ***********************************************************************//************************************************************************* Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. ** ** **** License to copy and use this software is granted provided that ** ** it is identified as the "RSA Data Security, Inc. MD5 Message ** ** Digest Algorithm" in all material mentioning or referencing this ** ** software or this function. **** **** License is also granted to make and use derivative works ** ** provided that such works are identified as "derived from the RSA ** ** Data Security, Inc. MD5 Message Digest Algorithm" in all **** material mentioning or referencing the derived work. **** **** RSA Data Security, Inc. makes no representations concerning ** ** either the merchantability of this software or the suitability **** of this software for any particular purpose. It is provided "as **** is" without express or implied warranty of any kind. **** **** These notices must be retained in any copies of any part of this ** ** documentation and/or software. *************************************************************************//* -- include the following line if the md5.h header file is separate -- */ /* #include "md5.h" *//* forward declaration */static void Transform ();static unsigned char PADDING[64] = {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};/* F, G and H are basic MD5 functions: selection, majority, parity */#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)))/* ROTATE_LEFT rotates x left n bits */#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 *//* Rotation is separate from addition to prevent recomputation */#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 MD5Init (MD5_CTX *mdContext){mdContext->i[0] = mdContext->i[1] = (UINT4)0;/* Load magic initialization constants.*/mdContext->buf[0] = (UINT4)0x67452301;mdContext->buf[1] = (UINT4)0xefcdab89;mdContext->buf[2] = (UINT4)0x98badcfe;mdContext->buf[3] = (UINT4)0x10325476;}void MD5Update (MD5_CTX *mdContext, unsigned char *inBuf, unsigned int inLen) {UINT4 in[16];int mdi;unsigned int i, ii;/* compute number of bytes mod 64 */mdi = (int)((mdContext->i[0] >> 3) & 0x3F);/* update number of bits */if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])mdContext->i[1]++;mdContext->i[0] += ((UINT4)inLen << 3);mdContext->i[1] += ((UINT4)inLen >> 29);while (inLen--) {/* add new character to buffer, increment mdi */mdContext->in[mdi++] = *inBuf++;/* transform if necessary */if (mdi == 0x40) {for (i = 0, ii = 0; i < 16; i++, ii += 4)Transform (mdContext->buf, in);mdi = 0;}}}void MD5Final (MD5_CTX *mdContext){UINT4 in[16];int mdi;unsigned int i, ii;unsigned int padLen;/* save number of bits */in[14] = mdContext->i[0];in[15] = mdContext->i[1];/* compute number of bytes mod 64 */mdi = (int)((mdContext->i[0] >> 3) & 0x3F);/* pad out to 56 mod 64 */padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);MD5Update (mdContext, PADDING, padLen);/* append length in bits and transform */for (i = 0, ii = 0; i < 14; i++, ii += 4)in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |(((UINT4)mdContext->in[ii+2]) << 16) |(((UINT4)mdContext->in[ii+1]) << 8) |((UINT4)mdContext->in[ii]);Transform (mdContext->buf, in);/* store buffer in digest */for (i = 0, ii = 0; i < 4; i++, ii += 4) {mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF); mdContext->digest[ii+1] =(unsigned char)((mdContext->buf[i] >> 8) & 0xFF);mdContext->digest[ii+2] =(unsigned char)((mdContext->buf[i] >> 16) & 0xFF);mdContext->digest[ii+3] =(unsigned char)((mdContext->buf[i] >> 24) & 0xFF);}}/* Basic MD5 step. Transform buf based on in.*/static void Transform (UINT4 *buf, UINT4 *in){UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];/* Round 1 */#define S11 7#define S12 12#define S13 17#define S14 22FF ( a, b, c, d, in[ 0], S11, 3614090360); /* 1 */FF ( d, a, b, c, in[ 1], S12, 3905402710); /* 2 */FF ( c, d, a, b, in[ 2], S13, 606105819); /* 3 */FF ( b, c, d, a, in[ 3], S14, 3250441966); /* 4 */FF ( a, b, c, d, in[ 4], S11, 4118548399); /* 5 */FF ( d, a, b, c, in[ 5], S12, 1200080426); /* 6 */FF ( c, d, a, b, in[ 6], S13, 2821735955); /* 7 */FF ( b, c, d, a, in[ 7], S14, 4249261313); /* 8 */FF ( a, b, c, d, in[ 8], S11, 1770035416); /* 9 */FF ( d, a, b, c, in[ 9], S12, 2336552879); /* 10 */FF ( c, d, a, b, in[10], S13, 4294925233); /* 11 *//* Round 2 */#define S21 5#define S22 9#define S23 14#define S24 20GG ( a, b, c, d, in[ 1], S21, 4129170786); /* 17 */ GG ( d, a, b, c, in[ 6], S22, 3225465664); /* 18 */ GG ( c, d, a, b, in[11], S23, 643717713); /* 19 */ GG ( b, c, d, a, in[ 0], S24, 3921069994); /* 20 */ GG ( a, b, c, d, in[ 5], S21, 3593408605); /* 21 */ GG ( d, a, b, c, in[10], S22, 38016083); /* 22 */ GG ( c, d, a, b, in[15], S23, 3634488961); /* 23 */ GG ( b, c, d, a, in[ 4], S24, 3889429448); /* 24 */ GG ( a, b, c, d, in[ 9], S21, 568446438); /* 25 */ GG ( d, a, b, c, in[14], S22, 3275163606); /* 26 */ GG ( c, d, a, b, in[ 3], S23, 4107603335); /* 27 */ GG ( b, c, d, a, in[ 8], S24, 1163531501); /* 28 */ GG ( a, b, c, d, in[13], S21, 2850285829); /* 29 */ GG ( d, a, b, c, in[ 2], S22, 4243563512); /* 30 */ GG ( c, d, a, b, in[ 7], S23, 1735328473); /* 31 */ GG ( b, c, d, a, in[12], S24, 2368359562); /* 32 */ /* Round 3 */#define S31 4#define S32 11#define S33 16#define S34 23HH ( a, b, c, d, in[ 5], S31, 4294588738); /* 33 */ HH ( d, a, b, c, in[ 8], S32, 2272392833); /* 34 */ HH ( c, d, a, b, in[11], S33, 1839030562); /* 35 */ HH ( b, c, d, a, in[14], S34, 4259657740); /* 36 */ HH ( a, b, c, d, in[ 1], S31, 2763975236); /* 37 */ HH ( d, a, b, c, in[ 4], S32, 1272893353); /* 38 */ HH ( c, d, a, b, in[ 7], S33, 4139469664); /* 39 */ HH ( b, c, d, a, in[10], S34, 3200236656); /* 40 */ HH ( a, b, c, d, in[13], S31, 681279174); /* 41 */ HH ( d, a, b, c, in[ 0], S32, 3936430074); /* 42 */ HH ( c, d, a, b, in[ 3], S33, 3572445317); /* 43 */ HH ( b, c, d, a, in[ 6], S34, 76029189); /* 44 */ HH ( a, b, c, d, in[ 9], S31, 3654602809); /* 45 */ HH ( d, a, b, c, in[12], S32, 3873151461); /* 46 */ HH ( c, d, a, b, in[15], S33, 530742520); /* 47 */ HH ( b, c, d, a, in[ 2], S34, 3299628645); /* 48 */ /* Round 4 */#define S41 6#define S42 10#define S43 15#define S44 21II ( a, b, c, d, in[ 0], S41, 4096336452); /* 49 */ II ( d, a, b, c, in[ 7], S42, 1126891415); /* 50 */ II ( c, d, a, b, in[14], S43, 2878612391); /* 51 */ II ( b, c, d, a, in[ 5], S44, 4237533241); /* 52 */ II ( a, b, c, d, in[12], S41, 1700485571); /* 53 */ II ( d, a, b, c, in[ 3], S42, 2399980690); /* 54 */ II ( c, d, a, b, in[10], S43, 4293915773); /* 55 */ II ( b, c, d, a, in[ 1], S44, 2240044497); /* 56 */ II ( a, b, c, d, in[ 8], S41, 1873313359); /* 57 */ II ( d, a, b, c, in[15], S42, 4264355552); /* 58 */II ( b, c, d, a, in[ 9], S44, 3951481745); /* 64 */buf[0] += a;buf[1] += b;buf[2] += c;buf[3] += d;}/************************************************************************* End of md5.c ********************************* (cut) *********************************/以下是 md5.h 的源码复制代码代码如下:/************************************************************************* md5.h -- Header file for implementation of MD5 **** RSA Data Security, Inc. MD5 Message Digest Algorithm ** ** Created: 2/17/90 RLR **** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version ** ** Revised (for MD5): RLR 4/27/91 **** -- G modified to have y&~z instead of y&z **** -- FF, GG, HH modified to add in last register done **** -- Access pattern: round 2 works mod 5, round 3 works mod 3 ** ** -- distinct additive constant for each step **** -- round 4 added, working mod 7 *************************************************************************//************************************************************************* Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. ** ** **** License to copy and use this software is granted provided that ** ** it is identified as the "RSA Data Security, Inc. MD5 Message ** ** Digest Algorithm" in all material mentioning or referencing this ** ** software or this function. **** **** License is also granted to make and use derivative works ** ** provided that such works are identified as "derived from the RSA ** ** Data Security, Inc. MD5 Message Digest Algorithm" in all **** material mentioning or referencing the derived work. **** **** RSA Data Security, Inc. makes no representations concerning ** ** either the merchantability of this software or the suitability **** of this software for any particular purpose. It is provided "as **** is" without express or implied warranty of any kind. **** **** These notices must be retained in any copies of any part of this ** ** documentation and/or software. *************************************************************************//* typedef a 32 bit type */typedef unsigned long int UINT4;/* Data structure for MD5 (Message Digest) computation */typedef struct {UINT4 i[2]; /* number of _bits_ handled mod 2^64 */void MD5Init ();void MD5Update ();void MD5Final ();/*********************************************************************** ** End of md5.h ********************************* (cut) ******************************** */。
MD5加密C语言实现MD5(Message-Digest Algorithm 5)是一种广泛使用的散列函数,它将任意长度的数据映射为固定长度的散列值。
在本文中,我们将实现一个简单的MD5加密算法的C语言版本。
1.MD5加密算法的基本原理:MD5算法由四个基本步骤组成:填充,初始化,循环计算和摘要。
-填充:将需要加密的数据填充到一个长度为64的倍数的消息块,填充内容为一个1和若干个0,在结尾添加64位表示原始消息长度的二进制表示。
-初始化:初始化四个64位的缓冲区,用于存储计算结果。
-循环计算:将填充后的数据分为若干个消息块,每个消息块都经过四个循环处理(共64轮),根据一系列的位运算对缓冲区进行更新。
-摘要:将经过循环计算后的结果拼接起来,得到最终的128位摘要。
2.C语言实现:下面是一个简单的C语言版本的MD5加密算法实现。
该实现基于RFC1321中的伪代码。
首先,我们需要定义一些常数和辅助函数:```c#include <stdio.h>#include <stdint.h>//初始化缓冲区//将字符串转换为二进制消息块void stringToBinary(const char *str, uint32_t *msg, uint32_t len)uint32_t i;for (i = 0; i < len; i++)msg[i] = ((uint32_t)str[i * 4 + 3] << 24) ,((uint32_t)str[i * 4 + 2] << 16)((uint32_t)str[i * 4 + 1] << 8) , (uint32_t)str[i * 4];}//辅助函数:循环左移uint32_t rotateLeft(uint32_t x, uint32_t n)return (x << n) , (x >> (32 - n));```接下来,我们可以实现MD5算法的四个步骤:-填充:```c//填充消息块void padding(uint32_t *msg, uint32_t len)uint32_t midBitLength = len * 4 * 8;uint32_t padLen = ((midBitLength + 8) / 512 + 1) * 512 / 32 - len; // 计算填充长度msg[len] = 0x80; // 添加一个1uint32_t i;for (i = len + 1; i < len + padLen - 8; i++)msg[i] = 0x00; // 其余位置0}```-初始化:```c//初始化缓冲区void initBufferbuffer[1] = 0xEFCDAB89;buffer[2] = 0x98BADCFE;```-循环计算:```c//对每个消息块进行循环计算void processBlock(uint32_t *block)uint32_t a = buffer[0], b = buffer[1], c = buffer[2], d = buffer[3];uint32_t i;//主循环,共64轮for (i = 0; i < 64; i++)uint32_t f, g;//根据i的值选取相应的函数和常数if (i < 16)f=(b&c),((~b)&d);g=i;}else if (i < 32)f=(d&b),((~d)&c);g=(5*i+1)%16;}else if (i < 48)f=b^c^d;g=(3*i+5)%16;}elsef=c^(b,(~d));g=(7*i)%16;}uint32_t temp = d;d=c;c=b;b = b + rotateLeft(a + f + k[i] + block[g], s[i]);a = temp;}//更新缓冲区buffer[0] += a;buffer[1] += b;buffer[2] += c;buffer[3] += d;```-摘要:```c//生成最终的摘要void generateDigest(uint32_t *msg, uint32_t len)uint32_t i;for (i = 0; i < len / 16; i++)processBlock(msg + i * 16);}```最后,我们可以编写一个简单的主函数来使用这些函数:```cint main//待加密的字符串const char *str = "Hello, MD5!";uint32_t msgLength = (uint32_t)(strlen(str) + 1) / 4; //将字符串转换为二进制消息块uint32_t msg[msgLength];stringToBinary(str, msg, msgLength);//加密padding(msg, msgLength);initBuffer(;generateDigest(msg, msgLength + 64 / 4);//输出结果printf("MD5: ");for (int i = 0; i < 4; i++)printf("%02x", buffer[i] >> 0 & 0xff);printf("%02x", buffer[i] >> 8 & 0xff);printf("%02x", buffer[i] >> 16 & 0xff);printf("%02x", buffer[i] >> 24 & 0xff);}printf("\n");return 0;```这个简单的C语言版本的MD5加密算法实现了填充,初始化,循环计算和摘要等基本步骤。
C语⾔实现MD5校验根据⽹上资料,整理验证C程序代码。
接⼝函数:1/******************************************************2*函数名称:Compute_data_md53*输⼊:data 校验数据⾸地址4 len 校验数据长度5 md5_str 字符串形式的MD5值6*输出:⽆7*功能:计算数据MD5值,并以字符串形式返回8*******************************************************/9int Compute_data_md5(unsigned char *data, unsigned int len,unsigned char *md5_str)View CodeMD5.h 的内容:1 #ifndef _MD5_H_2#define _MD5_H_34#define MD5_SIZE 165#define MD5_STR_LEN (MD5_SIZE * 2)67 typedef struct8 {9 unsigned int count[2];10 unsigned int state[4];11 unsigned char buffer[64];12 } MD5_CTX;1314#define F(x,y,z) ((x & y) | (~x & z))15#define G(x,y,z) ((x & z) | (y & ~z))16#define H(x,y,z) (x^y^z)17#define I(x,y,z) (y ^ (x | ~z))18#define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))1920#define FF(a,b,c,d,x,s,ac) \21 { \22 a += F(b,c,d) + x + ac; \23 a = ROTATE_LEFT(a,s); \24 a += b; \25 }26#define GG(a,b,c,d,x,s,ac) \27 { \28 a += G(b,c,d) + x + ac; \29 a = ROTATE_LEFT(a,s); \30 a += b; \31 }32#define HH(a,b,c,d,x,s,ac) \33 { \34 a += H(b,c,d) + x + ac; \35 a = ROTATE_LEFT(a,s); \36 a += b; \37 }38#define II(a,b,c,d,x,s,ac) \39 { \40 a += I(b,c,d) + x + ac; \41 a = ROTATE_LEFT(a,s); \42 a += b; \43 }44void MD5Init(MD5_CTX *context);45void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen);46void MD5Final(MD5_CTX *context, unsigned char digest[16]);47void MD5Transform(unsigned int state[4], unsigned char block[64]);48void MD5Encode(unsigned char *output, unsigned int *input, unsigned int len);49void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len);5051int Compute_data_md5(unsigned char *data, unsigned int len,unsigned char *md5_str);5253#endifView CodeMD5.c 的内容:1 #include "md5.h"2 #include <memory.h>3 #include <stdio.h>45 unsigned char PADDING[] =6 {70x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,011 };1213void MD5Init(MD5_CTX *context)14 {15 context->count[0] = 0;16 context->count[1] = 0;17 context->state[0] = 0x67452301;18 context->state[1] = 0xEFCDAB89;19 context->state[2] = 0x98BADCFE;20 context->state[3] = 0x10325476;21 }2223void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen)24 {25 unsigned int i = 0;26 unsigned int index = 0;27 unsigned int partlen = 0;2829 index = (context->count[0] >> 3) & 0x3F;30 partlen = 64 - index;31 context->count[0] += inputlen << 3;3233if(context->count[0] < (inputlen << 3))34 context->count[1]++;35 context->count[1] += inputlen >> 29;3637if(inputlen >= partlen)38 {39 memcpy(&context->buffer[index], input,partlen);40 MD5Transform(context->state, context->buffer);4142for(i = partlen; i+64 <= inputlen; i+=64)43 MD5Transform(context->state, &input[i]);4445 index = 0;46 }47else48 {49 i = 0;50 }51 memcpy(&context->buffer[index], &input[i], inputlen-i);52 }5354void MD5Final(MD5_CTX *context, unsigned char digest[16])55 {56 unsigned int index = 0,padlen = 0;57 unsigned char bits[8];5859 index = (context->count[0] >> 3) & 0x3F;60 padlen = (index < 56)?(56-index):(120-index);61 MD5Encode(bits, context->count, 8);62 MD5Update(context, PADDING, padlen);63 MD5Update(context, bits, 8);64 MD5Encode(digest, context->state, 16);65 }6667void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len)68 {69 unsigned int i = 0;70 unsigned int j = 0;7172while(j < len)73 {74 output[j] = input[i] & 0xFF;75 output[j+1] = (input[i] >> 8) & 0xFF;76 output[j+2] = (input[i] >> 16) & 0xFF;77 output[j+3] = (input[i] >> 24) & 0xFF;78 i++;79 j += 4;80 }81 }8283void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len)84 {85 unsigned int i = 0;86 unsigned int j = 0;8788while(j < len)89 {90 output[i] = (input[j]) |91 (input[j+1] << 8) |92 (input[j+2] << 16) |93 (input[j+3] << 24);94 i++;95 j += 4;96 }97 }9899void MD5Transform(unsigned int state[4], unsigned char block[64]) 100 {101 unsigned int a = state[0];102 unsigned int b = state[1];103 unsigned int c = state[2];104 unsigned int d = state[3];105 unsigned int x[64];106107 MD5Decode(x,block,64);108109 FF(a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */110 FF(d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */111 FF(c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */112 FF(b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */113 FF(a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */114 FF(d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */115 FF(c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */116 FF(b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */117 FF(a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */118 FF(d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */119 FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */120 FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */121 FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */122 FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */123 FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */124 FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */125126/* Round 2 */127 GG(a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */128 GG(d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */129 GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */130 GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */131 GG(a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */132 GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */133 GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */134 GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */135 GG(a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */136 GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */137 GG(c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */138 GG(b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */139 GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */140 GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */141 GG(c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */142 GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */143144/* Round 3 */145 HH(a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */146 HH(d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */147 HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */148 HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */149 HH(a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */150 HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */151 HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */152 HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */153 HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */154 HH(d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */155 HH(c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */156 HH(b, c, d, a, x[ 6], 23, 0x4881d05); /* 44 */157 HH(a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */158 HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */159 HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */160 HH(b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */161162/* Round 4 */163 II(a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */164 II(d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */165 II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */166 II(b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */167 II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */168 II(d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */169 II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */170 II(b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */171 II(a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */172 II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */173 II(c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */174 II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */175 II(a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */176 II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */177 II(c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */178 II(b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */179 state[0] += a;180 state[1] += b;181 state[2] += c;182 state[3] += d;183 }184185186/******************************************************187*函数名称:Compute_data_md5188*输⼊:data 校验数据⾸地址189 len 校验数据长度190 md5_str 字符串形式的MD5值191*输出:⽆192*功能:计算数据MD5值,并以字符串形式返回193*******************************************************/194int Compute_data_md5(unsigned char *data, unsigned int len,unsigned char *md5_str) 195 {196 unsigned char md5_value[MD5_SIZE];197 unsigned int i = 0;198199 MD5_CTX md5;200201 MD5Init(&md5);202 MD5Update(&md5, data, len);203 MD5Final(&md5, md5_value);204for(i = 0; i < MD5_SIZE; i++)205 {206 snprintf(md5_str + i*2, 2+1, "%02x", md5_value[i]);207 }208 md5_str[MD5_STR_LEN] = '\0'; // add end209 }210211#if 0212int Compute_file_md5(const char *file_path, char *md5_str)213 {214int i;215int fd;216int ret;217 unsigned char data[READ_DATA_SIZE];218 unsigned char md5_value[MD5_SIZE];219 MD5_CTX md5;220221 fd = open(file_path, O_RDONLY);222if (-1 == fd)223 {224 perror("open");225return -1;226 }227228// init md5229 MD5Init(&md5);230231while (1)232 {233 ret = read(fd, data, READ_DATA_SIZE);234if (-1 == ret)235 {236 perror("read");237return -1;238 }239240 MD5Update(&md5, data, ret);241242if (0 == ret || ret < READ_DATA_SIZE)243 {244break;245 }246 }247248 close(fd);249250 MD5Final(&md5, md5_value);251252for(i = 0; i < MD5_SIZE; i++)253 {254 snprintf(md5_str + i*2, 2+1, "%02x", md5_value[i]); 255 }256 md5_str[MD5_STR_LEN] = '\0'; // add end257258return0;259 }260#endifView Code。
C语言实现MD5算法1.理解MD5算法原理:-MD5算法是一种常用的哈希函数,用于将任意长度的消息转换为固定长度的哈希值,通常为128位。
-MD5算法主要包括四个循环运算和四个非线性函数,通过迭代运算将输入的消息分块处理并输出最终的哈希值。
2.定义MD5算法所需的常量和函数:-定义一个64个元素的常数列表,用于在算法运算中使用。
-定义四个非线性的F,G,H,I函数,用于在循环运算中使用。
3.定义MD5算法所需的全局变量:-定义一个128位的缓冲区保存最终输出的哈希值。
-定义一个64位的计数指示器,记录输入消息的长度。
-定义四个32位的寄存器变量A、B、C、D,用于循环运算过程中保存中间结果。
4.实现MD5算法的主要函数:- 实现Padding函数,将输入消息按照MD5算法的规则进行填充,使其长度满足对512位的整数倍。
-实现FF,GG,HH,II四个函数,用于在每个循环运算中进行非线性转换。
-实现MD5算法的核心循环函数,将输入消息分块处理,并对每个分块进行四轮循环运算,更新寄存器变量的值。
-实现MD5算法的输出函数,将最终运算得到的寄存器变量的值按照一定顺序连接起来,得到最终的128位哈希值。
5.实现MD5算法的入口函数:- 在main函数中,读取输入消息,并调用MD5算法的相关函数,得到最终的哈希值。
-打印输出哈希值。
以下为C语言实现MD5算法的伪代码:```c//定义MD5算法所需的常量和函数const uint32_t s[64] = { ... }; // 常数表const uint32_t k[64] = { ... }; // F,G,H,I函数对应的逻辑常数uint32_t F(uint32_t x, uint32_t y, uint32_t z) { ... } // F 函数uint32_t G(uint32_t x, uint32_t y, uint32_t z) { ... } // G 函数uint32_t H(uint32_t x, uint32_t y, uint32_t z) { ... } // H 函数uint32_t I(uint32_t x, uint32_t y, uint32_t z) { ... } // I 函数//定义MD5算法所需的全局变量uint32_t buffer[4]; // 128位缓冲区uint64_t count = 0; // 输入消息的长度uint32_t A, B, C, D; // 寄存器变量// 实现Padding函数void padding(char* message) { ... }//实现FF,GG,HH,II四个函数void FF(...) { ... }void GG(...) { ... }void HH(...) { ... }void II(...) { ... }//实现MD5算法的核心循环函数void MD5Block(uint32_t* block) { ... }//实现MD5算法的输出函数void MD5Output(uint32_t* digest) { ... }//实现MD5算法的入口函数int maichar message[MAX_LENGTH];uint32_t digest[4];//读取输入消息gets(message);//填充输入消息padding(message);//分块处理for (uint32_t i = 0; i < count; i += 64)MD5Block(&message[i]);}//输出最终结果MD5Output(digest);//打印哈希值printf("%08x%08x%08x%08x", digest[0], digest[1], digest[2], digest[3]);return 0;```以上是一个简单的伪代码,实现了C语言下的MD5算法。
C#一个封装的加密解密类using System;using System.Text;using System.Globalization;using System.Security.Cryptography;using System.Windows.Forms;namespace Space{public class ClsMD5{///<summary>///构造方法///</summary>public ClsMD5(){}///<summary>///使用缺省密钥字符串加密///</summary>///<param name="original">明文</param>///<returns>密文</returns>public static string Encrypt(string original){return Encrypt(original,"JASONHEUNG");}///<summary>///使用缺省密钥解密///</summary>///<param name="original">密文</param>///<returns>明文</returns>public static string Decrypt(string original){return Decrypt(original,"JASONHEUNG",System.Text.Encoding.Default);}///<summary>///使用给定密钥解密///</summary>///<param name="original">密文</param>///<param name="key">密钥</param>///<returns>明文</returns>public static string Decrypt(string original,string key){return Decrypt(original,key,System.Text.Encoding.Default);}///<summary>///使用缺省密钥解密,返回指定编码方式明文///</summary>///<param name="original">密文</param>///<param name="encoding">编码方式</param>///<returns>明文</returns>public static string Decrypt(string original,Encoding encoding){return Decrypt(original,"JASONHEUNG",encoding);}///<summary>///使用给定密钥加密///</summary>///<param name="original">原始文字</param>///<param name="key">密钥</param>///<param name="encoding">字符编码方案</param>///<returns>密文</returns>public static string Encrypt(string original,string key){byte[]buff=System.Text.Encoding.Default.GetBytes(original);byte[]kb=System.Text.Encoding.Default.GetBytes(key);return Convert.ToBase64String(Encrypt(buff,kb));}///<summary>///使用给定密钥解密///</summary>///<param name="encrypted">密文</param>///<param name="key">密钥</param>///<param name="encoding">字符编码方案</param>///<returns>明文</returns>public static string Decrypt(string encrypted,string key,Encoding encoding) {byte[]buff=Convert.FromBase64String(encrypted);byte[]kb=System.Text.Encoding.Default.GetBytes(key);return encoding.GetString(Decrypt(buff,kb));}///<summary>///生成MD5摘要///</summary>///<param name="original">数据源</param>///<returns>摘要</returns>public static byte[]MakeMD5(byte[]original){MD5CryptoServiceProvider hashmd5=new MD5CryptoServiceProvider();byte[]keyhash=puteHash(original);hashmd5=null;return keyhash;}///<summary>///使用给定密钥加密///</summary>///<param name="original">明文</param>///<param name="key">密钥</param>///<returns>密文</returns>public static byte[]Encrypt(byte[]original,byte[]key){TripleDESCryptoServiceProvider des=new TripleDESCryptoServiceProvider();des.Key=MakeMD5(key);des.Mode=CipherMode.ECB;return des.CreateEncryptor().TransformFinalBlock(original,0, original.Length);}///<summary>///使用给定密钥解密数据///</summary>///<param name="encrypted">密文</param>///<param name="key">密钥</param>///<returns>明文</returns>public static byte[]Decrypt(byte[]encrypted,byte[]key){TripleDESCryptoServiceProvider des=new TripleDESCryptoServiceProvider();des.Key=MakeMD5(key);des.Mode=CipherMode.ECB;return des.CreateDecryptor().TransformFinalBlock(encrypted,0, encrypted.Length);}///<summary>///使用给定密钥加密///</summary>///<param name="original">原始数据</param>///<param name="key">密钥</param>///<returns>密文</returns>public static byte[]Encrypt(byte[]original){byte[]key=System.Text.Encoding.Default.GetBytes("JASONHEUNG");return Encrypt(original,key);}///<summary>///使用缺省密钥解密数据///</summary>///<param name="encrypted">密文</param>///<param name="key">密钥</param>///<returns>明文</returns>public static byte[]Decrypt(byte[]encrypted){byte[]key=System.Text.Encoding.Default.GetBytes("JASONHEUNG");return Decrypt(encrypted,key);}}}。