1、 n(26,0); for(int i=0;ix; ) for(int i=0; if(int(x)=si) ni+; float sum=0.0; for(int j=0;j+j) sum+=nj; cout统计结果如下:endl; for(int k=0;k+k) / nk=nk/sum; cout char(k+97)出现的概率为:nk/sum /coutnk (2)仿射变换加/解密程序对一段较长的英文文章进行加密/判断两个数是不是互素(辗转相除)/bool gcd(int a) int f=26,g,r; g=a; do r=f%g; f=g; g=r; while(r); if(f=
2、1) return 1; else return 0;/求逆/int inv(int a) int x,i; for(i=1;=30; if(26*i+1)%a=0) x=(26*i+1)/a;break; return x;/请你选择操作密码的方式:endlz; if(z=0|z=1) /请输入密钥a和b: int a,b;ab; if(a25)|(b25)a,b的输入范围有错! if(gcd(a)=0) cout密钥a有误,与26不互素 else if(z=0)/加密算法 ofstream out(b.txt s.push_back(int(x);s.size(); si=(a*(si-9
3、7)+b)%26; outchar(si+97); outcout加密成功!明文请见“b.txt” else/解密算法 ifstream in( vector si=inv(a)*(si-97-b+26)%26; outvoid main() /下面是密钥的产生/ int a31=1,1,0,0,1; for(int k=5;31;+k) ak=(ak-2+ak-5)%2;密钥如下: for(int jj=0;jj+jj)ajj/ int i=0,key;请选择操作方式: 1-加密 2-解密key; s,ss; if(key=1|key=2) if(key=1) coutc) int sum=
4、0; for(int j=0;8; sum+=pow(2,7-j)*a(i+j)%31; if(i+j32) i=(i+j-1)%31+1; i=i+8; s.push_back(int(c)sum); for(int kk=0;kk+kk) outchar(skk); if(key=2) 解密成功,明文见in.txt /操作无效!四、实验结果及分析在“in.txt”中输入如下内容:实验结果如下:得到密文“out.txt”如下:实验三、流密码(生成非线性序列)以LFSR序列为基础,生成非线性序列,并利用该序列对文件进行加密、解密。1)利用CC+实现Geffe序列生成器及J-K触发器;2)利用生
5、成的非线性序列对文件进行加密、解密(按对应位作模2加运算)。#include /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)128 ); return
6、 (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; unsigned char temp = (unsigned char)0; j j ); next_state = fn
7、_feedback(n, c, next_state); output_bytesi = temp;/output a char type of data in binary wayvoid output_binary(unsigned char c) if( (c (unsigned char)128 ) cout 1 cout stdio.hwindows.hclass yxyDESpublic: yxyDES(); yxyDES(); void InitializeKey(string); void EncryptData(string); void DecryptData(string
8、 ); 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 szCiphertext16; char szPlainte
9、xt8; 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 PermutationP(string ,char
10、*); 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 package(smart_init)/ permu
11、ted 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 choice key (PC2)const
12、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,2,2,2,2,2,1,2,2,2,2,2,2
13、,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, 55, 47, 39, 31, 23, 15
14、, 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 static int S_Box8416 = / S1 1
15、4, 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