Skip to content

Instantly share code, notes, and snippets.

@BolajiOlajide
Forked from jameslyons/caesarCipher.py
Created October 2, 2018 03:13
Show Gist options
  • Save BolajiOlajide/5bbe5c2841cdc26be267f95eebfa5446 to your computer and use it in GitHub Desktop.
Save BolajiOlajide/5bbe5c2841cdc26be267f95eebfa5446 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment