Decrypting a Message

FUNCTION – def decrypt(cipher):

The connections on the wheels are bi-directional. So if a character is encoded as an “F”, then with the wheel in the same position encoding an “F” would return you the original character. Consequently, we can use the same encryption routine to decrypt. We just need a slightly different for loop to feed it characters to decrypt.

Pertinent bits of code

#Decrypt each letter in the ciphertext string.
for e_char in cipher[1:]:
    #Decrypt this character.
    o_char = transformChar(e_char, selectedWheel, pointer)
    #Do something if the character was decrypted ok.
    if len(o_char) > 0:
       plaintext += o_char #Add the character to the result.
       pointer = (pointer + increment)%26 #Turn the wheel, using mod 26 to stay within circle.

Yes, you will find that a lot of grammatical information is lost in the encryption/decryption process, specifically all spacing and punctuation.

It is up to the message recipient to put that back, replacing “X” characters that do not make sense with full stops and inferring spaces where they are appropriate.