資源描述:
《仿射密碼和置換密碼》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、福州大學(xué)數(shù)學(xué)與計(jì)算機(jī)科學(xué)學(xué)院《計(jì)算機(jī)密碼學(xué)》上機(jī)實(shí)驗(yàn)報(bào)告專業(yè)和班級(jí)數(shù)學(xué)2班姓名吳少敦成績(jī)學(xué)號(hào)030801212實(shí)驗(yàn)名稱古典密碼學(xué)實(shí)驗(yàn)實(shí)驗(yàn)?zāi)康暮鸵螅?)練習(xí)古典密碼學(xué)的加密和解密;(2)練習(xí)仿射密碼和置換密碼的加密和解密實(shí)驗(yàn)任務(wù)(1)熟悉仿射密碼。(2)熟悉置換密碼。實(shí)驗(yàn)內(nèi)容仿射密碼:importjava.util.Scanner;publicclassFangsheMima{publicFangsheMima(){System.out.println("請(qǐng)輸入兩個(gè)密鑰:");Scannersacn1=newScanner(Syste
2、m.in);intk1=sacn1.nextInt();intk2=sacn1.nextInt();if(gcd(k1,26)!=1){System.out.println("密鑰錯(cuò)誤?。?!");}else{booleanboo=true;do{System.out.print("請(qǐng)選擇功能:");System.out.print("1:加密");System.out.print("2:解密");System.out.println("3:退出");intt=sacn1.nextInt();switch(t){case1:System
3、.out.println("請(qǐng)輸入明文:");Scannersacn2=newScanner(System.in);Stringmingwen=sacn2.nextLine();System.out.println("明文:"+mingwen);System.out.println("密鑰:k1="+k1+",k2="+k2);System.out.println("密文:"+jiaMi(mingwen,k1,k2));break;case2:System.out.println("請(qǐng)輸入密文:");Scannersacn3=newS
4、canner(System.in);Stringmiwen=sacn3.nextLine();System.out.println("密文:"+miwen);System.out.println("密鑰:k1="+k1+",k2="+k2);System.out.println("明文:"+jieMi(miwen,k1,k2));break;case3:boo=false;System.out.println("已經(jīng)退出!?。?);break;}}while(boo);}}publicintgcd(inta,intb){intk=0;
5、do{k=a%b;a=b;b=k;}while(k!=0);returna;}//輾轉(zhuǎn)相除球最大公約數(shù)publicintni(inta,intb){inti=1;while(a*i%b!=1){i++;}returni;}//a相對(duì)b的逆publicStringjiaMi(Stringmingwen,intk1,intk2){Stringmiwen="";char[]c=newchar[mingwen.length()];for(inti=0;i6、>='a'&&c[i]<='z'){c[i]=(char)((k1*(c[i]-'a')+k2)%26+'a');}elseif(c[i]>='A'&&c[i]<='Z'){c[i]=(char)((k1*(c[i]-'A')+k2)%26+'A');}miwen+=c[i];}returnmiwen;}//加密publicStringjieMi(Stringmiwen,intk1,intk2){Stringmingwen="";char[]c=newchar[miwen.length()];inttem=0;for(inti=0;i
7、64&&c[i]<91){tem=(ni(k1,26)*((c[i]-65)-k2))%26;if(tem<0){c[i]=(char)(tem%26+26+65);}else{c[i]=(char)(tem%26+65);}}elseif(c[i]>97&&c[i]<123){tem=(ni(k1,26)*((c[i]-97)-k2))%26;if(tem<0){c[i]=(char)(tem%26+26+97);}else{c[i]=(char
8、)(tem%26+97);}}mingwen+=c[i];}returnmingwen;}//解密publicstaticvoidmain(String[]args){newFangsheMima();}}置換密碼:pa