java凯撒密码实现完美版.wps
《java凯撒密码实现完美版.wps》由会员分享,可在线阅读,更多相关《java凯撒密码实现完美版.wps(5页珍藏版)》请在冰豆网上搜索。
代码:
packagening.hao;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.util.Scanner;publicclassCryptologycharciphertext;/密文intkey;charplaintext;/明文StringBufferplaintextStr;StringBufferciphertextStr;finalintmax=500;Cryptology()key=0;plaintextStr=newStringBuffer();ciphertextStr=newStringBuffer();intsetKey()System.out.println(请输入一个Caesar密钥);Scannersc=newScanner(System.in);trykey=sc.nextInt()%26;catch(Exceptione)System.out.println(需要输入整数!
);returnkey;voidgetPlaintext()/获得明文plaintext=newcharmax;for(intj=0;j=a&ch=A&ch=Z|ch=|ch=,|ch=.|ch=!
)plaintexti=ch;i+;elseSystem.out.println(输入不支持!
);break;trych=(char)bf.read();catch(IOExceptione1)catch(IOExceptione)voidencryption()/加密inttemp=0;ciphertext=newcharmax;for(intj=0;jmax;j+)ciphertextj=;for(inti=0;i=a&plaintexti122)ciphertexti=(char)(97+temp-123);elseciphertexti=(char)temp;if(plaintexti=A&plaintexti90)ciphertexti=(char)(65+temp-91);elseciphertexti=(char)temp;ciphertextStr.append(ciphertexti);elsebreak;voiddeciphering()/解密charc=;inttemp=0;for(inti=0;i=97&ciphertexti=122)c=(char)(temp);if(temp=65&ciphertexti=90)c=(char)(temp);if(temp65)c=(char)(26+temp);plaintextStr.append(c);elsebreak;voiddisplay()System.out.println(密文明文对比);System.out.println(密文:
+ciphertextStr);System.out.println(明文:
+plaintextStr);主函数部分:
packagening.hao;publicclassComplicationpublicstaticvoidmain(Stringargs)Cryptologycry=newCryptology();cry.setKey();cry.getPlaintext();cry.encryption();cry.deciphering();cry.display();结果1:
结果2: