资源描述:
《密码学课程设计仿射密码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#includeusingnamespacestd;charz[28][3]={"计","算","机","学","院","网","络","工","程","信","息","安","全",",","我","们","热","爱","中","华","人","民","共","和","国","。","大","家"};intkey[12]={1,3,5,9,11,13,15,17,19,23,25,27};intgcd(intn,intm){intr,temp;if(n2、m!=0){r=n%m;n=m;m=r;}returnn;}intniyuan(inta){intb;for(b=1;b<28;b++)if((a*b)%28==1){returnb;}return-1;}classfangshe{public:intk[2],kx;charc[200],m[200];voidsetkey(){cout<<"请输入仿射密码密钥的两部分:";loop:cin>>k[0]>>k[1];kx=niyuan(k[0]);if(kx==-1){cout<<"此密钥违法!请重新输入:";gotoloop;}}voidsetkey(inta,i
3、ntb){k[0]=a;k[1]=b;kx=niyuan(k[0]);}int*getkey(){returnk;}voidencryption(charm[],charc[]){inti=strlen(m),kk=i/2,j;intmm[100],cc[100];ctoi(m,mm);for(j=1;j<=kk;j++)cc[j]=(k[0]*mm[j]+k[1])%28;itoc(cc,c,kk);}voiddecryption(charc[],charm[]){inti=strlen(c),kk=i/2,jj;intmm[100],cc[100];ctoi(
4、c,cc);for(jj=1;jj<=kk;jj++){intzan=cc[jj]-k[1];if(zan<0)zan=zan+28;mm[jj]=(kx*zan)%28;}itoc(mm,m,kk);}voidctoi(charm[],intmm[]){intmi=0,ii=1,j;for(mi=0,ii=1;m[mi]!=' ';mi=mi+2,ii++){for(j=0;j<=27;j++)if(m[mi]==z[j][0]&&m[mi+1]==z[j][1]){mm[ii]=j;break;}}}voiditoc(intcc[],charc[],intk
5、k){inti,ci=0;for(ci=0,i=1;i<=kk;i++,ci=ci+2){c[ci]=z[cc[i]][0];c[ci+1]=z[cc[i]][1];c[ci+2]=' ';}c[ci]=' ';}};intmain(){fangsheyunlong;cout<<"******************************************************************"<6、"******************************************************************"<7、息安全,我们热爱中华人民共和国。大家**"<>cmd;if(cmd<1
8、
9、cmd>3){cout<<"输入有误!!!"<>yunlo
10、ng.m;