MD5算法程序C++.docx

上传人:b****8 文档编号:9704144 上传时间:2023-05-20 格式:DOCX 页数:27 大小:23.80KB
下载 相关 举报
MD5算法程序C++.docx_第1页
第1页 / 共27页
MD5算法程序C++.docx_第2页
第2页 / 共27页
MD5算法程序C++.docx_第3页
第3页 / 共27页
MD5算法程序C++.docx_第4页
第4页 / 共27页
MD5算法程序C++.docx_第5页
第5页 / 共27页
MD5算法程序C++.docx_第6页
第6页 / 共27页
MD5算法程序C++.docx_第7页
第7页 / 共27页
MD5算法程序C++.docx_第8页
第8页 / 共27页
MD5算法程序C++.docx_第9页
第9页 / 共27页
MD5算法程序C++.docx_第10页
第10页 / 共27页
MD5算法程序C++.docx_第11页
第11页 / 共27页
MD5算法程序C++.docx_第12页
第12页 / 共27页
MD5算法程序C++.docx_第13页
第13页 / 共27页
MD5算法程序C++.docx_第14页
第14页 / 共27页
MD5算法程序C++.docx_第15页
第15页 / 共27页
MD5算法程序C++.docx_第16页
第16页 / 共27页
MD5算法程序C++.docx_第17页
第17页 / 共27页
MD5算法程序C++.docx_第18页
第18页 / 共27页
MD5算法程序C++.docx_第19页
第19页 / 共27页
MD5算法程序C++.docx_第20页
第20页 / 共27页
亲,该文档总共27页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

MD5算法程序C++.docx

《MD5算法程序C++.docx》由会员分享,可在线阅读,更多相关《MD5算法程序C++.docx(27页珍藏版)》请在冰点文库上搜索。

MD5算法程序C++.docx

MD5算法程序C++

2、具体的实现理论

2.1算法概述

MD5算法是MD4算法的改进算法。

RonRivest于1990年提出MD4单向散列函数,MD表示消息摘要(MessageDigest),对输入消息,算法产生128位散列值。

该算法首次公布之后,BertdenBoer和AntoonBosselaers对算法三轮中的后两轮进行了成功的密码分析。

在一个不相关的分析结果中,RalphMerKle成功地攻击了前两轮。

尽管这些攻击都没有扩展到整个算法,但Rivest还是改进了其算法,结果就是MD5算法。

MD5算法是MD4的改进算法,它比MD4更复杂,但设计思想相似,输入的消息可任意长,输出结果也仍为128位,特别适用于高速软件实现,是基于32-位操作数的一些简单的位操作。

2.2算法步骤

l将输入消息按512-位分组,最后要填充成为512位的整数倍,且最后一组的后64位用来填充消息长度(填充前)。

填充方法为附一个1在消息后,后接所要求的多个0。

这样可以确保不同消息在填充后不相同。

l由于留出64位用来表示消息长度,那么消息的长度最多可达264字节,相当于4G×4G字节,文件的长度是不可能达到这么大,因此通常都是只采用64位中的低32位来表示消息长度,高32位填充0。

l初始化MD变量。

由于每轮输出128位,这128位可用下面四个32位字A,B,C,D来表示。

其初始值设为:

A=0x01234567

B=0x89ABCDEF

C=0xFEDCBA98

D=0x76543210

l开始进入算法主循环,循环的次数是消息中512位消息分组的数目。

先将上面A、B、C、D四个变量分别复制到另外四个变量a、b、c、d中去。

主循环有四轮,每轮很相似。

每轮进行16次操作,每次操作对a、b、c、d四个变量中的三个作一次非线性函数运算,然后将所得结果加上第四个变量,消息的一个子分组和一个常数。

再将所得结果向右环移一个不定的数,并加上a,b,c或d中之一。

最后用该结果取代a,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))

其中,⊕是异或,∧是与,∨是或,是反符号。

这些函数是这样设计的:

如果X、Y和Z的对应位是独立和均匀的,那么结果的每一位也应是独立和均匀的。

函数F是按逐位方式操作:

如果X,那么Y,否则Z。

函数H是逐位奇偶操作符。

设Mj表示消息的第j个子分组(从0到15),<<

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

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

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

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

四轮(64步)结果略。

注:

常数ti的选择:

第i步中,ti是232×abs(sin(i))的整数部分,i的单位是弧度。

所有这些完成之后,将A,B,C,D分别加上a,b,c,d。

然后用下一分组数据继续运行算法,最后的输出是A,B,C和D的级联。

l最后得到的A,B,C,D就是输出结果,A是低位,D为高位,DCBA组成128位输出结果。

2.3MD5的安全性

RonRivest概述了MD5安全性[8]:

l与MD4相比,增加了第四轮。

l每一步均有唯一的加法常数。

l为减弱第二轮中函数G的对称性从((X∧Y)∨(X∧Z)∨(Y∧Z))变为((X∧Z)∨(Y∧(Z)))。

l每一步加上了上一步的结果,引起更快的雪崩效应。

l改变了第二轮和第三轮中访问消息子分组的次序,使其形式更不相似。

l近似优化了每一轮中的循环左移位移量以实现更快的雪崩效应。

各轮的位移量互不相同。

从安全角度讲,MD5的输出为128位,若采用纯强力攻击寻找一个消息具有给定Hash值的计算困难性为2128,用每秒可试验1000000000个消息的计算机需时1.07×1022年。

若采用生日攻击法,寻找有相同Hash值的两个消息需要试验264个消息,用每秒可试验1000000000个消息的计算机需时585年。

二、实现方法

由于此处的文件校验用到要求比较高的场合,故采用了方法6,md5校验算法,从CodeGuru下载了一个md5校验算法的实现模块,加入自己要校验的文件名,实现完成。

下面具体描述一下实现过程:

1、创建一个简单的对话框程序;

2、设置CString类型的变量m_filename和m_strFileChecksum以存放要校验的文件名和校验和;

3、在对话框类中创建ChecksumSelectedFile()函数,调用md5校验和类(附录中有其实现文件)中的GetMD5计算文件校验和。

4、使用定时器定时巡检该文件的校验和,一旦发现校验和发生变化,立刻出现提示。

三、附录(md5算法实现的源码)

以下代码实现均来自。

1、MD5ChecksumDefines.h(定义相关常量的头文件)

//Magicinitializationconstants

#defineMD5_INIT_STATE_00x67452301

#defineMD5_INIT_STATE_10xefcdab89

#defineMD5_INIT_STATE_20x98badcfe

#defineMD5_INIT_STATE_30x10325476

//ConstantsforTransformroutine.

#defineMD5_S117

#defineMD5_S1212

#defineMD5_S1317

#defineMD5_S1422

#defineMD5_S215

#defineMD5_S229

#defineMD5_S2314

#defineMD5_S2420

#defineMD5_S314

#defineMD5_S3211

#defineMD5_S3316

#defineMD5_S3423

#defineMD5_S416

#defineMD5_S4210

#defineMD5_S4315

#defineMD5_S4421

//TransformationConstants-Round1

#defineMD5_T010xd76aa478//TransformationConstant1

#defineMD5_T020xe8c7b756//TransformationConstant2

#defineMD5_T030x242070db//TransformationConstant3

#defineMD5_T040xc1bdceee//TransformationConstant4

#defineMD5_T050xf57c0faf//TransformationConstant5

#defineMD5_T060x4787c62a//TransformationConstant6

#defineMD5_T070xa8304613//TransformationConstant7

#defineMD5_T080xfd469501//TransformationConstant8

#defineMD5_T090x698098d8//TransformationConstant9

#defineMD5_T100x8b44f7af//TransformationConstant10

#defineMD5_T110xffff5bb1//TransformationConstant11

#defineMD5_T120x895cd7be//TransformationConstant12

#defineMD5_T130x6b901122//TransformationConstant13

#defineMD5_T140xfd987193//TransformationConstant14

#defineMD5_T150xa679438e//TransformationConstant15

#defineMD5_T160x49b40821//TransformationConstant16

//TransformationConstants-Round2

#defineMD5_T170xf61e2562//TransformationConstant17

#defineMD5_T180xc040b340//TransformationConstant18

#defineMD5_T190x265e5a51//TransformationConstant19

#defineMD5_T200xe9b6c7aa//TransformationConstant20

#defineMD5_T210xd62f105d//TransformationConstant21

#defineMD5_T220x02441453//TransformationConstant22

#defineMD5_T230xd8a1e681//TransformationConstant23

#defineMD5_T240xe7d3fbc8//TransformationConstant24

#defineMD5_T250x21e1cde6//TransformationConstant25

#defineMD5_T260xc33707d6//TransformationConstant26

#defineMD5_T270xf4d50d87//TransformationConstant27

#defineMD5_T280x455a14ed//TransformationConstant28

#defineMD5_T290xa9e3e905//TransformationConstant29

#defineMD5_T300xfcefa3f8//TransformationConstant30

#defineMD5_T310x676f02d9//TransformationConstant31

#defineMD5_T320x8d2a4c8a//TransformationConstant32

//TransformationConstants-Round3

#defineMD5_T330xfffa3942//TransformationConstant33

#defineMD5_T340x8771f681//TransformationConstant34

#defineMD5_T350x6d9d6122//TransformationConstant35

#defineMD5_T360xfde5380c//TransformationConstant36

#defineMD5_T370xa4beea44//TransformationConstant37

#defineMD5_T380x4bdecfa9//TransformationConstant38

#defineMD5_T390xf6bb4b60//TransformationConstant39

#defineMD5_T400xbebfbc70//TransformationConstant40

#defineMD5_T410x289b7ec6//TransformationConstant41

#defineMD5_T420xeaa127fa//TransformationConstant42

#defineMD5_T430xd4ef3085//TransformationConstant43

#defineMD5_T440x04881d05//TransformationConstant44

#defineMD5_T450xd9d4d039//TransformationConstant45

#defineMD5_T460xe6db99e5//TransformationConstant46

#defineMD5_T470x1fa27cf8//TransformationConstant47

#defineMD5_T480xc4ac5665//TransformationConstant48

//TransformationConstants-Round4

#defineMD5_T490xf4292244//TransformationConstant49

#defineMD5_T500x432aff97//TransformationConstant50

#defineMD5_T510xab9423a7//TransformationConstant51

#defineMD5_T520xfc93a039//TransformationConstant52

#defineMD5_T530x655b59c3//TransformationConstant53

#defineMD5_T540x8f0ccc92//TransformationConstant54

#defineMD5_T550xffeff47d//TransformationConstant55

#defineMD5_T560x85845dd1//TransformationConstant56

#defineMD5_T570x6fa87e4f//TransformationConstant57

#defineMD5_T580xfe2ce6e0//TransformationConstant58

#defineMD5_T590xa3014314//TransformationConstant59

#defineMD5_T600x4e0811a1//TransformationConstant60

#defineMD5_T610xf7537e82//TransformationConstant61

#defineMD5_T620xbd3af235//TransformationConstant62

#defineMD5_T630x2ad7d2bb//TransformationConstant63

#defineMD5_T640xeb86d391//TransformationConstant64

 

//Nulldata(exceptforfirstBYTE)usedtofinalisethechecksumcalculation

staticunsignedcharPADDING[64]={

0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

};

2、CountChecksum.h(md5校验和类的头文件)

classCMD5Checksum

{

public:

//interfacefunctionsfortheRSAMD5calculation

staticCStringGetMD5(BYTE*pBuf,UINTnLength);

staticCStringGetMD5(CFile&File);

staticCStringGetMD5(constCString&strFilePath);

protected:

//constructor/destructor

CMD5Checksum();

virtual~CMD5Checksum(){};

//RSAMD5implementation

voidTransform(BYTEBlock[64]);

voidUpdate(BYTE*Input,ULONGnInputLen);

CStringFinal();

inlineDWORDRotateLeft(DWORDx,intn);

inlinevoidFF(DWORD&A,DWORDB,DWORDC,DWORDD,DWORDX,DWORDS,DWORDT);

inlinevoidGG(DWORD&A,DWORDB,DWORDC,DWORDD,DWORDX,DWORDS,DWORDT);

inlinevoidHH(DWORD&A,DWORDB,DWORDC,DWORDD,DWORDX,DWORDS,DWORDT);

inlinevoidII(DWORD&A,DWORDB,DWORDC,DWORDD,DWORDX,DWORDS,DWORDT);

//utilityfunctions

voidDWordToByte(BYTE*Output,DWORD*Input,UINTnLength);

voidByteToDWord(DWORD*Output,BYTE*Input,UINTnLength);

private:

BYTEm_lpszBuffer[64];//inputbuffer

ULONGm_nCount[2];//numberofbits,modulo2^64(lsbfirst)

ULONGm_lMD5[4];//MD5checksum

};

#endif//!

defined(AFX_MD5CHECKSUM_H__2BC7928E_4C15_11D3_B2EE_A4A60E20D2C3__INCLUDED_)

3、CountChecksum.cpp(md5校验和类的实现文件)

/*****************************************************************************************

FUNCTION:

CMD5Checksum:

:

GetMD5

DETAILS:

static,public

DESCRIPTION:

GetstheMD5checksumforaspecifiedfile

RETURNS:

CString:

thehexadecimalMD5checksumforthespecifiedfile

ARGUMENTS:

CString&strFilePath:

thefullpathnameofthespecifiedfile

NOTES:

ProvidesaninterfacetotheCMD5Checksumclass.'strFilePath'nameshould

holdthefullpathnameofthefile,egC:

\MyDocuments\Arcticle.txt.

NB.Ifanyproblemsoccurwithopeningorreadingthisfile,aCFileException

willbethrown;callersofthisfunctionshouldbereadytocatchthis

exception.

*****************************************************************************************/

CStringCMD5Checksum:

:

GetMD5(constCString&strFilePath)

{

//openthefileasabinaryfileinreadonlymode,denyingwriteaccess

CFileFile(strFilePath,CFile:

:

shareDenyNone);

//thefilehasbeensuccessfullyopened,sonowgetandreturnitschecksum

returnGetMD5(File);

}

 

/*****************************************************************************************

FUNCTION:

CMD5Checksum:

:

GetMD5

DETAILS:

static,public

DESCRIPTION:

GetstheMD5checksumforaspecifiedfile

RETURNS:

CString:

thehexadecimalMD5checksumforthespecifiedfile

ARGUMENTS:

CFile&File:

thespecifiedfile

NOTES:

ProvidesaninterfacetotheCMD5Checksumclass.'File'shouldbeopenin

binaryreadonlymodebeforecallingthisfunction.

NB.CallersofthisfunctionshouldbereadytocatchanyCFileException

thrownbytheCFilefunctions

*****************************************************************************************/

CStringCMD5Checksum:

:

GetMD5(CFile&File)

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 法律文书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2