古典密码习题解析
2.20 代碼解題:
package mima_210;public class Main {public static void main(String[] args) {// TODO Auto-generated method stubString str="I thought to see the fairies in the"+ "fields,but I saw only th evil elephants"+ "with their black backs!Woe!how that"+ "sight awed me! The elves danced all"+"around and about while I heard voices"+"calling clearly.Ah! how I tried to"+"see-throw off the ugly clout-but no blind"+"eye of a mortal was permitted to spy"+"them.So then came minstrels,having gold"+"trumpets,harps and drums.These palyed"+"very loudly beside me,breaking that spell"+"So the dream vanished, whereat I thanked"+"Heaven.I shed many tears before the thin"+"straw.Now though the Enchanter gnash"+"Spring returns. Oh,wretched man!Hell"+"gapes, Erebus now lies open.The mouths"+"of Death wait on thy end.";String indexs="787656543432112343456567878878765654"+ "3432112343456567878878765654433211234";//首先對該字符串進行篩選,將空格標點去掉String newStr="";for(int t=0;t<str.length();t++){char c=str.charAt(t);if((c<='z'&&c>='a')||(c<='Z'&&c>='A')){newStr+=c;}}System.out.println("密鑰長度為:"+indexs.length()+"字符串長度為:"+newStr.length());//每五組一行的格式輸出整理好的字符串for(int i=0;i<indexs.length()-9;i++){if(i%6==0&&i!=0)System.out.println();for(int j=0;j<8;j++){System.out.print(newStr.charAt(j+i*8));}System.out.print(" ");}//輸出密文System.out.println();System.out.print("密文為:");for(int i=0;i<indexs.length()-9;i++){int j=indexs.charAt(i)-48;System.out.print(newStr.charAt(j+i*8-1));}} }運行結果如下:
?
密鑰長度為:73字符串長度為:514
Ithought?? toseethe??fairiesi?? nthefiel?? dsbutIsa??wonlythe??
vileleph?? antswith??theirbla?? ckbacksW?? oehowtha??tsightaw??
edmeThee?? lvesdanc??edallaro?? undandab?? outwhile??Iheardvo??
icescall?? ingclear??lyAhhowI?? triedtos?? eethrowo??fftheugl??
ycloutbu?? tnoblind??eyeofamo?? rtalwasp?? ermitted??tospythe??
mSothenc?? ameminst??relshavi?? nggoldtr?? umpetsha??rpsanddr??
umsThese?? palyedve??ryloudly?? besideme?? breaking??thatspel??
lSothedr?? eamvanis??hedwhere?? atIthank? ?edHeaven??Ishedman??
ytearsbe?? forethet??hinstraw?? Nowthoug?? htheEnch??antergna??
shSpring?? returnsO??hwretche?? dmanHell?? gapesEre??busnowli??
esopenTh?? emouthso??fDeathwa?? itonthye??
密文為:hesittlseahselddweslodogunopeennhdtasylebtSmwIedstruhngOhlrwehtn
?
由于覺得個人手工來統計密文,會很容易出現失誤和偏差,所以將該過程編寫程序處理,編程過程中發現密鑰的長度較長,沒有足夠的字符串供密鑰使用,故認為該題目所給信息存在問題。但是,對于該加密解密方法可以認同。此外,為了編程處理簡便,將最后的兩個字母省略掉了。
?
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
- 上一篇: 密码学之hill密码
- 下一篇: 宏内核与微内核