实现一个功能,对指定密文进行解密

不是真正意义上的加密,可以说是凯撒密码吧。考验的其实就是字符串的处理,基础差,用比较笨的方法实现了,还参考了部分别人的代码:

已知的密文转换方式表:

* 大写字母:
* A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
* EB EC ED EE EF EG EH EI EJ EK EL EM EN EO EP FA FB FC FD FE FF FG FH FI FJ FK
*
* 小写字母:
* a b c d e f g h i j k l m n o p q r s t u v w x y z
* GB GC GD GE GF GG GH GI GJ GK GL GM GN GO GP HA HB HC HD HE HF HG HH HI HJ HK
*
* 数字:
* 1 2 3 4 5 6 7 8 9 0
* DB DC DD DE DF DG DH DI DJ DA
*
* 特殊符号:
* ~ ! @ # $ % ^ & * ( ) _ + - = { } | \ : ; ' " < > ? / , .
* HO CB EA CD CE CF FO CG CK CI CJ FP CL CN DN HL HN HM FM DK DL CH CC DM DO DP CP CM CO
*
* 空格:
* CA
*
* 分析:
* 1.输入一串密文
* 2.将密文每两个后面添加一个,号分隔。

* 3.将添加逗号的密文按照逗号分割存入数组,转换成明文后再将其拼凑

* 4.输出明文

 

public class demo {
public static void main(String[] args) throws UnsupportedEncodingException {  
		
		Scanner sc = new Scanner(System.in);    //创建键盘录入对象
		System.out.println("请输入密文:");   //提示
		String input = sc.nextLine();    //接受
		
		if ((input.length()%2) != 0) {    //判断密文是否双数
			System.out.println("抱歉!输入有误,请输入双数密文!");
		}else {
			StringBuffer sb = new StringBuffer();   //创建StringBuffer对象
			String temp = input;      //设置临时变量
			int num = 1;     //添加,号的截取长度
			int len = input.length();   //输入字符串的总长度
			
			String dd = null;   //初始化
			while(len > 0) {   //利用while循环对驶入字符串进行添加,号分隔符操作
				
				int index = getEndIndex(input,num);   //调用方法
				
				sb.append(temp.substring(0,index + 1)).append(",");
				
				temp = temp.substring(index + 1);
				
				len = temp.length();
				
				dd = sb.toString();
				
				
			}
			
			String [] arr = dd.split(",");
			
			for(int x=0;x<arr.length;x++) {
				
				String da = arr[x];
				 String decode = null;
				switch(da){
				case "EB":
			        decode = "A";
					break;
				case "EC":
					decode = "B";
					break;
				case "ED":
					decode = "C";
					break;
				case "EE":
					decode = "D";
					break;
				case "EF":
					decode = "E";
					break;
				case "EG":
					decode = "F";
					break;
				case "EH":
					decode = "G";
					break;
				case "EI":
					decode = "H";
					break;
				case "EJ":
					decode = "I";
					break;
				case "EK":
					decode = "J";
					break;
				case "EL":
					decode = "K";
					break;
				case "EM":
					decode = "L";
					break;
				case "EN":
					decode = "M";
					break;
				case "EO":
					decode = "N";
					break;
				case "EP":
					decode = "O";
					break;
				case "FA":
					decode = "P";
					break;
				case "FB":
					decode = "Q";
					break;
				case "FC":
					decode = "R";
					break;
				case "FD":
					decode = "S";
					break;
				case "FE":
					decode = "T";
					break;
				case "FF":
					decode = "U";
					break;
				case "FG":
					decode = "V";
					break;
				case "FH":
					decode = "W";
					break;
				case "FI":
					decode = "X";
					break;
				case "FJ":
					decode = "Y";
					break;
				case "FK":
					decode = "Z";
					break;
				case "GB":
					decode = "a";
					break;
				case "GC":
					decode = "b";
					break;
				case "GD":
					decode = "c";
					break;
				case "GE":
					decode = "d";
					break;
				case "GF":
					decode = "e";
					break;
				case "GG":
					decode = "f";
					break;
				case "GH":
					decode = "g";
					break;
				case "GI":
					decode = "h";
					break;
				case "GJ":
					decode = "i";
					break;
				case "GK":
					decode = "j";
					break;
				case "GL":
					decode = "k";
					break;
				case "GM":
					decode = "l";
					break;
				case "GN":
					decode = "m";
					break;
				case "GO":
					decode = "n";
					break;
				case "GP":
					decode = "o";
					break;
				case "HA":
					decode = "p";
					break;
				case "HB":
					decode = "q";
					break;
				case "HC":
					decode = "r";
					break;
				case "HD":
					decode = "s";
					break;
				case "HE":
					decode = "t";
					break;
				case "HF":
					decode = "u";
					break;
				case "HG":
					decode = "v";
					break;
				case "HH":
					decode = "w";
					break;
				case "HI":
					decode = "x";
					break;
				case "HJ":
					decode = "y";
					break;
				case "HK":
					decode = "z";
					break;
				case "DB":
					decode = "1";
					break;
				case "DC":
					decode = "2";
					break;
				case "DD":
					decode = "3";
					break;
				case "DE":
					decode = "4";
					break;
				case "DF":
					decode = "5";
					break;
				case "DG":
					decode = "6";
					break;
				case "DH":
					decode = "7";
					break;
				case "DI":
					decode = "8";
					break;
				case "DJ":
					decode = "9";
					break;
				case "DA":
					decode = "0";
					break;
				case "HO":
					decode = "~";
					break;
				case "CB":
					decode = "!";
					break;
				case "EA":
					decode = "@";
					break;
				case "CD":
					decode = "#";
					break;
				case "CE":
					decode = "$";
					break;
				case "CF":
					decode = "%";
					break;
				case "FO":
					decode = "^";
					break;
				case "CG":
					decode = "&";
					break;
				case "CK":
					decode = "*";
					break;
				case "CI":
					decode = "(";
					break;
				case "CJ":
					decode = ")";
					break;
				case "FP":
					decode = "_";
					break;
				case "CL":
					decode = "+";
					break;
				case "CN":
					decode = "-";
					break;
				case "DN":
					decode = "=";
					break;
				case "HL":
					decode = "{";
					break;
				case "HN":
					decode = "}";
					break;
				case "HM":
					decode = "|";
					break;
				case "FM":
					decode = "\\";
					break;
				case "DK":
					decode = ":";
					break;
				case "DL":
					decode = ";";
					break;
				case "CH":
					decode = "'";
					break;
				case "CC":
					decode = "\"";
					break;
				case "DM":
					decode = "<";
					break;
				case "DO":
					decode = ">";
					break;
				case "DP":
					decode = "?";
					break;
				case "CP":
					decode = "/";
					break;
				case "CM":
					decode = ",";
					break;
				case "CO":
					decode = ".";
					break;
				case "CA":
					decode = " ";
					break;
					
			   }
			   
				System.out.print(decode);
	}
				
			}

			
		}
		
	
	
	private static int getEndIndex(String input, int num) throws UnsupportedEncodingException {
		int index = 0;
		double val = 0.00;
		
		for(int i=0;i<input.length();i++) {
			
			if (String.valueOf(input.charAt(i)).getBytes("UTF-8").length >= 3) {
				
				val += 1.00;
			}else {
				val +=0.50;
			}
			if(val >=num) {
				index = i;
				if(val - num == 0.5) {
					index = i-1;
				}
				break;
			}
		}
		if(index == 0) {
			index = input.length() - 1;
		}
		return index;
	
		
	}
	
	}