现代密码学实验报告.docx
- 文档编号:3857961
- 上传时间:2022-11-25
- 格式:DOCX
- 页数:62
- 大小:1.12MB
现代密码学实验报告.docx
《现代密码学实验报告.docx》由会员分享,可在线阅读,更多相关《现代密码学实验报告.docx(62页珍藏版)》请在冰豆网上搜索。
现代密码学实验报告
学生姓名
学号
专业班级
指导教师
学院信息科学与工程学院
完成时间2014年5月
实验一对称密码算法实验
[实验目的]
1.掌握密码学中经典的对称密码算法DES、AES、RC4的算法原理。
2.掌握DES、AES、RC4的算法流程和实现方法。
[实验预备]
1.DES算法有什么特点?
算法中的哪些结构保证了其混淆和扩散的特性?
答:
分组比较短、密钥太短、密码生命周期短、运算速度较慢。
采用替代和置换的方法简单有效地遵循了香农定理,替代操作通过S盒达到了混淆效果,置换操作通过P盒扩散效果。
2.AES算法的基本原理和特点。
答:
AES加密数据块分组长度必须为128比特,密钥长度可以是128比特、192比特、256比特中的任意一个(如果数据块及密钥长度不足时,会补齐)。
AES加密有很多轮的重复和变换。
大致步骤如下:
1、密钥扩展(KeyExpansion),2、初始轮(InitialRound),3、重复轮(Rounds),每一轮又包括:
SubBytes、ShiftRows、MixColumns、AddRoundKey,4、最终轮(FinalRound),最终轮没有MixColumns。
3.流密码RC4的密钥流生成以及S盒初始化过程。
答:
RC4由伪随机数生成器和异或运算组成。
RC4的密钥长度可变,范围是[1,255]。
RC4一个字节一个字节地加解密。
给定一个密钥,伪随机数生成器接受密钥并产生一个S盒。
S盒用来加密数据,而且在加密过程中S盒会变化。
初始化长度为256的S盒。
第一个for循环将0到255的互不重复的元素装入S盒。
第二个for循环根据密钥打乱S盒。
下面i,j是两个指针。
每收到一个字节,就进行while循环。
通过一定的算法((a),(b))定位S盒中的一个元素,并与输入字节异或,得到k。
循环中还改变了S盒((c))。
如果输入的是明文,输出的就是密文;如果输入的是密文,输出的就是明文。
[实验内容]
1.分析DES、AES、RC4、SHA的实现过程。
2.用程序设计语言将算法过程编程实现。
3.完成字符串数据的加密运算和解密运算
输入明文:
Idolikethisbook
输入密钥:
cryption
[实验步骤]
1.预习DES、AES、RC4算法。
2.写出算法流程,用程序设计语言将算法过程编程实现。
DES算法流程:
代码:
#include"memory.h"
#include"stdio.h"
#include
#include
#include
usingnamespacestd;
enum{encrypt,decrypt};
//ENCRYPT:
加密,DECRYPT:
解密
voiddes_run(charout[8],charin[8],booltype=encrypt);
//设置密钥
voiddes_setkey(constcharkey[8]);
staticvoidf_func(boolin[32],constboolki[48]);
//f函数
staticvoids_func(boolout[32],constboolin[48]);
//s盒代替
//变换
staticvoidtransform(bool*out,bool*in,constchar*table,intlen);
staticvoidxor(bool*ina,constbool*inb,intlen);
//异或
staticvoidrotatel(bool*in,intlen,intloop);
//循环左移
//字节组转换成位组
staticvoidbytetobit(bool*out,constchar*in,intbits);
//位组转换成字节组
staticvoidbittobyte(char*out,constbool*in,intbits);
//置换IP表
conststaticcharip_table[64]={58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7};
//逆置换IP-1表
conststaticcharipr_table[64]={40,8,48,16,56,24,64,32,
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25};
//E位选择表
staticconstchare_table[48]={32,1,2,3,4,5,4,5,
6,7,8,9,8,9,10,11,
12,13,12,13,14,15,
16,17,16,17,18,19,
20,21,20,21,22,23,
24,25,24,25,26,27,
28,29,28,29,30,31,32,1};
//P换位表
conststaticcharp_table[32]={16,7,20,21,29,12,28,
17,1,15,23,26,5,18,
31,10,2,8,24,14,32,
27,3,9,19,13,30,6,22,11,4,25};
//pc1选位表
conststaticcharpc1_table[56]={57,49,41,33,25,17,9,
1,58,50,42,34,26,18,
10,2,59,51,43,35,27,
19,11,3,60,52,44,36,
63,55,47,39,31,23,15,
7,62,54,46,38,30,22,
14,6,61,53,45,37,29,
21,13,5,28,20,12,4};
//pc2选位表
conststaticcharpc2_table[48]={14,17,11,24,1,5,3,28,
15,6,21,10,23,19,12,4,
26,8,16,7,27,20,13,2,
41,52,31,37,47,55,30,
40,51,45,33,48,44,49,
39,56,34,53,46,42,50,36,29,32};
//左移位数表
conststaticcharloop_table[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
//S盒
conststaticchars_box[8][4][16]={//s1
14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13,
//s2
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9,
//s3
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12,
//s4
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14,
//s5
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3,
//s6
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13,
//s7
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12,
//s8
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11};
staticboolsubkey[16][48];
//16圈子密钥
voiddes_run(charout[8],charin[8],booltype)
{
staticboolm[64],tmp[32],*li=&m[0],*ri=&m[32];bytetobit(m,in,64);
transform(m,m,ip_table,64);
if(type==encrypt)
{
for(inti=0;i<16;i++)
{
memcpy(tmp,ri,32);
f_func(ri,subkey[i]);
xor(ri,li,32);
memcpy(li,tmp,32);
}
}else
{
for(inti=15;i>=0;i--)
{
memcpy(tmp,li,32);
f_func(li,subkey[i]);
xor(li,ri,32);
memcpy(ri,tmp,32);
}
}
transform(m,m,ipr_table,64);
bittobyte(out,m,64);
}
voiddes_setkey(constcharkey[8])
{
staticboolk[64],*kl=&k[0],*kr=&k[28];
bytetobit(k,key,64);
transform(k,k,pc1_table,56);
for(inti=0;i<16;i++)
{
rotatel(kl,28,loop_table[i]);
rotatel(kr,28,loop_table[i]);
transform(subkey[i],k,pc2_table,48);
}
}
voidf_func(boolin[32],constboolki[48])
{
staticboolmr[48];
transform(mr,in,e_table,48);
xor(mr,ki,48);
s_func(in,mr);
transform(in,in,p_table,32);
}
voids_func(boolout[32],constboolin[48])
{
for(chari=0,j,k;i<8;i++,in+=6,out+=4)
{
j=(in[0]<<1)+in[5];
k=(in[1]<<3)+(in[2]<<2)+(in[3]<<1)+in[4];
bytetobit(out,&s_box[i][j][k],4);
}
}
voidtransform(bool*out,bool*in,constchar*table,intlen)
{
staticbooltmp[256];
for(inti=0;i tmp[i]=in[table[i]-1]; memcpy(out,tmp,len); } voidxor(bool*ina,constbool*inb,intlen) { for(inti=0;i ina[i]^=inb[i]; } voidrotatel(bool*in,intlen,intloop) { staticbooltmp[256]; memcpy(tmp,in,loop); memcpy(in,in+loop,len-loop); memcpy(in+len-loop,tmp,loop); } voidbytetobit(bool*out,constchar*in,intbits) { for(inti=0;i out[i]=(in[i/8]>>(i%8))&1; } voidbittobyte(char*out,constbool*in,intbits) { memset(out,0,(bits+7)/8); for(inti=0;i out[i/8]|=in[i]<<(i%8); } voidmain() { stringstr; puts("*****************DES***********************"); cout< puts("pleaseinputyourwords"); cin>>str; //getline(cin,str); printf("\n"); charkey[8]; cout<<"pleaseinputyourkey(8): "; for(intp=0;p<=7;p++) cin>>key[p]; des_setkey(key); intm=str.size(); intn=m/8+1; str=str.substr(0,m); inti=0; stringaw,mw; for(n;n>0;n--) { char*str1=newchar[8]; stringtemp; temp=str.substr(i,8); i=i+8; strcpy(str1,temp.c_str()); des_run(str1,str1,encrypt); aw=aw+str1; aw=aw.substr(0,m+6);//m+1-->m+6 des_run(str1,str1,decrypt); mw=mw+str1; stringtemp1; strcpy(str1,temp1.c_str()); str1=""; temp=""; } puts("afterencrypting: "); cout< puts("afterdecrypting: "); cout< } AES算法流程图: 代码: #include #include #include #definenull0 constunsignedcharSbox[256]={//forwards-box 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16}; constunsignedcharISbox[256]={//inverses-box 0x52,0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb, 0x7c,0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb, 0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,0xee,0x4c,0x95,0x0b,0x42,0xfa,0xc3,0x4e, 0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25, 0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92, 0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84, 0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,0xf7,0xe4,0x58,0x05,0xb8,0xb3,0x45,0x06, 0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b, 0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 现代 密码学 实验 报告