Search code examples
pythonpython-3.5caesar-cipher

Caesar Cipher Query


Okay, so here it is, my code, the problem with it is that I can't seem to get it to return to its original form when I choose the Decrypt option, and I am not sure what is wrong with it. And I was wondering if you guys could give me some pointers as to what is wrong and as to what I should do? Here is my code:

Choice = input("Would you like to decrypt or encrypt a message?\nPlease enter 'E' or 'D': ")
Message = input("Enter text to Cipher: ")
Offset = int(input("Please enter your offset: "))
Encrypt = ''
Decrypt = ''

if (Choice == 'e') or (Choice == 'E'):
    for character in Message:
        x = ord(character) - 32
        Encrypt += chr(((Offset + x) % 94) + 32)
    print(Encrypt)
if (Choice == 'd') or (Choice == 'D'):
    for character in Message:
        x = ord(character) - 32
        Decrypt += chr(((Offset - x) % 94) + 32)
    print(Decrypt)

Solution

  • You have to write:

        Decrypt += chr((( x -Offset) % 94) + 32)
    

    not

        Decrypt += chr(((Offset - x) % 94) + 32)