Skip to content

Instantly share code, notes, and snippets.

@jameslyons
Last active February 10, 2022 19:53
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jameslyons/8701593 to your computer and use it in GitHub Desktop.
Save jameslyons/8701593 to your computer and use it in GitHub Desktop.
Caesar Cipher Python
# we need 2 helper mappings, from letters to ints and the inverse
L2I = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
I2L = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
key = 3
plaintext = "DEFEND THE EAST WALL OF THE CASTLE"
# encipher
ciphertext = ""
for c in plaintext.upper():
if c.isalpha(): ciphertext += I2L[ (L2I[c] + key)%26 ]
else: ciphertext += c
# decipher
plaintext2 = ""
for c in ciphertext.upper():
if c.isalpha(): plaintext2 += I2L[ (L2I[c] - key)%26 ]
else: plaintext2 += c
print plaintext
print ciphertext
print plaintext2
@ZiadTariq506
Copy link

I needed to edit a littell bit to run on python3 but I like it good job pro : ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment