现代密码学实验题目+代码Word文档格式.docx
《现代密码学实验题目+代码Word文档格式.docx》由会员分享,可在线阅读,更多相关《现代密码学实验题目+代码Word文档格式.docx(46页珍藏版)》请在冰豆网上搜索。
n(26,0);
for(inti=0;
i<
26;
++i)
s.push_back(97+i);
for(charx;
in>
>
x;
)
for(inti=0;
if(int(x)==s[i]){
n[i]++;
}
floatsum=0.0;
for(intj=0;
j<
++j)
sum+=n[j];
cout<
<
"
统计结果如下:
endl;
for(intk=0;
k<
++k){
//n[k]=n[k]/sum;
cout<
'
'
char(k+97)<
出现的概率为:
n[k]/sum<
//cout<
n[k]<
}
(2)仿射变换加/解密程序对一段较长的英文文章进行加密
//////////判断两个数是不是互素(辗转相除)////////
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;
=30;
if((26*i+1)%a==0)
{
x=(26*i+1)/a;
break;
returnx;
}//////////////////////////////////////////////////////////
请你选择操作密码的方式:
endl<
0—表示加密"
1—表示解密"
intz;
cin>
z;
if(z==0||z==1)
{
//////////////////////////////////////////
请输入密钥a和b:
inta,b;
a>
b;
if((a<
1||a>
25)||(b<
0||b>
25))
a,b的输入范围有错!
if(gcd(a)==0)
cout<
密钥a有误,与26不互素"
else
if(z==0)////加密算法
{
ofstreamout("
b.txt"
s.push_back(int(x));
s.size();
{
s[i]=(a*(s[i]-97)+b)%26;
out<
char(s[i]+97)<
;
}out<
cout<
加密成功!
明文请见“b.txt”"
}
else////解密算法
ifstreamin("
vector<
{
s[i]=inv(a)*(s[i]-97-b+26)%26;
}out<
解密成功!
密文请见“a.txt”"
}
}
////////////////////////////////////////
所选操作无效!
}四、实验结果及分析
该程序是对文件进行操作,结果如下:
下面是文本内容:
实验二、流密码(认识LFSR及流密码)
通过实现简单的线性反馈移位寄存器(LFSR),理解LFSR的工作原理、本原多项式重要意义。
1)利用C\C++语言实现LFSR(其中LFSR已给定);
2)通过不同初始状态生成相应的序列,并观察他们的周期有什么特点;
3)利用生成的序列对文本进行加/解密(按对应位作模2加运算)。
其中的LFSR为:
cmath>
voidmain()
{
///下面是密钥的产生/////////////
inta[31]={1,1,0,0,1};
for(intk=5;
31;
++k)
a[k]=(a[k-2]+a[k-5])%2;
密钥如下:
for(intjj=0;
jj<
++jj)
a[jj]<
//////////////////////
inti=0,key;
请选择操作方式:
1-加密2-解密"
key;
s,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;
8;
sum+=pow(2,7-j)*a[(i+j)%31];
if(i+j>
32)
i=(i+j-1)%31+1;
i=i+8;
s.push_back((int(c))^sum);
for(intkk=0;
kk<
++kk)
out<
char(s[kk]);
if(key==2)
{
解密成功,明文见in.txt"
////////////
操作无效!
四、实验结果及分析
在“in.txt”中输入如下内容:
实验结果如下:
得到密文“out.txt”如下:
实验三、流密码(生成非线性序列)
以LFSR序列为基础,生成非线性序列,并利用该序列对文件进行加密、解密。
1)利用C\C++实现Geffe序列生成器及J-K触发器;
2)利用生成的非线性序列对文件进行加密、解密(按对应位作模2加运算)。
#include<
//returnnextstate
unsignedcharfn_feedback(unsignedcharn,
unsignedcharc,
unsignedcharcurr_state)
unsignedchart=c&
curr_state;
unsignedchars=t&
(unsignedchar)128;
//getfirstbit
for(inti=1;
i<
n;
i++)
s=s^((t<
i)&
(unsignedchar)128);
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<
byte_length;
unsignedchartemp=(unsignedchar)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)
if((c<
(unsignedchar)128)
cout<
"
1"
cout<
0"
voidGeffe(unsignedcharbuf[],
unsignedcharbuf1[],
unsignedcharbuf2[],
unsignedcharb[])
10;
b[i]=buf[i]*buf1[i]+buf2[i]*buf1[i]+buf2[i];
output_binary((unsignedchar)b[i]);
voidJK(unsignedcharbuf[],unsignedcharbuf1[],unsignedcharc[])
for(inti=1;
11;
c[i]=(buf[i]+buf1[i]+1)*c[i-1]+buf[i];
output_binary((unsignedchar)c[i]);
voidcypt(unsignedcharb[])
unsignedcharcypher[10],cyph[10];
inputthecyphertext:
cypher;
cypher[j]!
='
\0'
j++)
cyph[j]=cypher[j]^b[j];
cyph[j];
j;
k++)
cypher[k]=cyph[k]^b[k];
cypher[k];
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);
Geffeoperate:
cypt(b);
J-Koperate:
cypt(c);
return0;
实验四、DES算法的实现
通过实现DES算法,加深对DES算法的理解,同时学习组合密码常用的代换、移位等运算的实现。
1)利用C\C++实现DES算法的加、解密运算。
定义头文件:
yxyDES.h
#ifndefyxyDESH
#defineyxyDESH
string>
stdio.h>
windows.h>
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,