现代密码学实验题目+代码.docx

上传人:b****5 文档编号:3950813 上传时间:2022-11-26 格式:DOCX 页数:46 大小:81.90KB
下载 相关 举报
现代密码学实验题目+代码.docx_第1页
第1页 / 共46页
现代密码学实验题目+代码.docx_第2页
第2页 / 共46页
现代密码学实验题目+代码.docx_第3页
第3页 / 共46页
现代密码学实验题目+代码.docx_第4页
第4页 / 共46页
现代密码学实验题目+代码.docx_第5页
第5页 / 共46页
点击查看更多>>
下载资源
资源描述

现代密码学实验题目+代码.docx

《现代密码学实验题目+代码.docx》由会员分享,可在线阅读,更多相关《现代密码学实验题目+代码.docx(46页珍藏版)》请在冰豆网上搜索。

现代密码学实验题目+代码.docx

现代密码学实验题目+代码

实验报告

 

实验课程名称现代密码学

学院**年级**专业班*********

学生姓名*******学号********

开课时间200-至200-学年第二学期

 

总成绩

教师签名

实验项目

名称

实验一、古典密码(认识密码学)

成绩

一、实验目的

通过实现简单的古典密码算法,理解密码学的相关概念如明文(plaintext)、密文(ciphertext)、加密密钥(encryptionkey)、解密密钥(decryptionkey)、加密算法(encryptionalgorithm)、解密算法(decryptionalgorithm)等。

二、实验内容

1)用C\C++语言实现仿射变换(Affine)加/解密算法;2)用C\C++语言实现统计26个英文字母出现的频率的程序;3)利用仿射变换加/解密程序对一段较长的英文文章进行加密,再利用统计软件对明文和密文中字母出现的频率进行统计并作对比,观察有什么规律。

放射变换:

加密:

解密:

其中a,b为密钥,

,且gcd(a,26)=1

实验要求:

加/解密程序对任意满足条件的a、b都能够处理。

三、实验步骤

(1)统计26个英文字母出现的频率的程序

#include

#include

#include

usingnamespacestd;

voidmain(){

ifstreamin("a.txt");

vectors;

vectorn(26,0);

for(inti=0;i<26;++i)

s.push_back(97+i);

for(charx;in>>x;)

for(inti=0;i<26;++i)

if(int(x)==s[i]){

n[i]++;}

floatsum=0.0;

for(intj=0;j<26;++j)

sum+=n[j];

cout<<"统计结果如下:

"<

for(intk=0;k<26;++k){

//n[k]=n[k]/sum;

cout<<''<

"<

//cout<

}

(2)仿射变换加/解密程序对一段较长的英文文章进行加密

#include

#include

#include

usingnamespacestd;

//////////判断两个数是不是互素(辗转相除)////////

boolgcd(inta){

intf=26,g,r;

g=a;

do{

r=f%g;

f=g;

g=r;

}while(r);

if(f==1)

return1;

else

return0;

}

//////////////////求逆//////

intinv(inta){

intx,i;

for(i=1;i<=30;++i)

if((26*i+1)%a==0)

{

x=(26*i+1)/a;break;}

returnx;

}//////////////////////////////////////////////////////////

voidmain(){

cout<<"请你选择操作密码的方式:

"<

intz;

cin>>z;

if(z==0||z==1)

{

//////////////////////////////////////////

cout<<"请输入密钥a和b:

"<

inta,b;

cin>>a>>b;

if((a<1||a>25)||(b<0||b>25))

cout<<"a,b的输入范围有错!

"<

else

if(gcd(a)==0)

cout<<"密钥a有误,与26不互素"<

else

{

if(z==0)////加密算法

{

ifstreamin("a.txt");

ofstreamout("b.txt");

vectors;

for(charx;in>>x;)

s.push_back(int(x));

for(inti=0;i

{

s[i]=(a*(s[i]-97)+b)%26;

out<

}out<

明文请见“b.txt”"<

}

else////解密算法

{

ifstreamin("b.txt");

ofstreamout("a.txt");

vectors;

for(charx;in>>x;)

s.push_back(int(x));

for(inti=0;i

{

s[i]=inv(a)*(s[i]-97-b+26)%26;

out<

}out<

密文请见“a.txt”"<

}

}

////////////////////////////////////////

}

else

cout<<"所选操作无效!

"<

}

}四、实验结果及分析

该程序是对文件进行操作,结果如下:

 

(1)统计26个英文字母出现的频率的程序

(2)仿射变换加/解密程序对一段较长的英文文章进行加密

下面是文本内容:

 

实验项目

名称

实验二、流密码(认识LFSR及流密码)

成绩

一、实验目的

通过实现简单的线性反馈移位寄存器(LFSR),理解LFSR的工作原理、本原多项式重要意义。

二、实验内容

1)利用C\C++语言实现LFSR(其中LFSR已给定);2)通过不同初始状态生成相应的序列,并观察他们的周期有什么特点;3)利用生成的序列对文本进行加/解密(按对应位作模2加运算)。

其中的LFSR为:

 

三、实验步骤

#include

#include

#include

#include

usingnamespacestd;

voidmain()

{

///下面是密钥的产生/////////////

inta[31]={1,1,0,0,1};

for(intk=5;k<31;++k)

a[k]=(a[k-2]+a[k-5])%2;

cout<<"密钥如下:

"<

for(intjj=0;jj<31;++jj)

cout<

cout<

//////////////////////

inti=0,key;

cout<<"请选择操作方式:

1-加密2-解密"<

cin>>key;

vectors,ss;

if(key==1||key==2)

{

if(key==1)

{cout<<"加密成功,密文见out.txt"<

ifstreamin("in.txt");

ofstreamout("out.txt");

charc;

while(in>>c)

{

intsum=0;

for(intj=0;j<8;++j)

sum+=pow(2,7-j)*a[(i+j)%31];

if(i+j>32)

i=(i+j-1)%31+1;

else

i=i+8;

s.push_back((int(c))^sum);

}

for(intkk=0;kk

{

out<

}

}

if(key==2)

{

cout<<"解密成功,明文见in.txt"<

ifstreamin("out.txt");

ofstreamout("in.txt");

charc;

while(in>>c)

{

intsum=0;

for(intj=0;j<8;++j)

sum+=pow(2,7-j)*a[(i+j)%31];

if(i+j>32)

i=(i+j-1)%31+1;

else

i=i+8;

s.push_back((int(c))^sum);

}

for(intkk=0;kk

{

out<

}

}

////////////

}

else

cout<<"操作无效!

"<

}

四、实验结果及分析

在“in.txt”中输入如下内容:

实验结果如下:

得到密文“out.txt”如下:

实验项目

名称

实验三、流密码(生成非线性序列)

成绩

一、实验目的

以LFSR序列为基础,生成非线性序列,并利用该序列对文件进行加密、解密。

二、实验内容

1)利用C\C++实现Geffe序列生成器及J-K触发器;2)利用生成的非线性序列对文件进行加密、解密(按对应位作模2加运算)。

三、实验步骤

#include

usingnamespacestd;

//returnnextstate

unsignedcharfn_feedback(unsignedcharn,

unsignedcharc,

unsignedcharcurr_state)

{

unsignedchart=c&curr_state;

unsignedchars=t&(unsignedchar)128;//getfirstbit

for(inti=1;i

{

s=s^((t<

}

return(unsignedchar)((curr_state<<1)|(s>>(n-1)));//returnnextstate

}

voidlfsr_output_byte(intn,

unsignedcharc,

unsignedcharinit_state,

unsignedcharoutput_bytes[],

intbyte_length)

{

unsignedcharnext_state=init_state;

for(inti=0;i

{

unsignedchartemp=(unsignedchar)0;

for(intj=0;j<8;j++)

{

temp=temp|((next_state&(unsignedchar)128)>>j);

next_state=fn_feedback(n,c,next_state);

}

output_bytes[i]=temp;

}

}

//outputachartypeofdatainbinaryway

voidoutput_binary(unsignedcharc)

{

for(inti=0;i<8;i++)

{

if((c<

cout<<"1";

else

cout<<"0";

}

}

voidGeffe(unsignedcharbuf[],

unsignedcharbuf1[],

unsignedcharbuf2[],

unsignedcharb[])

{

for(inti=0;i<10;i++)

{

b[i]=buf[i]*buf1[i]+buf2[i]*buf1[i]+buf2[i];

output_binary((unsignedchar)b[i]);

}

}

voidJK(unsignedcharbuf[],unsignedcharbuf1[],unsignedcharc[])

{

for(inti=1;i<11;i++)

{

c[i]=(buf[i]+buf1[i]+1)*c[i-1]+buf[i];

output_binary((unsignedchar)c[i]);

}

}

voidcypt(unsignedcharb[])

{

unsignedcharcypher[10],cyph[10];

cout<<"inputthecyphertext:

";

cin>>cypher;

for(intj=0;cypher[j]!

='\0';j++)

{

cyph[j]=cypher[j]^b[j];

cout<

}

cout<

for(intk=0;k

{

cypher[k]=cyph[k]^b[k];

cout<

}

cout<

}

intmain(intargc,char*argv[])

{

unsignedcharbuf[10],buf1[10],buf2[10],b[100],c[100];

//函数f初始状态152

lfsr_output_byte(5,(unsignedchar)144,(unsignedchar)152,buf,10);

lfsr_output_byte(5,(unsignedchar)44,(unsignedchar)152,buf1,10);

lfsr_output_byte(5,(unsignedchar)24,(unsignedchar)152,buf2,10);

Geffe(buf,buf1,buf2,b);

c[0]=0;

JK(buf,buf1,c);

cout<

cout<<"Geffeoperate:

"<

cypt(b);

cout<

cout<<"J-Koperate:

"<

cypt(c);

return0;

}

四、实验结果及分析

实验项目

名称

实验四、DES算法的实现

成绩

一、实验目的

通过实现DES算法,加深对DES算法的理解,同时学习组合密码常用的代换、移位等运算的实现。

 

二、实验内容

1)利用C\C++实现DES算法的加、解密运算。

 

三、实验步骤

定义头文件:

yxyDES.h

#ifndefyxyDESH

#defineyxyDESH

#include

#include

#include

#include

usingnamespacestd;

classyxyDES

{

public:

yxyDES();

~yxyDES();

voidInitializeKey(string);

voidEncryptData(string);

voidDecryptData(string);

voidEncryptAnyLength(string);

voidDecryptAnyLength(string);

voidSetCiphertext(char*value);

char*GetCiphertext();

voidSetPlaintext(char*value);

char*GetPlaintext();

char*GetCiphertextAnyLength();

char*GetPlaintextAnyLength();

private:

charSubKeys[16][48];

charszCiphertext[16];

charszPlaintext[8];

charszFCiphertextAnyLength[8192];

charszFPlaintextAnyLength[4096];

voidCreateSubKey(char*);

voidFunctionF(char*,char*,int);

voidInitialPermuteData(string,char*,bool);

voidExpansionR(char*,char*);

voidXOR(char*,char*,int,char*);

stringCompressFuncS(char*);

voidPermutationP(string,char*);

stringFillToEightBits(string);

voidCleanPlaintextMark();

stringHexCharToBinary(char);

stringHexIntToBinary(int);

stringBinaryToString(char*,int,bool);

intSingleCharToBinary(char);

charSingleBinaryToChar(int);

};

#endif

构造主文件:

#pragmahdrstop

#include"yxyDES.h"

//---------------------------------------------------------------------------

#pragmapackage(smart_init)

//permutedchoicetable(PC1)

conststaticintPC1_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

};

//permutedchoicekey(PC2)

conststaticintPC2_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

};

//numberleftrotationsofpc1

conststaticintShift_Table[16]={

1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1

};

//initialpermutation(IP)

conststaticintIP_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

};

//expansionoperationmatrix(E)

staticconstintE_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

};

//The(in)famousS-boxes

conststaticintS_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,

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

当前位置:首页 > 小学教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1