Solve vigenere code without key - python

7k Views Asked by At

This is the problem I have to figure out:

"A message has been encryption using Vigenere encryption method discussed in class, and its cipher text EQNVZ has been intercepted. Break the code. Assume that the key is the same length as the message, and the message and the key consist only of uppercase letters."

Is there are way to do this in python? I know there is a way to make a cipher text in python, but is there a way to go the other way?

3

There are 3 best solutions below

2
On BEST ANSWER

It is a trick question. If "the key is the same length as the message" then you have a one-time pad that conceals everything except the message length.

0
On

Since your cipher text is so short, it's probably easiest to just brute force it:

def break_vignere(input, list_of_words):
    for word in list_of_words:
        crypt = vignere(word)
        if crypt == input:
            return word
    return None

Of course, it can fail if we don't find the input text in our list_of_words.

0
On

if "EQNVZ" is the whole ciphertext, then the key is also 5 uppercase characters so

from string import uppercase
from itertools import product, imap
for key in imap("".join, product(uppercase, repeat=5)):
    if test(key):
        break

will test all the keys assuming you have a function test() that checks that the plaintext is all uppercase and perhaps against a dictionary.