应用密码学第6上机及作业Word文件下载.docx
- 文档编号:18762936
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:7
- 大小:17.14KB
应用密码学第6上机及作业Word文件下载.docx
《应用密码学第6上机及作业Word文件下载.docx》由会员分享,可在线阅读,更多相关《应用密码学第6上机及作业Word文件下载.docx(7页珍藏版)》请在冰豆网上搜索。
shaSuccess=0,
shaNull,
/**//*Nullpointerparameter*/
shaInputTooLong,
/**//*inputdatatoolong*/
shaStateError
/**//*calledInputafterResult*/
};
#endif
#defineSHA1HashSize20
typedefstructSHA1Context
DWORDIntermediate_Hash[SHA1HashSize/4];
//MessageDigest
DWORDLength_Low;
//Messagelengthinbits
DWORDLength_High;
intMessage_Block_Index;
//Indexintomessageblockarray
unsignedcharMessage_Block[64];
//512-bitmessageblocks
intComputed;
//Isthedigestcomputed?
intCorrupted;
//Isthemessagedigestcorrupted?
}SHA1Context;
//FunctionPrototypes
CStringGetSHA1String(CStringsSource);
intSHA1Reset(SHA1Context*);
intSHA1Input(SHA1Context*,constunsignedchar*,unsignedint);
intSHA1Result(SHA1Context*,unsignedcharMessage_Digest[SHA1HashSize]);
CStringSha1toBase32(constunsignedchar*);
==================================================================================
#include"
sha1.h"
//DefinetheSHA1circularleftshiftmacro
#defineSHA1CircularShift(bits,word)(((word)<
<
(bits))|((word)>
>
(32-(bits))))
//LocalFunctionPrototyptes*/
voidSHA1PadMessage(SHA1Context*);
voidSHA1ProcessMessageBlock(SHA1Context*);
intSHA1Reset(SHA1Context*c)
if(!
c)
returnshaNull;
c->
Length_Low
=0;
Length_High
Message_Block_Index
Intermediate_Hash[0]
=0x67452301;
Intermediate_Hash[1]
=0xEFCDAB89;
Intermediate_Hash[2]
=0x98BADCFE;
Intermediate_Hash[3]
=0x10325476;
Intermediate_Hash[4]
=0xC3D2E1F0;
Computed
Corrupted=0;
returnshaSuccess;
}
intSHA1Result(SHA1Context*c,unsignedcharMessage_Digest[SHA1HashSize])
inti;
c||!
Message_Digest)
if(c->
Corrupted)
returnc->
Corrupted;
c->
Computed)
...{
SHA1PadMessage(c);
for(i=0;
i<
64;
Message_Block[++i]=0)
;
Length_Low=0;
/**//*andclearlength*/
Length_High=0;
Computed=1;
}
i<
SHA1HashSize;
++i)
Message_Digest[i]=c->
Intermediate_Hash[i>
2]>
8*(3-(i&
0x03));
intSHA1Input(SHA1Context*context,constunsignedchar*message_array,unsignedlength)
length)
context||!
message_array)
if(context->
context->
Corrupted=shaStateError;
returnshaStateError;
returncontext->
while(length--&
&
!
context->
Message_Block[context->
Message_Block_Index++]=(*message_array&
0xFF);
Length_Low+=8;
Length_Low==0)
Length_High++;
Length_High==0)
/**//*Messageistoolong*/
Corrupted=1;
Message_Block_Index==64)
SHA1ProcessMessageBlock(context);
message_array++;
voidSHA1ProcessMessageBlock(SHA1Context*context)
constDWORDK[]=
...{0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6};
int
t;
DWORDtemp;
DWORDW[80];
DWORDA,B,C,D,E;
/**//*
*Initializethefirst16wordsinthearrayW
*/
for(t=0;
t<
16;
t++)
W[t]=context->
Message_Block[t*4]<
24;
W[t]|=context->
Message_Block[t*4+1]<
Message_Block[t*4+2]<
8;
Message_Block[t*4+3];
for(t=16;
80;
W[t]=SHA1CircularShift(1,W[t-3]^W[t-8]^W[t-14]^W[t-16]);
A=context->
Intermediate_Hash[0];
B=context->
Intermediate_Hash[1];
C=context->
Intermediate_Hash[2];
D=context->
Intermediate_Hash[3];
E=context->
Intermediate_Hash[4];
20;
temp=SHA1CircularShift(5,A)+
((B&
C)|((~B)&
D))+E+W[t]+K[0];
E=D;
D=C;
C=SHA1CircularShift(30,B);
B=A;
A=temp;
for(t=20;
40;
temp=SHA1CircularShift(5,A)+(B^C^D)+E+W[t]+K[1];
for(t=40;
60;
C)|(B&
D)|(C&
D))+E+W[t]+K[2];
for(t=60;
temp=SHA1CircularShift(5,A)+(B^C^D)+E+W[t]+K[3];
Intermediate_Hash[0]+=A;
Intermediate_Hash[1]+=B;
Intermediate_Hash[2]+=C;
Intermediate_Hash[3]+=D;
Intermediate_Hash[4]+=E;
Message_Block_Index=0;
voidSHA1PadMessage(SHA1Context*context)
Message_Block_Index>
55)
Message_Block_Index++]=0x80;
while(context->
Message_Block_Index<
64)
Message_Block_Index++]=0;
56)
else
*Storethemessagelengthasthelast8octets
Message_Block[56]=context->
Length_High>
Message_Block[57]=context->
Message_Block[58]=context->
Message_Block[59]=context->
Length_High;
Message_Block[60]=context->
Length_Low>
Message_Block[61]=context->
Message_Block[62]=context->
Message_Block[63]=context->
Length_Low;
//Convert5Bytesto8BytesBase32
void_Sha1toBase32(unsignedchar*out,constunsignedchar*in)
constchar*Table="
ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
;
out[0]=Table[((in[0]>
3)
)&
0x1F];
out[1]=Table[((in[0]<
2)|(in[1]>
6))&
out[2]=Table[((in[1]>
1)
out[3]=Table[((in[1]<
4)|(in[2]>
4))&
out[4]=Table[((in[2]<
1)|(in[3]>
7))&
out[5]=Table[((in[3]>
2)
out[6]=Table[((in[3]<
3)|(in[4]>
5))&
out[7]=Table[((in[4]
)
//Returnabase32representationofasha1hash
CStringSha1toBase32(constunsignedchar*Sha1)
charBase32[32];
CStringret;
_Sha1toBase32((unsignedchar*)Base32,Sha1);
_Sha1toBase32((unsignedchar*)Base32+8,Sha1+5);
_Sha1toBase32((unsignedchar*)Base32+16,Sha1+10);
_Sha1toBase32((unsignedchar*)Base32+24,Sha1+15);
ret=CString(Base32,32);
returnret;
CStringGetSHA1String(CStringsSource)
SHA1Contextcontext;
unsignedchardigest[20];
CStringsTmp,sRet;
SHA1Reset(&
context);
SHA1Input(&
context,(constBYTE*)sSource.GetBuffer(0),sSource.GetLength());
SHA1Result(&
context,digest);
for(inti=0;
i++)...{
sTmp.Format("
%02X"
digest[i]);
sRet+=sTmp;
returnsRet;
六、学习总结
本次实验题目比较困难,在编程过程中实现SHA-1算法,由于算法掌握不够熟练最终才得出结果,我的程序一直出错,经过反复修改最终成功,在此过程中我发现我的知识掌握不够,对于编程知识有所欠缺,在学习过程中也遇到了不少问题,需要和同学共同商讨,查询资料才能把问题解决。
在完成作业的过程中我也培养了解决问题的能力,查阅资料是很好的手段,在今后的学习过程中应该继续发扬这些能力。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 应用 密码学 上机 作业