标签:
古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报:
设计思路:输入字符串,把每个字符装到数组里,其ascii码+3或-3,最后输出字符串型。
package luoma; import java.util.*; public class Mima { public static void Jiami() { System.out.println("请输入需要加密的字符串"); String zifu=new String(); Scanner chuan=new Scanner(System.in); zifu=chuan.next(); char leng[]=new char[zifu.length()]; for(int i=0;i<zifu.length();i++) { if(zifu.charAt(i)>=‘A‘&&zifu.charAt(i)<=‘W‘) { leng[i]=(char)(zifu.charAt(i)+3); } if(zifu.charAt(i)==‘X‘) leng[i]=‘A‘; if(zifu.charAt(i)==‘Y‘) leng[i]=‘B‘; if(zifu.charAt(i)==‘Z‘) leng[i]=‘C‘; } String show=new String(); for(int j=zifu.length()-1;j>=0;j--)//从右组合字符串 { show=leng[j]+show; } System.out.println("字符串加密后为:"+show); } public static void Jiemi() { System.out.println("请输入需要加密的字符串"); String zifu=new String(); Scanner chuan=new Scanner(System.in); zifu=chuan.next(); char leng[]=new char[zifu.length()]; for(int i=0;i<zifu.length();i++) { if(zifu.charAt(i)>=‘D‘&&zifu.charAt(i)<=‘Z‘) { leng[i]=(char)(zifu.charAt(i)-3); } if(zifu.charAt(i)==‘A‘) leng[i]=‘X‘; if(zifu.charAt(i)==‘B‘) leng[i]=‘Y‘; if(zifu.charAt(i)==‘C‘) leng[i]=‘Z‘; } String show=new String(); for(int j=zifu.length()-1;j>=0;j--)//从右组合字符串 { show=leng[j]+show; } System.out.println("字符串加密后为:"+show); } public static void main(String args[]) { System.out.println("请选择:1--加密;2--解密"); int xuan; Scanner ze=new Scanner(System.in); xuan=ze.nextInt(); if(xuan==1) { Mima m=new Mima(); m.Jiami(); } if(xuan==2) { Mima n=new Mima(); n.Jiemi(); } } }
结果截图:
标签:
原文地址:http://www.cnblogs.com/maplely/p/4909682.html