1、代码:package ning.hao;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner;public class Cryptology char ciphertext;/密文 int key;char plaintext;/明文 StringBuffer plaintextStr;StringBuffer ciphertextStr;final int max=500;Cryptology()key=0;plain
2、textStr=new StringBuffer();ciphertextStr=new StringBuffer();int setKey()System.out.println(请输入一个 Caesar 密钥);Scanner sc=new Scanner(System.in);try key=sc.nextInt()%26;catch(Exception e)System.out.println(需要输入整数!);return key;void getPlaintext()/获得明文 plaintext=new charmax;for(int j=0;j=a&ch=A&ch=Z|ch=|
3、ch=,|ch=.|ch=!)plaintexti=ch;i+;else System.out.println(输入不支持!);break;try ch=(char)bf.read();catch(IOException e1)catch(IOException e)void encryption()/加密 int temp=0;ciphertext=new charmax;for(int j=0;jmax;j+)ciphertextj=;for(int i=0;i=a&plaintexti122)ciphertexti=(char)(97+temp-123);else ciphertexti
4、=(char)temp;if(plaintexti=A&plaintexti90)ciphertexti=(char)(65+temp-91);else ciphertexti=(char)temp;ciphertextStr.append(ciphertexti);else break;void deciphering()/解密 char c=;int temp=0;for(int i=0;i=97&ciphertexti=122)c=(char)(temp);if(temp=65&ciphertexti=90)c=(char)(temp);if(temp65)c=(char)(26+tem
5、p);plaintextStr.append(c);else break;void display()System.out.println(密文明文对比);System.out.println(密文:+ciphertextStr);System.out.println(明文:+plaintextStr);主函数部分:package ning.hao;public class Complication public static void main(String args)Cryptology cry=new Cryptology();cry.setKey();cry.getPlaintext();cry.encryption();cry.deciphering();cry.display();结果 1:结果 2:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1