Java caesars密码暴力强制

时间:2014-05-10 15:10:10

标签: java encryption cryptography brute-force

我是Java新手,我需要编写一个算法,使用强力破解凯撒的密码,然后匹配字典中的单词以找到正确的移位。那是我到目前为止编写的代码。如果有人帮我改变字母并将它们与文件dictionary.txt匹配,我真的很感激。

private char[] alphabet = {'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'};
private char[] decodedText;
private String plainText;

public String producePlaintext(String cipherText) {
    //put each letter of the ciphertext in an array of characters in the upper case format
    char[] message = cipherText.toUpperCase().toCharArray();
    //loop through all the possible keys
    for (int key = 0; key < alphabet.length; key++) {
        //set the value of the decrypted array of characters to be the same as the length of the cipher text
        decodedText = new char[message.length];
        //loop through the characters of the ciphertext
        for (int i = 0; i < message.length; i++) {

            //if character is not space
            if (message[i] != ' ') {
                //shift the letters

            }else{
                decodedText[i] = ' ';
            }
        }
        plainText = String.valueOf(decodedText);
    }
    return plainText;
}

1 个答案:

答案 0 :(得分:1)

您需要将字母数组转换为List才能使用indexOf方法:

private List<Character> alphabetList = java.util.Arrays.asList(alphabet);

在里面,您可以执行以下操作:

decodedText[i] = alphabet[(alphabetList.indexOf(message[i])+key) % alphabet.length];

您可能应该使用key而不是1开始0的迭代,因为您将只返回密文。

完整的解决方案:

public class Test01 {

    private Character[] alphabet = {'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'};
    private char[] decodedText;
    private String[] plainText;
    private java.util.List<Character> alphabetList;

    public Test01(){
        alphabetList = java.util.Arrays.asList(alphabet);
        plainText = new String[alphabet.length];
    }

    public String[] producePlaintext(String cipherText) {
        //put each letter of the ciphertext in an array of characters in the upper case format
        char[] message = cipherText.toUpperCase().toCharArray();
        //loop through all the possible keys
        for (int key = 0; key < alphabet.length; key++) {
            //set the value of the decrypted array of characters to be the same as the length of the cipher text
            decodedText = new char[message.length];
            //loop through the characters of the ciphertext
            for (int i = 0; i < message.length; i++) {

                //if character is not space
                if (message[i] != ' ') {
                    //shift the letters
                    decodedText[i] = alphabet[(alphabetList.indexOf(message[i])+key) % alphabet.length];
                }else{
                    decodedText[i] = ' ';
                }
            }
            plainText[key] = String.valueOf(decodedText);
        }
        return plainText;
    }

    public static void main(String[] args) {
        Test01 t = new Test01();
        for(String pt : t.producePlaintext("abc")) {
            System.out.println(pt);
        }
    }

}

请注意char类型的不同。