1

A classic:

'''Caesar_Cipher1.py
Caesar Cipher encryption/decryption
using Python's double-ended queue rotation
'''

import string
from collections import deque

def caesar_cipher(text, shift, decode=False):
    # change from encode to decode if decode=True
    if decode:
        shift = -shift
    alpha = string.ascii_uppercase + string.ascii_lowercase
    dq = deque(alpha)
    # shift/rotate the alphabet (dq in place)
    dq.rotate(shift)
    crypted = ""
    for c in text:
        if c in alpha:
            # get the letter's index in alpha and apply to dq
            index_alpha = alpha.index(c)
            crypted += dq[index_alpha]
        else:
            crypted += c
    return crypted


# testing ...
# shift key should be less than 26
shift = 11

# test, change letters not other characters
text = "The quick brown fox jumped over the lazy dogs ..123!"
print(text)

encoded = caesar_cipher(text, shift)

print(encoded)

print(caesar_cipher(encoded, shift, decode=True))

''' result ...
The quick brown fox jumped over the lazy dogs ..123!
IWT fjXRZ Qgdlc Udm YjbeTS dkTg iWT aPon SdVh ..123!
The quick brown fox jumped over the lazy dogs ..123!
'''
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.