-2

我正在制作一个小程序来猜测一个vigenere-cipher的密钥长度,给定长度在5-15之间,在java中。我通过计算转置的标准偏差来做到这一点。但我真正的问题是为什么我在方法“countFrequency”中的数组中出现越界错误。我似乎看不到它超出范围的地方。

我知道我的程序可能会更有效率,但我认为一旦修复了这个错误,它应该可以工作。我的程序代码如下。

谢谢!

import java.awt.List;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class main {

// Scanning the encrypted text

// private static char encryptedText[];

public static void main(String[] args) throws IOException{
Scanner scan = null;new Scanner(System.in);
scan = new Scanner(System.in);
int i;
int vectors[][];
System.out.println("Give the decrypted text: \n");
String encryptedText =  scan.next().toString();
vectors = makeArray();
vectors = countFrequency(vectors, encryptedText);
calculateDeviations(vectors);

// No need to scan more

if(scan!=null)
    scan.close();
}


private static void calculateDeviations(int[][] vectors) {
    int i;
    int j;
    int sumpowfreq;
    int freqpowsum;
    for(i=0;i<12;i++){
        double deviation =0;
        for(j=0; j<26;j++){
            sumpowfreq = sumPowwFreq(vectors,i);
            freqpowsum = freqPowwSum(vectors,i);
            deviation = Math.sqrt((sumpowfreq/26) - (freqpowsum/26));
        }
        System.out.println("The devation of " + j + " is " + deviation + ".");
    }

}


private static int freqPowwSum(int[][] vectors, int i) {
    int powsum=0;
    int sum=0;
    int j;
    for(j=0;j<26;j++){
        sum = sum + vectors[j][i];
    }
    powsum = sum * sum;
    return powsum;
}


public static int sumPowwFreq(int[][] vectors, int i) {
    int sum=0;
    int j;
    for(j=0;j<26;j++){
        sum = sum + (vectors[j][i] * vectors[j][i]);
    }
    return sum;
}

public static int[][] makeArray() {
// Making the 2-dimensional array and set it to 0
    int keySize;
    int letterFrequency[][] = new int[26][11];
    for(keySize=5;keySize<16;keySize++){
        int j;
        for(j=0;j<26;j++){
            letterFrequency[j][keySize-5] = 0;  
            }
        }
    return letterFrequency;
}


public static int[][] countFrequency(int freq[][], String encryptedText){
    int i,j,c;
    int splitSize;
    int ascii;
    String splittedText[];
    for(splitSize=5; splitSize<15; splitSize++){
        splittedText = splitText(splitSize, encryptedText);
        for(j=0;j<splitSize;j++){
            for(c=0;c<splittedText[j].length();c++){
                ascii= splittedText[j].charAt(c);
                ascii = ascii - 97; // because the ascii table starts at 97, 0 represents an 'a' now
                                    // and we assumed that the encrypted text only contained small letters
                freq[ascii][j]++;
            }
        }       
    }   

    return freq;
}


public static String[] splitText(int partLength, String encryptedText){
    int len = encryptedText.length();

    int amountparts = len / (partLength);
    String parts[] = new String[amountparts];

    // Break into parts
    int offset= 0;
    int i = 0;
    while (i < amountparts){
        parts[i] = encryptedText.substring(offset, Math.min(offset + partLength, len));
        offset += partLength;
        i++;
    }

    return parts;
}

}

4

1 回答 1

0

我不是加密怪胎,但我进入了您的代码并看到了异常。我认为,您的问题是特定领域而不是技术问题......

countFrequency() 中的嵌套 for 循环要求数组 splittedText 至少包含 15 个元素。

  • 对于 1) splitSize 从 5 运行到 14
  • 对于 2) 对于每个 splitSize,j 从 0 运行到 splitSize
  • 对于 3) 对于每个 j,对 splittedText 的第 j 个元素做一些事情

最后一个对于异常很重要:

splittedText 必须至少包含 15 个元素。

splittedText 中有多少个元素?

这是在 splitText(int, String) 中确定的。此方法在 (for 1) 内调用,当前 splitSize 为 partLength,用户输入为 encryptedText。splitText 返回一个包含 (encryptedText.length() / partlength) 元素的数组。所以我们有:

splittedText.length() = (inputlength / splitSize)

对于每个 splitSize(5 到 14),输入长度必须满足以下条件:

输入长度/splitSize >= splitSize ; 这相当于

输入长度 >= splitSize * splitSize

对于 14 的 splitSize,您的输入必须大于 196。

据我所知,这对 vigenere-ciphers 没有限制。而且 - 顺便说一句 - 如果你传递了第一个 ArrayOutOfBoundException,你将遇到下一个异常。

于 2014-09-18T14:59:58.887 回答