1、现代密码学实验题目+代码实 验 报 告实验课程名称 现代密码学 学 院 * 年级 * 专业班 * 学 生 姓 名 * 学 号 * 开 课 时 间 200- 至 200- 学年第 二 学期总 成 绩教师签名实验项目名 称实验一、古典密码(认识密码学)成绩一、实验目的 通过实现简单的古典密码算法,理解密码学的相关概念如明文(plaintext)、密文(ciphertext)、加密密钥(encryption key)、解密密钥(decryption key)、加密算法(encryption algorithm)、解密算法(decryption algorithm)等。二、实验内容 1)用CC+语言实
2、现仿射变换(Affine)加/解密算法;2)用CC+语言实现统计26个英文字母出现的频率的程序;3)利用仿射变换加/解密程序对一段较长的英文文章进行加密,再利用统计软件对明文和密文中字母出现的频率进行统计并作对比,观察有什么规律。 放射变换:加密:解密:其中a, b为密钥,且gcd(a, 26)=1实验要求:加/解密程序对任意满足条件的a、b都能够处理。三、实验步骤(1)统计26个英文字母出现的频率的程序#include#include#includeusing namespace std;void main() ifstream in(a.txt); vector s; vector n(2
3、6,0); for(int i=0;ix; ) for(int i=0;i26;+i) if(int(x)=si) ni+; float sum=0.0; for(int j=0;j26;+j) sum+=nj; cout统计结果如下:endl; for(int k=0;k26;+k) / nk=nk/sum; cout char(k+97)出现的概率为:nk/sumendl; /coutnkendl; (2)仿射变换加/解密程序对一段较长的英文文章进行加密#include#include#includeusing namespace std;/判断两个数是不是互素(辗转相除)/bool gc
4、d(int a) int f=26,g,r; g=a; do r=f%g; f=g; g=r; while(r); if(f=1) return 1; else return 0;/求逆/int inv(int a) int x,i; for(i=1;i=30;+i) if(26*i+1)%a=0) x=(26*i+1)/a;break; return x;/void main() cout请你选择操作密码的方式:endl 0表示加密 endl 1表示解密 z; if(z=0|z=1) / cout请输入密钥a和b:ab; if(a25)|(b25) couta,b的输入范围有错!endl;
5、else if(gcd(a)=0) cout密钥a有误,与26不互素endl; else if(z=0)/加密算法 ifstream in(a.txt); ofstream out(b.txt); vector s; for(char x;inx; ) s.push_back(int(x); for(int i=0;is.size();+i) si=(a*(si-97)+b)%26; outchar(si+97) ; outendl;cout加密成功!明文请见“b.txt”endl; else/解密算法 ifstream in(b.txt); ofstream out(a.txt); vect
6、or s; for(char x;inx; ) s.push_back(int(x); for(int i=0;is.size();+i) si=inv(a)*(si-97-b+26)%26; outchar(si+97) ; outendl;cout解密成功!密文请见“a.txt”endl; / else cout所选操作无效!endl;四、实验结果及分析该程序是对文件进行操作,结果如下:(1)统计26个英文字母出现的频率的程序(2)仿射变换加/解密程序对一段较长的英文文章进行加密下面是文本内容:实验项目名 称实验二、流密码(认识LFSR及流密码)成绩一、实验目的通过实现简单的线性反馈移位寄
7、存器(LFSR),理解LFSR的工作原理、本原多项式重要意义。二、实验内容1)利用CC+语言实现LFSR(其中LFSR已给定);2)通过不同初始状态生成相应的序列,并观察他们的周期有什么特点;3)利用生成的序列对文本进行加/解密(按对应位作模2加运算)。其中的LFSR为:三、实验步骤#include#include#include#includeusing namespace std;void main() /下面是密钥的产生/ int a31=1,1,0,0,1; for(int k=5;k31;+k) ak=(ak-2+ak-5)%2; cout密钥如下:endl; for(int jj=
8、0;jj31;+jj) coutajj ; coutendl;/ int i=0,key; cout请选择操作方式: 1-加密 2-解密key; vector s,ss; if(key=1|key=2) if(key=1) cout加密成功,密文见out.txtc) int sum=0; for(int j=0;j32) i=(i+j-1)%31+1; else i=i+8; s.push_back(int(c)sum); for(int kk=0;kks.size();+kk) outchar(skk); if(key=2) cout解密成功,明文见in.txtc) int sum=0; f
9、or(int j=0;j32) i=(i+j-1)%31+1; else i=i+8; s.push_back(int(c)sum); for(int kk=0;kks.size();+kk) outchar(skk); / else cout操作无效!endl;四、实验结果及分析在“in.txt”中输入如下内容:实验结果如下:得到密文“out.txt”如下:实验项目名 称实验三、流密码(生成非线性序列)成绩一、实验目的以LFSR序列为基础,生成非线性序列,并利用该序列对文件进行加密、解密。二、实验内容1)利用CC+实现Geffe序列生成器及J-K触发器;2)利用生成的非线性序列对文件进行加密
10、、解密(按对应位作模2加运算)。三、实验步骤#include using namespace std;/return next stateunsigned char fn_feedback(unsigned char n, unsigned char c, unsigned char curr_state) unsigned char t = c & curr_state; unsigned char s = t & (unsigned char)128;/get first bit for(int i =1; i n; i+) s = s ( (t i) & (unsigned char)1
11、28 ); return (unsigned char)(curr_state (n-1); /return next statevoid lfsr_output_byte(int n, unsigned char c, unsigned char init_state, unsigned char output_bytes, int byte_length) unsigned char next_state = init_state; for(int i = 0; i byte_length; i+) unsigned char temp = (unsigned char)0; for(in
12、t j=0; j j ); next_state = fn_feedback(n, c, next_state); output_bytesi = temp; /output a char type of data in binary wayvoid output_binary(unsigned char c) for(int i=0; i 8; i+) if( (c i) & (unsigned char)128 ) cout 1; else cout 0; void Geffe(unsigned char buf, unsigned char buf1, unsigned char buf
13、2, unsigned char b) for(int i=0; i 10; i+) bi=bufi*buf1i+buf2i*buf1i+buf2i; output_binary( (unsigned char)bi); void JK(unsigned char buf,unsigned char buf1,unsigned char c) for(int i=1; i 11; i+) ci=(bufi+buf1i+1)*ci-1+bufi; output_binary( (unsigned char)ci); void cypt(unsigned char b) unsigned char
14、 cypher10,cyph10; coutcypher; for(int j=0;cypherj!=0;j+) cyphj=cypherjbj; coutcyphj; coutendl; for(int k=0;kj;k+) cypherk=cyphkbk; coutcypherk; coutendl;int main(int argc, char* argv) unsigned char buf10,buf110,buf210,b100,c100; / 函数f 初始状态152 lfsr_output_byte(5,(unsigned char)144,(unsigned char)152,
15、 buf, 10); lfsr_output_byte(5,(unsigned char)44,(unsigned char)152, buf1, 10); lfsr_output_byte(5,(unsigned char)24,(unsigned char)152, buf2, 10); Geffe(buf,buf1,buf2,b); c0=0; JK(buf,buf1,c); coutendl; coutGeffe operate:endl; cypt(b); coutendl; coutJ-K operate:endl; cypt(c); return 0;四、实验结果及分析实验项目名
16、 称实验四、DES算法的实现成绩一、实验目的通过实现DES算法,加深对DES算法的理解,同时学习组合密码常用的代换、移位等运算的实现。二、实验内容1)利用CC+实现DES算法的加、解密运算。三、实验步骤定义头文件:yxyDES.h#ifndef yxyDESH#define yxyDESH#include #include #include #include using namespace std;class yxyDESpublic: yxyDES(); yxyDES(); void InitializeKey(string); void EncryptData(string); void
17、DecryptData(string ); void EncryptAnyLength(string); void DecryptAnyLength(string); void SetCiphertext(char* value); char* GetCiphertext(); void SetPlaintext(char* value); char* GetPlaintext(); char* GetCiphertextAnyLength(); char* GetPlaintextAnyLength(); private: char SubKeys1648; char szCiphertex
18、t16; char szPlaintext8; char szFCiphertextAnyLength8192; char szFPlaintextAnyLength4096; void CreateSubKey(char*); void FunctionF(char*,char*,int); void InitialPermuteData(string,char*,bool); void ExpansionR(char* ,char*); void XOR(char* ,char* ,int ,char*); string CompressFuncS(char* ); void Permut
19、ationP(string ,char*); string FillToEightBits(string); void CleanPlaintextMark(); string HexCharToBinary(char); string HexIntToBinary(int ); string BinaryToString(char*,int,bool); int SingleCharToBinary(char); char SingleBinaryToChar(int); ;#endif构造主文件:#pragma hdrstop#include yxyDES.h/-#pragma packa
20、ge(smart_init)/ permuted choice table (PC1)const static int PC1_Table56 = 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;/ permuted
21、choice key (PC2)const static int PC2_Table48 = 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;/ number left rotations of pc1 const static int Shift_Table16 = 1,1,2,
22、2,2,2,2,2,1,2,2,2,2,2,2,1;/ initial permutation (IP)const static int IP_Table64 = 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
23、, 55, 47, 39, 31, 23, 15, 7;/ expansion operation matrix (E)static const int E_Table48 = 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)famous S-boxes const sta
24、tic int S_Box8416 = / 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