Search code examples
pythonpython-2.7encryptiontransposevigenere

How to decrypt a vigenere columnar transposition cipher


I am going through old exams, my final is in a few days. I want to decrypt a ciphertext, it was first encrypted by vigenere and then encrypted by columna transposition. How do I decrypt to get original message?

message -> rgyqhbmnwaazxcajittuzqyagkx
vigenere key -> final
columnar transposition key -> exam

I have spent hours on this, but am not getting anything out. I first want to decrypt by columnar then Vigenere. I think output should look English, but am just getting junk. My vigenere code is below, I guess the problem is with the columnar, all the codes I have come across take numbers as key, but I have a letter key.

def decrypt(message, password):
    decrypted = ''
    for i in range(0, len(message)):
        letter = ord(message[i]) - ord(password[i%len(password)]) + 65
        if letter < 65:
            letter += 26
        decrypted += chr(letter)
    return decrypted

thanks


Solution

  • The columnar transposition does take a word as key, not a number. If "exam" is the key, then you write out the message from left to write in rows of four and read off the cipher text from top to bottom starting with column 3, then column 1, then column 4 then column 2. The order comes form the alphabetical order of the letters "e","x","a","m": "2nd", "4th", "1st", "3rd".

    For example, to encrypt "THIS IS THE MESSAGE":

    E  X  A  M
    ----------
    T  H  I  S
    I  S  T  H
    E  M  E  S  
    S  A  G  E
    

    --> ITEG TIES SHSE HSMA

    I.e., "ITEGTIESSHSEHSMA".

    To decrypt do the reverse. I don't want to solve your homework for you, but I will say that, when you properly decrypt via the columnar transposition with "exam" you will find a string starting with "nzrawq..."

    When you then further decrypt that string with the vigenere cipher you will indeed find English words, in particular a string starting with "ireallywant..."