1. MD5的概述

MD5由美国密码学家罗纳德·李维斯特(Ronald Linn Rivest)设计,于1992年公开,是一个典型的密码散列函数,MD5方法将所有的文件看作为一个个的文本文件,通过不可逆 的字符串变化算法可以将输入的报文信息转化生成一个128位的散列值。如果报文的内容发生变化,通过哈希算法得到的散列值也会相应地发生变化。

  1. MD5的特点

a) MD5的过程是不可逆的

b)MD5具有高度的离散性

c)产生固定长度的输出结果

d)抵制弱碰撞性

  1. MD5的过程
  1. 添加填充位

报文的输入长度可以是任意值,首先需要对输入的报文进行填充处理,使得该报文添加填充位之后的报文长度模512余448,若输入的报文已经满足模余的要求,依旧需要进行填充512位。

图1 报文格式的填充 1

填充位最小长度位最小长度为1,最大长度为512。

  1. 填充长度项

填充的的最后64位表示了该报文的未补位之前的长度,大于2^64bit的报文,舍弃掉报文的整数部分,保留余数。

填充结束之后,报文的总长度为512的整数倍。

图2 填充长度项 1

  1. 初始化

初始化MD5的四个寄存器,每个寄存器都为32位,初始化的内容为: A=67452301

B=EFCDAB89

C=98BADCFE

D=10325476

  1. 分块:按照每组512比特将报文进行分组,多轮压缩:依次对于每组的内容进行单向的哈希变换,每次变换之后使寄存器的值发生变化。

图3 总的操作流程 1

第k个数据块的变化如下所示,首先保存第k-1次的ABCD的值,然后输入第k个数据块,对于ABCD的进行变换。

图4 每个分块的操作 1

在过程中用到的Disturb Functions定义如下:

FF(a,b,c,d,Mj,s,ti)  a=b+(a+F(b,c,d)+Mj+ti)<<s

GG(a,b,c,d,Mj,s,ti)  a=b+(a+G(b,c,d)+Mj+ti)<< s

HH(a,b,c,d,Mj,s,ti)  a=b+(a+H(b,c,d)+Mj+ti)<< s

II(a,b,c,d,Mj,s,ti)  a=b+(a+I(b,c,d)+Mj+ti)<< s

上式中:Mj(0<j<=15)为报文的第j个分块。

<<循环左移 S是表示循环左移s个bit,ti=[4294967296]*abs(sin(i))]标识操作的步骤

四个函数的Fx(b,c,d)定义如下:

F(x,y,z)=(x&y)|((~x)&z)

G(x,y,z)=(x&z)|(y&(~z))

H(x,y,z)=x^y^z

I(x,y,z)=y^(x|(~z))

当所有的报文段都处理完毕之后,最后的输出为 ABCD四个寄存器中内容的级联,从A的低字节开始,直到D的高字节。该值即为该报文的报文摘要。

A=A+AA B=B+BB

C=C+CC D=D+DD

代码实现:

#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<fstream>namespace  md5 {//UINT4 defines a four byte wordtypedef unsigned int UINT4;typedef unsigned char* POINTER;//define four auxiliary functions
#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))))using std::string;using std::cin;using std::cout;using std::endl;//void FF(UINT4& a, UINT4 b, UINT4 c, UINT4 d, UINT4 m, UINT4 s, UINT4 t){a = b + ROTATE_LEFT((a + F(b, c, d) + m + t), s);}void GG(UINT4& a, UINT4 b, UINT4 c, UINT4 d, UINT4 m, UINT4 s, UINT4 t){a = b + ROTATE_LEFT((a + G(b, c, d) + m + t), s);}void HH(UINT4& a, UINT4 b, UINT4 c, UINT4 d, UINT4 m, UINT4 s, UINT4 t){a = b + ROTATE_LEFT((a + H(b, c, d) + m + t), s);}void II(UINT4& a, UINT4 b, UINT4 c, UINT4 d, UINT4 m, UINT4 s, UINT4 t){a = b + ROTATE_LEFT((a + I(b, c, d) + m + t), s);}typedef struct {UINT4 state[4];                                   /* state (ABCD) */UINT4 count[2];        /* number of bits, modulo 2^64 (lsb first) */unsigned char buffer[64];                         /* input buffer */} MD5_CTX;//Initialize MD Buffervoid MD5Init(MD5_CTX* context){context->count[0] = context->count[1] = 0;/* Load magic initialization constants.*/context->state[0] = 0x67452301;context->state[1] = 0xefcdab89;context->state[2] = 0x98badcfe;context->state[3] = 0x10325476;}//k[i]=4294967296*abs(sin(i))const unsigned int t[] = {0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,0xa679438e,0x49b40821,0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391 };//循环左移位数const unsigned int  s[] = {7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21 };static unsigned char PADDING[] ={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};static void MD5_memcpy(POINTER output, POINTER input, unsigned int len){unsigned int i;for (i = 0; i < len; i++)output[i] = input[i];}static void MD5_memset(POINTER output, int value, unsigned int len){unsigned int i;for (i = 0; i < len; i++)((char*)output)[i] = (char)value;}static void MD5_print(unsigned char digest[16]){unsigned int i;for (i = 0; i < 16; i++){printf("%02x", digest[i]);}}//8->32static void Decode(UINT4* output, unsigned char* input, unsigned int len){unsigned int i, j;for (i = 0, j = 0; j < len; j += 4, i++){output[i] = ((UINT4)input[j]) | (((UINT4)input[j + 1]) << 8) | (((UINT4)input[j + 2]) << 16) | (((UINT4)input[j + 3]) << 24);}}//32->8static void Encode(unsigned char* output, UINT4* input, unsigned int len){unsigned int i, j;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);}}//md5基本转换static void  MD5Transform(UINT4 state[4], unsigned char block[64]){UINT4 a = state[0], b = state[1], c = state[2], d = state[3], M[16];Decode(M, block, 64);int i = 0;FF(a, b, c, d, M[0], s[i], t[i]); i++;FF(d, a, b, c, M[1], s[i], t[i]); i++;FF(c, d, a, b, M[2], s[i], t[i]); i++;FF(b, c, d, a, M[3], s[i], t[i]); i++;FF(a, b, c, d, M[4], s[i], t[i]); i++;FF(d, a, b, c, M[5], s[i], t[i]); i++;FF(c, d, a, b, M[6], s[i], t[i]); i++;FF(b, c, d, a, M[7], s[i], t[i]); i++;FF(a, b, c, d, M[8], s[i], t[i]); i++;FF(d, a, b, c, M[9], s[i], t[i]); i++;FF(c, d, a, b, M[10], s[i], t[i]); i++;FF(b, c, d, a, M[11], s[i], t[i]); i++;FF(a, b, c, d, M[12], s[i], t[i]); i++;FF(d, a, b, c, M[13], s[i], t[i]); i++;FF(c, d, a, b, M[14], s[i], t[i]); i++;FF(b, c, d, a, M[15], s[i], t[i]); i++;GG(a, b, c, d, M[1], s[i], t[i]); i++;GG(d, a, b, c, M[6], s[i], t[i]); i++;GG(c, d, a, b, M[11], s[i], t[i]); i++;GG(b, c, d, a, M[0], s[i], t[i]); i++;GG(a, b, c, d, M[5], s[i], t[i]); i++;GG(d, a, b, c, M[10], s[i], t[i]); i++;GG(c, d, a, b, M[15], s[i], t[i]); i++;GG(b, c, d, a, M[4], s[i], t[i]); i++;GG(a, b, c, d, M[9], s[i], t[i]); i++;GG(d, a, b, c, M[14], s[i], t[i]); i++;GG(c, d, a, b, M[3], s[i], t[i]); i++;GG(b, c, d, a, M[8], s[i], t[i]); i++;GG(a, b, c, d, M[13], s[i], t[i]); i++;GG(d, a, b, c, M[2], s[i], t[i]); i++;GG(c, d, a, b, M[7], s[i], t[i]); i++;GG(b, c, d, a, M[12], s[i], t[i]); i++;HH(a, b, c, d, M[5], s[i], t[i]); i++;HH(d, a, b, c, M[8], s[i], t[i]); i++;HH(c, d, a, b, M[11], s[i], t[i]); i++;HH(b, c, d, a, M[14], s[i], t[i]); i++;HH(a, b, c, d, M[1], s[i], t[i]); i++;HH(d, a, b, c, M[4], s[i], t[i]); i++;HH(c, d, a, b, M[7], s[i], t[i]); i++;HH(b, c, d, a, M[10], s[i], t[i]); i++;HH(a, b, c, d, M[13], s[i], t[i]); i++;HH(d, a, b, c, M[0], s[i], t[i]); i++;HH(c, d, a, b, M[3], s[i], t[i]); i++;HH(b, c, d, a, M[6], s[i], t[i]); i++;HH(a, b, c, d, M[9], s[i], t[i]); i++;HH(d, a, b, c, M[12], s[i], t[i]); i++;HH(c, d, a, b, M[15], s[i], t[i]); i++;HH(b, c, d, a, M[2], s[i], t[i]); i++;II(a, b, c, d, M[0], s[i], t[i]); i++;II(d, a, b, c, M[7], s[i], t[i]); i++;II(c, d, a, b, M[14], s[i], t[i]); i++;II(b, c, d, a, M[5], s[i], t[i]); i++;II(a, b, c, d, M[12], s[i], t[i]); i++;II(d, a, b, c, M[3], s[i], t[i]); i++;II(c, d, a, b, M[10], s[i], t[i]); i++;II(b, c, d, a, M[1], s[i], t[i]); i++;II(a, b, c, d, M[8], s[i], t[i]); i++;II(d, a, b, c, M[15], s[i], t[i]); i++;II(c, d, a, b, M[6], s[i], t[i]); i++;II(b, c, d, a, M[13], s[i], t[i]); i++;II(a, b, c, d, M[4], s[i], t[i]); i++;II(d, a, b, c, M[11], s[i], t[i]); i++;II(c, d, a, b, M[2], s[i], t[i]); i++;II(b, c, d, a, M[9], s[i], t[i]); i++;state[0] += a;state[1] += b;state[2] += c;state[3] += d;MD5_memset((POINTER)M, 0, sizeof(M));}void MD5_Update(MD5_CTX* context, unsigned char* input, unsigned int inputLen){unsigned int i, index, partLen;//paddingindex = (UINT4)((context->count[0] >> 3) & 0x3F);//进位 if ((context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3)){context->count[1]++;}context->count[1] += ((UINT4)inputLen >> 29);partLen = 64 - index;if (inputLen >= partLen){MD5_memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);MD5Transform(context->state, context->buffer);for (i = partLen; i + 63 < inputLen; i += 64)MD5Transform(context->state, &input[i]);index = 0;}elsei = 0;MD5_memcpy((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen - i);}string toHex(UINT4 num){int x;string tp;char HexArray[] = "0123456789abcdef";string str = "";for (int i = 0; i < 4; i++){tp = "";x = ((num >> i * 8) % (1 << 8)) & 0xff;for (int j = 0; j < 2; j++){tp.insert(0, 1, HexArray[x % 16]);x /= 16;}str += tp;}return str;}string MD5_Final(unsigned char digest[16], MD5_CTX* context){unsigned char bits[8];unsigned int index, padLen;Encode(bits, context->count, 8);//填充操作index = (unsigned int)((context->count[0] >> 3) & 0x3f);padLen = (index < 56) ? (56 - index) : (120 - index);MD5_Update(context, PADDING, padLen);//长度MD5_Update(context, bits, 8);string s = "";for (int i = 0; i < 4; i++)s += toHex(context->state[i]);cout << s;//摘要中的存储状态Encode(digest, context->state, 16);MD5_memset((POINTER)context, 0, sizeof(*context));return s;}string MDString(char* string){MD5_CTX context;char* digest = new char[16];unsigned int len = strlen(string);MD5Init(&context);MD5_Update(&context, (unsigned char*)string, len);std::string  s = MD5_Final((unsigned char*)digest, &context);//MD5_print((unsigned char*)digest);return s;}string MDFile(char* filename){MD5_CTX context;int len;unsigned char buffer[1024], digest[16];std::fstream inFile;inFile.open(filename, std::ios::binary | std::ios::in);if (!inFile.is_open()) {cout << "Could not open the file:" << endl;cout << "Program terminating!\n";exit(EXIT_FAILURE);}else{MD5Init(&context);while (inFile.good() && !inFile.eof()){inFile.read((char*)buffer, 1024);MD5_Update(&context, buffer, inFile.gcount());}std::string s = MD5_Final(digest, &context);//MD5_print(digest);return s;}}}

MD5算法原理及实现相关推荐

  1. C++安全方向openssl(三):3.2 md5算法原理详解以及代码实现

    如下图: 由上可知,任意大小的数据经过md5算法是都是4个字节. 涉及到新的安全相关的内容,不再用md5了.通过md5算法的分析我们应该知道我们通过什么方式实现不可逆,又是通过什么方式实现修改一处内容 ...

  2. MD5算法原理简要介绍并采用C#应用在桌面应用系统的用户登录与注册中

    MD5算法原理简要介绍并采用C#应用在桌面应用系统的用户登录与注册中 接上文,本文简要介绍一下MD5加密算法的原理,并采用C#实现MD5算法的加密与解密过程,将这一实现过程应用在我自己开发的桌面办公应 ...

  3. MD5算法原理与常用实现

    目录 定义 MD5特点 常见应用场景 1.校验文件的完整性 2.存储用户密码 原理 1.填补信息 2.拿到初始值 3.真正的计算 MD5为什么不可逆 java实现和使用 定义 MD全称Message- ...

  4. php md5 file算法原理,MD5算法原理与实现

    //MessageDigestAlgorithm5.cpp #include "stdafx.h" #include "MessageDigestAlgorithm5.h ...

  5. 【密码学】MD5算法原理

    MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2.MD3和MD4发展而来.MD5算法的使用不需要支付任何版权费用. MD5功能:     ...

  6. 【编程开发】MD5算法原理

     MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2.MD3和MD4发展而来.MD5算法的使用不需要支付任何版权费用. MD5功能:     ...

  7. MD5算法原理及其实现

    什么是MD5算法 MD5讯息摘要演算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码杂凑函数,可以产生出一个128位元(16位元组)的散列值(hash val ...

  8. MD5算法的编程实现

    MD5算法的编程实现 [实验目的] [实验环境] [实验预备知识点] [实验内容] [实验步骤] [实验思考题] [实验目的] 1.理解Hash函数的计算原理和特点. 2.理解MD5算法原理. 3.了 ...

  9. MD5加密算法原理及一些其他的加密算法

    Md5加密算法: 概述:md5算法也可以称为消息摘要算法,属于hash算法的一种,md5算法对输入的任意长度的消息进行运行,然后产生一个128位的消息摘要 特点: #不可逆性 唯一性:相同数据的md5 ...

最新文章

  1. Mybatis框架中是如何获取到SQL语句的,让我们一起来模拟一下吧
  2. iptables 生效_Linux防火墙Firewall和Iptables的使用
  3. 利用mochiweb让服务端主动推送数据至前端页面
  4. Python数据分析教程:Numpy 中不得不知的4个重要函数
  5. [转载]SIFT(尺度不变特征变换)算法小结
  6. python数据结构剑指offer-从尾到头打印链表
  7. python的浮点数占字节_Python二进制文件读取并转换为浮点数详解
  8. 常见linux服务器存储空间,全面了解linux服务器的常用命令总结
  9. Atitit 编程语言语言规范总结 语法部分 目录 1. 语言规范 3 2. Types 3 2.1.1. Primitive types 3 2.1.2. Compound types 4 3.
  10. github搭建php,在github规范开发以及持续构建php项目
  11. Word文档单页页眉横线如何去除
  12. 【分享】Gitee如何下载单个文件
  13. 网络科学论坛纪要-2012
  14. 【问题记录】怎么用python读取CIFAR10数据集?
  15. matlab函数equalize
  16. Win系统 - 从USB3.0端口启动安装Windows系统,提示缺少驱动器设备驱动程序的解决办法
  17. 二叉树遍历方式-先序、中序、后序和层序遍历(JAVA)
  18. vertical-align作用,基线详解
  19. ios莫名其妙闪退的解决方法
  20. linux驱动开发学习2 设备树

热门文章

  1. Python 获取字典中的第一个键
  2. 利用Idea生成的类图分析框架源码
  3. 访问System x3650 IMM2的几种方式
  4. 全球及中国自行车碳纤维车架行业商业模式分析及投资风险预测2022年版
  5. mysql 中 RPAD函数
  6. 拓嘉辰丰电商:拼多多怎样下架商品退出资源位
  7. k8s查看pod的yaml文件_K8S教程(6)YAML资源配置清单
  8. 2022年常问redis面试题
  9. 矩阵键盘线反法C语言,矩阵键盘线反转法实现
  10. 注册公司的公章遗失了怎么办?今天就教你如何处理!