概率算法/重合指数法 破解凯撒偏移密码

//作者-生化,用于密码学基础作业- -
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);
	}
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值