Question

I am trying to develop a program that encodes, decodes, and breaks the encryption for a message encoded with a Vigenere cipher. Where I am getting stuck is breaking [the encryption on] the message (without the key). I have an idea of how to go about doing it, but I'm not sure how to code it. My idea is a follows:

The program would systematically generate potential keys, with a length starting at 1 and ending at 26. The keys would contain the letters of the English alphabet and are NOT case sensitive. For each key length (anywhere from 1-26), the key would be filled with the letter 'a', and then the program would check to see if they key was corrent (I have another method for that). If they key was incorrect, then the letter in the last position would be rotated to the next letter in the alphabet. Once the last letter had gone through all 26 possible positions, the second to last letter would be rotated, and then the last letter and the second to last letter would rotate accordingly, and so on a so forth (all the way back to the first letter of the [potential] key). Each time a new key is generated, the [potential] key is being checked with a separate method, and the method stops when the correct key is found. The key creation progression would go something like this:

[starting with keys that are only 1 letter long]
a
b
c
...
x
y
z

[now the potential key length becomes two]
aa
ab
ac
ad
...
zw
zx
zy
zz

[eventually the potential key length becomes 26]
aaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaab
aaaaaaaaaaaaaaaaaaaaaaaaac
aaaaaaaaaaaaaaaaaaaaaaaaad
...
zzzzzzzzzzzzzzzzzzzzzzzzzw
zzzzzzzzzzzzzzzzzzzzzzzzzx
zzzzzzzzzzzzzzzzzzzzzzzzzy
zzzzzzzzzzzzzzzzzzzzzzzzzz

(hopefully you can see the pattern there)

If anyone has or knows the code for how to do this, or could help guide met through the steps necessary to code this, it would be much appreciated.

Thanks!

Was it helpful?

Solution

Edit (now that I've done the math)

There are around 6*10^36 possible combinations you'll need to iterate through - the largest long value is about 9*10^18 - a much smaller number.

That being said, say you find an optimized way to iterate over the combinations where you can generate and compare a trillion (10^12) combinations per second (much faster than your average developer's machine), and you can parallelize it across a million machines - that would be (60*60*24*365*10^12)*10^6 per year, or about 3*10^25 combinations checked per year.

At that blazing speed, it would still take ~190 billion years to go over all combinations.

I strongly urge you to investigate another alternative for achieving your goal instead of trying every single key.

Now (back to my original answer), if you had a reasonably sized subset of keys you wanted to iterate over, I could imagine doing something like this with a straight up number loop that just converted the number to a modified base-26 for the key.

Some pseudo-code:

public void runAlgorithm () {
    for (int i=startKey; i<endKey; i++) {
        String key = toBase26(i);
        testKey(key);
    }
}

Use something like this hexavigesimal converter from wikipedia for the impl of toBase26 (copied below):

public static String toBase26(int number){
    number = Math.abs(number);
    String converted = "";
    // Repeatedly divide the number by 26 and convert the
    // remainder into the appropriate letter.
    do
    {
        int remainder = number % 26;
        converted = (char)(remainder + 'A') + converted;
        number = (number - remainder) / 26;
    } while (number > 0);

    return converted;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top