//作者-生化,用于密码学基础作业- -
package Algorithms;

import java.util.ArrayList;
import java.util.List;
import java.lang.Math;;

public class ShiftCiper {

	public static void satitisticalAnalysis() {
		String cipherText = "OVDTHUFWVZZPISLRLFZHYLAOLYLWSLHZLMPUKAOLHUZDLY";
		List<prob> probList = new ArrayList<prob>();
		addprob(probList);
		List<prob> cipherList = new ArrayList<prob>();
		addprob(cipherList);
		for(int i=0;i<cipherList.size();i++){
			cipherList.get(i).setProb(0);
		}
		for(int i=0;i<cipherText.length();i++){
			int loc = (int)(cipherText.charAt(i) - 'A');
			cipherList.get(loc).addProb();
		}
		for(int i=0;i<cipherList.size();i++){
			cipherList.get(i).setProb(cipherList.get(i).getProb()/cipherText.length());
		}
		
		for(int i=0;i<26;i++){
			double result = 0;
			for(int j=0;j<26;j++){
				result+= probList.get(j).getProb() * cipherList.get((i+j)%26).getProb();
			}
			if(Math.abs(result-0.065)<=0.005){
				System.out.println("当偏移量为" + i + "时,统计量为"+result);
				String plainText="";
				for(int m=0;m<cipherText.length();m++){
					char s= (char)(cipherText.charAt(m)-i);
					if(s>'Z'){
						s-=26;
					}else if(s<'A'){
						s+=26;
					}
					s+='a'-'A';
					plainText+=s;
				}
				System.out.println("原文为:"+plainText);
			}
		}
	}
	
	public static void main(String[] args) {
		satitisticalAnalysis();
	}

    static class prob {
		String key;
		double prob;
		
		public prob(String key, double prob) {
			super();
			this.key = key;
			this.prob = prob/100;
		}
		
		public void addProb(){
			prob = prob+1;
		}

		public String getKey() {
			return key;
		}

		public void setKey(String key) {
			this.key = key;
		}

		public double getProb() {
			return prob;
		}

		public void setProb(double prob) {
			this.prob = prob;
		}
		
	}

	private static void addprob(List<prob> problist) {
		// TODO Auto-generated method stub
		prob a = new prob("a", 8.2);
		prob b = new prob("b", 1.5);
		prob c = new prob("c", 2.8);
		prob d = new prob("d", 4.3d);
		prob e = new prob("e", 12.7);
		prob f = new prob("f", 2.2d);
		prob g = new prob("g", 2.0d);
		prob h = new prob("h", 6.1d);
		prob i = new prob("i", 7.0d);
		prob j = new prob("j", 0.2d);
		prob k = new prob("k", 0.8d);
		prob l = new prob("l", 4.0d);
		prob m = new prob("m", 2.4d);
		prob n = new prob("n", 6.7d);
		prob o = new prob("o", 1.5d);
		prob p = new prob("p", 1.9d);
		prob q = new prob("q", 0.1d);
		prob r = new prob("r", 6.0d);
		prob s = new prob("s", 6.3d);
		prob t = new prob("t", 9.1d);
		prob u = new prob("u", 2.8d);
		prob v = new prob("v", 1.0d);
		prob w = new prob("w", 2.4d);
		prob x = new prob("x", 0.2d);
		prob y = new prob("y", 2.0d);
		prob z = new prob("z", 0.1d);
		problist.add(a);
		problist.add(b);
		problist.add(c);
		problist.add(d);
		problist.add(e);
		problist.add(f);
		problist.add(g);
		problist.add(h);
		problist.add(i);
		problist.add(j);
		problist.add(k);
		problist.add(l);
		problist.add(m);
		problist.add(n);
		problist.add(o);
		problist.add(p);
		problist.add(q);
		problist.add(r);
		problist.add(s);
		problist.add(t);
		problist.add(u);
		problist.add(v);
		problist.add(w);
		problist.add(x);
		problist.add(y);
		problist.add(z);
	}
}

作者-生化,用于密码学基础作业- -

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐