欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

维吉尼亚密码解密

程序员文章站 2022-07-08 20:23:32
...

利用重合法根据密文串找到**长度,找到**从而找到**,我也不知道自己理解的对不对,仅供参考
https://wenku.baidu.com/view/8f9d144deef9aef8941ea76e58fafab069dc44a7.html 这个网址是有关维吉尼亚密码的和重合指数法解密的基本知识

import java.util.Scanner;
/*
 * 利用重合指数法解密,划分子串,计算每个子串与C=0.065的差值
 * 若差值小于0.01说明此次划分的子串个数就是**字长度,然后根据
 * **字长度划分字符再结合公式得到**,从而找到明文
 */
public class Virginia {

	public Virginia() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		System.out.println("输入密文:");
		Scanner str=new Scanner(System.in);
		Virginia vir=new Virginia();
		String s=str.nextLine();
		int branchLength=0;//**长度
		//确定**长度
		for(int i=1;i<s.length();i++) {
			if(vir.suitable(s, i)) {
				//说明此次划分的每个子串都离0.065很近
				branchLength=i;
				break;
			}
		}
		//确定**
		int[] key=vir.getKeys(s, branchLength);
		//得到明文
		String plainCode=vir.deCode(s,key);
		System.out.println("明文是:\n"+plainCode);
	}
	
	//根据密文s和**keys得到明文 
	public String deCode(String s,int[] keys) {
		String text="";
		for(int i=0,j=0;i<s.length();i++,j++) {
			if(j==keys.length)   
				j=0;
			int result=0;
			if((s.charAt(i)-'A'-keys[j])<0) {
				result=(s.charAt(i)-'A'-keys[j])+26+'A';
			}else {
				result=(s.charAt(i)-'A'-keys[j])+'A';
			}
			text+=(char)result;
		}
		return text;
	}
	
	//根据密文串s和**长度m确定**
	public int[] getKeys(String s,int m) {
		float[] FRE= {0.082f,0.015f,0.028f,0.043f,0.127f,0.022f,0.020f,0.061f,
				0.070f,0.002f,0.008f,0.040f,0.024f,0.067f,0.075f,0.019f,0.001f,
				0.060f,0.063f,0.091f,0.028f,0.010f,0.023f,0.001f,0.020f,0.001f};
		float a=0.0f;
		String[] bs=branches(s,5);
		int[] keys=new int[bs.length];
		for(int t=0;t<bs.length;t++) {
			for(int k=0;k<26;k++) {
				for(int i=0;i<26;i++) {
					a+=FRE[i]*pinlv(bs[t],(char)((i+k)%26+65));
				}
	
				if(Math.abs(0.065-a)<0.01) {
					keys[t]=k;
				}
				a=0;
			}
		}
		return keys;
	}
	
	//把字符串str划分成m个子串
	public String[] branches(String str,int m) {
		String[] branches=new String[m];//子串数组
		for(int i=0;i<str.length();) {
			for(int j=0;j<m;j++,i++) {
				if(branches[j]==null) {
					branches[j]=""+str.charAt(i);
				}else if(i<str.length()) {
					branches[j]=branches[j]+str.charAt(i);
				}
			}
		}
		return branches;
	}
	
	//计算子串str中某一字符c出现的频率
	public float pinlv(String str,char c) {
		float sum=0.0f;
		
		//统计字符c出现的次数
		for(int i=0;i<str.length();i++) {
			if(str.charAt(i)==c) {
				sum++;
			}
		}
		return sum/(float)str.length();
	}
	
	//判断每个子串的重合指数是否都接近0.065
	public boolean suitable(String str,int m) {
		final float C=0.065f;
		String[] bs=branches(str,m);//子串数组
		
		//若每个子串的重合指数和0.065的差值的绝对值小于0.01,则此次划分合理,返回true
		for(int j=0;j<m;j++) {
			if(Math.abs(C-overlapIndex(bs[j]))>0.01) {
				return false;
			}
		}
		return true;
	}
	
	//计算字符串str重合指数
	public float overlapIndex(String str) {
		float index=0;
		float sum=0;
		float frequency=0;//字母频度
		float length=str.length();//文本长度
		
		for(int i=0;i<str.length();i++) {
			char c=str.charAt(0);
			for(int j=0;j<str.length();j++) {
				if(str.charAt(j)==c) {
					frequency++;
				}
			}
			sum+=frequency*(frequency-1);
			str=str.replaceAll(str.substring(0, 1), "");
			frequency=0;
		}
		index=sum/(length*(length-1));
		return index;
	}
}

相关标签: 网络安全