博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一些基础密码算法的实现
阅读量:6375 次
发布时间:2019-06-23

本文共 12418 字,大约阅读时间需要 41 分钟。

把以前写过的几个小算法稍整理下子。

1.多表替代密码:

1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 7 int gcd(int a,int b); //求最大公约数; 8 void jiam(string words,int* A);//加密; 9 void jiem(string words,int* A);//解密。10 int main()11 {12 int A[9],i,tmp;13 string words;14 bool r;15 cout<<"请随意输入一个整数,来改变随机种子:";16 cin>>i;17 srand(i);18 getchar();19 cout<<"系统随机生成一个三阶矩阵:"<
>r;40 if(r==0)41 jiam(words,A);42 else43 jiem(words,A);44 return 0;45 }46 void jiam(string words,int* A)47 {48 int i;49 for(i=0;i
View Code

2.多表替代密码(改进):

1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 7 int gcd(int a,int b);// 求最大公约数; 8 void jiami(string words,int* A,int& b0,int& b1,int& b2);//加密; 9 void jiemi(string words,int* A,int& b0,int& b1,int& b2);//解密; 10 void nijz(int* A);//求逆元矩阵; 11 int niyuan(long m,long n);//求逆元; 12 int main() 13 { 14 int A[9],i,tmp,b0,b1,b2; 15 string words; 16 bool r; 17 cout<<"请随意输入一个整数,来改变随机种子:"; 18 cin>>i; 19 srand(i); 20 getchar(); 21 cout<<"系统随机生成一个三阶矩阵:"<
>r; 41 cout<<"请输入密钥B矩阵(三阶):"; 42 cin>>b0>>b1>>b2; 43 if(r==1) 44 { 45 nijz(A); 46 jiemi(words,A,b0,b1,b2); 47 } 48 else 49 { 50 jiami(words,A,b0,b1,b2); 51 } 52 cout<
View Code

3.放射密码以及频率分析:

1 #include
//j=k0+ik1(mod n) 2 #include
3 using namespace std; 4 5 const int n=26; 6 struct f 7 { 8 char x; 9 float y; 10 float z; 11 }; 12 void jiami(string words,const int& k0,const int& k1);//加密; 13 void jiemi(string words,const int& k0,const int& k1);//解密; 14 int niyuan(long m,long n);//求逆元; 15 int gcd(int a,int b);//求最大公约数; 16 int main() 17 { 18 string words; 19 char r; 20 int i,j; 21 struct f pinlv[26]; 22 for(i=0;i<26;i++) 23 { 24 pinlv[i].x='A'+i; 25 } 26 pinlv[0].y=0.0856; 27 pinlv[1].y=0.0139; 28 pinlv[2].y=0.0297; 29 pinlv[3].y=0.0678; 30 pinlv[4].y=0.1304; 31 pinlv[5].y=0.0289; 32 pinlv[6].y=0.0199; 33 pinlv[7].y=0.0528; 34 pinlv[8].y=0.0627; 35 pinlv[9].y=0.0013; 36 pinlv[10].y=0.0042; 37 pinlv[11].y=0.0339; 38 pinlv[12].y=0.0249; 39 pinlv[13].y=0.0707; 40 pinlv[14].y=0.0797; 41 pinlv[15].y=0.0199; 42 pinlv[16].y=0.0012; 43 pinlv[17].y=0.0677; 44 pinlv[18].y=0.0607; 45 pinlv[19].y=0.1045; 46 pinlv[20].y=0.0249; 47 pinlv[21].y=0.0092; 48 pinlv[22].y=0.0149; 49 pinlv[23].y=0.0017; 50 pinlv[24].y=0.0199; 51 pinlv[25].y=0.0008; 52 int k0,k1; 53 cout<<"请输入明文或密文(大写):"; 54 getline(cin,words); 55 cout<<"加密(0)?解密(1)?请输入 0 or 1:"; 56 cin>>r; 57 t: 58 cout<<"请输入两个密钥:k0 k1(k1与26互素):"; 59 cin>>k0>>k1; 60 if(gcd(k1,26)!=1) 61 { 62 cout<<"请注意要求!"<
View Code

4.enigma密码机:

1 #include 
2 #include
3 #include
4 using namespace std; 5 6 void hl(int* x);//混合字母顺序-以数字代替字母 7 void result(string s,int* x1,int* x2,int* x3);//加解密 8 int main() 9 {10 int x1[26],x2[26],x3[26];11 hl(x1);12 hl(x2);13 hl(x3);14 string s;15 cin>>s;16 result(s,x1,x2,x3);17 return 0;18 }19 void hl(int* x)20 {21 int i,m,y[26];22 for(i=0;i<26;i++)23 {24 y[i]=i;25 }26 for(i=0;i<26;)27 {28 m=rand()%26;29 if(y[m]==-1)30 {31 continue;32 }33 else34 {35 x[i]=m;36 y[m]=-1;37 i++;38 }39 }40 }41 void result(string s,int* x1,int* x2,int* x3)42 {43 int m0=rand()%26,m1=rand()%26,m2=rand()%26,m3=rand()%26,i,j,tmp;44 for(i=0;i
View Code

5.DES加解密以及雪崩效应验证:

1 #include 
2 #include
3 4 using namespace std; 5 6 const int IP[]={ 7 58, 50, 42, 34, 26, 18, 10, 2, 8 60, 52, 44, 36, 28, 20, 12, 4, 9 62, 54, 46, 38, 30, 22, 14, 6, 10 64, 56, 48, 40, 32, 24, 16, 8, 11 57, 49, 41, 33, 25, 17, 9, 1, 12 59, 51, 43, 35, 27, 19, 11, 3, 13 61, 53, 45, 37, 29, 21, 13, 5, 14 63, 55, 47, 39, 31, 23, 15, 7}; 15 const int FP[]={ 16 40, 8, 48, 16, 56, 24, 64, 32, 17 39, 7, 47, 15, 55, 23, 63, 31, 18 38, 6, 46, 14, 54, 22, 62, 30, 19 37, 5, 45, 13, 53, 21, 61, 29, 20 36, 4, 44, 12, 52, 20, 60, 28, 21 35, 3, 43, 11, 51, 19, 59, 27, 22 34, 2, 42, 10, 50, 18, 58, 26, 23 33, 1, 41, 9, 49, 17, 57, 25}; 24 const int E[]={ 25 32, 1, 2, 3, 4, 5, 26 4, 5, 6, 7, 8, 9, 27 8, 9, 10, 11, 12, 13, 28 12, 13, 14, 15, 16, 17, 29 16, 17, 18, 19, 20, 21, 30 20, 21, 22, 23, 24, 25, 31 24, 25, 26, 27, 28, 29, 32 28, 29, 30, 31, 32, 1}; 33 const int P[]={ 34 16, 7, 20, 21, 35 29, 12, 28, 17, 36 1, 15, 23, 26, 37 5, 18, 31, 10, 38 2, 8, 24, 14, 39 32, 27, 3, 9, 40 19, 13, 30, 6, 41 22, 11, 4, 25};//0001 0101 1001 0101 0011 1101 0111 1000 1101 0000 1000 1011 1001 0001 1000 1110 42 const int S[8][64]={ 43 /* S1 */ 44 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 45 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 46 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 47 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13, 48 49 /* S2 */ 50 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 51 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 52 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 53 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9, 54 55 /* S3 */ 56 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 57 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 58 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 59 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12, 60 61 /* S4 */ 62 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 63 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 64 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 65 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14, 66 67 /* S5 */ 68 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 69 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 70 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 71 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3, 72 73 /* S6 */ 74 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 75 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 76 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 77 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13, 78 79 /* S7 */ 80 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 81 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 82 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 83 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12, 84 85 /* S8 */ 86 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 87 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 88 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 89 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}; 90 const int PC_1[]={ 91 57, 49, 41, 33, 25, 17, 9, 92 1, 58, 50, 42, 34, 26, 18, 93 10, 2, 59, 51, 43, 35, 27, 94 19, 11, 3, 60, 52, 44, 36, 95 96 63, 55, 47, 39, 31, 23, 15, 97 7, 62, 54, 46, 38, 30, 22, 98 14, 6, 61, 53, 45, 37, 29, 99 21, 13, 5, 28, 20, 12, 4};100 const int PC_2[]={101 14, 17, 11, 24, 1, 5,102 3, 28, 15, 6, 21, 10,103 23, 19, 12, 4, 26, 8,104 16, 7, 27, 20, 13, 2,105 41, 52, 31, 37, 47, 55,106 30, 40, 51, 45, 33, 48,107 44, 49, 39, 56, 34, 53,108 46, 42, 50, 36, 29, 32};109 struct w110 {111 unsigned int a:1;112 unsigned int b:1;113 unsigned int c:1;114 unsigned int d:1;115 unsigned int e:1;116 unsigned int f:1;117 unsigned int g:1;118 unsigned int h:1;119 };120 union data121 {122 char rr;123 w rw;124 };125 string DES(string s,string key);126 string Key1(string s);127 string Key2(string s);128 int main()129 {130 string words0,words,key0,key,tmp;131 bool mi;132 int i,bit,counts;133 cout<<"加密(0) or 解密(1):";134 cin>>mi;135 cout<<"请输入密钥(8个字符):";136 cin>>key0;137 if(mi==false)138 {139 cout<<"请输入明文(二进制数串:个数为64的倍数):";140 cin>>words0;141 key=Key1(key0);142 words=DES(words0,key);143 }144 else145 {146 cout<<"请输入密文(二进制数串:个数为64的倍数):";147 cin>>words0;148 key=Key2(key0);149 words=DES(words0,key);150 }151 cout<<"你想要的结果是:"<
<
>mi;154 if(!mi)155 return 0;156 cout<<"雪崩效应测试:"<
>bit;159 bit--;160 counts=0;161 for(i=0;i
>=1;228 *(*t+j*4+2)=tmp1&1;229 tmp1>>=1;230 *(*t+j*4+1)=tmp1&1;231 tmp1>>=1;232 *(*t+j*4)=tmp1;233 }234 for(j=32;j<64;j++)235 {236 *(*tt+j)=(*(*t+P[j-32]-1))^(*(*tt+j-32));237 }238 for(j=0;j<32;j++)239 {240 if(ts1[j]=='0')241 *(*tt+j)=false;242 else243 *(*tt+j)=true;244 }245 }246 for(j=0;j<32;j++)247 {248 tmp=*(*tt+j);249 *(*tt+j)=*(*tt+j+32);250 *(*tt+j+32)=tmp;251 }252 for(j=0;j<64;j++)253 {254 *(*t+j)=*(*tt+FP[j]-1);255 }256 for(j=0;j<8;j++)257 {258 for(l=0;l<8;l++)259 {260 if(t[j][l]==true)261 s+="1";262 else263 s+="0";264 }265 }266 }267 return s;268 }269 string Key1(string s)270 {271 string ss=s;272 int i,j,k;273 data d[8];274 bool tmp0,tmp1,tmp2,tmp3;275 bool t[8][8],tt[8][8];276 for(j=0;j<8;j++)277 {278 d[j].rr=ss[j];279 t[j][6]=d[j].rw.b;280 t[j][5]=d[j].rw.c;281 t[j][4]=d[j].rw.d;282 t[j][3]=d[j].rw.e;283 t[j][2]=d[j].rw.f;284 t[j][1]=d[j].rw.g;285 t[j][0]=d[j].rw.h;286 }287 for(j=0;j<56;j++)288 {289 *(*tt+j)=*(*t+PC_1[j]-1);290 }291 tmp0=*(*tt);292 tmp1=*(*tt+28);293 tmp2=*(*tt+1);294 tmp3=*(*tt+29);295 ss="";296 for(i=0;i<16;i++)297 {298 if(i==1||i==2||i==9||i==16)299 {300 for(j=0;j<55;j++)301 {302 if(j!=27)303 {304 *(*tt+j)=*(*tt+j+1);305 }306 else307 {308 *(*tt+j)=tmp0;309 }310 }311 *(*tt+55)=tmp1;312 }313 else314 {315 for(j=0;j<54;j++)316 {317 if(j!=27)318 {319 *(*tt+j)=*(*tt+j+2);320 321 }322 else323 {324 *(*tt+j-1)=tmp0;325 *(*tt+j)=tmp2;326 }327 }328 *(*tt+55)=tmp3;329 *(*tt+54)=tmp1;330 }331 for(j=0;j<48;j++)332 {333 if(*(*tt+PC_2[j]-1)==true)334 ss+="1";335 else336 ss+="0";337 }338 }339 return ss;340 }341 string Key2(string s)342 {343 string ss=s;344 int i,j,k;345 data d[8];346 bool tmp0,tmp1,tmp2,tmp3;347 bool t[8][8],tt[8][8];348 for(j=0;j<8;j++)349 {350 d[j].rr=ss[j];351 t[j][6]=d[j].rw.b;352 t[j][5]=d[j].rw.c;353 t[j][4]=d[j].rw.d;354 t[j][3]=d[j].rw.e;355 t[j][2]=d[j].rw.f;356 t[j][1]=d[j].rw.g;357 t[j][0]=d[j].rw.h;358 }359 for(j=0;j<56;j++)360 {361 *(*tt+j)=*(*t+PC_1[j]-1);362 }363 tmp0=*(*tt+27);364 tmp1=*(*tt+55);365 tmp2=*(*tt+26);366 tmp3=*(*tt+54);367 ss="";368 for(i=0;i<16;i++)369 {370 if(i==1||i==2||i==9||i==16)371 {372 for(j=55;j>0;j--)373 {374 if(j!=28)375 {376 *(*tt+j)=*(*tt+j-1);377 }378 else379 {380 *(*tt+j)=tmp1;381 }382 }383 *(*tt)=tmp0;384 }385 else386 {387 for(j=55;j>1;j--)388 {389 if(j!=28)390 {391 *(*tt+j)=*(*tt+j-2);392 393 }394 else395 {396 *(*tt+j+1)=tmp1;397 *(*tt+j)=tmp3;398 }399 }400 *(*tt)=tmp2;401 *(*tt+1)=tmp0;402 }403 for(j=0;j<48;j++)404 {405 if(*(*tt+PC_2[j]-1)==true)406 ss+="1";407 else408 ss+="0";409 }410 }411 s=ss;412 for(j=0;j<16;j++)413 {414 for(k=0;k<48;k++)415 {416 ss[j*48+k]=s[(15-j)*48+k];417 }418 }419 return ss;420 }
View Code

 

转载地址:http://iftqa.baihongyu.com/

你可能感兴趣的文章
iOS App无需跳转系统设置自动连接Wi-Fi
查看>>
一道柯里化面试题
查看>>
本科studying abroad 无法毕业申请硕士转学转校处理一切studying abroad 问题
查看>>
RxJava(RxAndroid)的简单学习
查看>>
Java8 函数式编程之函数接口(下)
查看>>
【本人秃顶程序员】MySQL 全表 COUNT(*) 简述
查看>>
centos7中使用febootstrap自制一个基础的centos 7.2的docker镜像
查看>>
系统优化和克隆过程
查看>>
C#开发Unity游戏教程之判断语句
查看>>
Windows自带Android模拟器启动失败
查看>>
安装 SharePoint Server 2007
查看>>
angularjs表单验证-学习
查看>>
springmvc mybatis 调用sql , 转成json
查看>>
linux centos 7 网卡突然不能上网异常解决
查看>>
shell+Python实现简单的链路监控
查看>>
授之以渔-运维平台发布模块一(Jenkins篇)
查看>>
DedeCMS操作基础(一)
查看>>
FreeBSD部署dns缓存服务器
查看>>
实现MySQL允许远程连接
查看>>
Java Outputstream to String
查看>>