Question

I am new in Java and I need to write an algorithm for cracking Caesar's cipher using brute force and then matching the words from the dictionary to find the correct shift. That's the code I have written so far. I would really appreciate if somebody helps me with the implementation of shifting the letters and matching them with the file 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;
}
Was it helpful?

Solution

You need to convert the alphabet array to List to use the indexOf method:

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

Inside you can then do the following:

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

You should probably begin the iteration of key with 1 and not with 0, because you will then just get the ciphertext back.

Complete solution:

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);
        }
    }

}

Note the differences of the char types.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top