Last active
May 27, 2024 20:52
-
-
Save jameslyons/8701593 to your computer and use it in GitHub Desktop.
Caesar Cipher Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
I needed to edit a littell bit to run on python3 but I like it good job pro : ).