Instantly share code, notes, and snippets.

Embed
What would you like to do?
Caesar Cipher Decryption Script
"""
Code to crack Google Security Key Challenge #3
Accompanying blog post: http://www.jasondsouza.org/post/caesar-cipher-decryption/
@author: Jason Dsouza
"""
from string import punctuation
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
KEY_SPACE = len(ALPHABET)
def create_caesar_cipher(N):
# we use the mod operator here to perform the wrap-around mentioned above
cipher = {ALPHABET[i]: ALPHABET[(i+N) % KEY_SPACE] for i, _ in enumerate(ALPHABET)}
return cipher
def decrypt(cipher, ciphertext):
# remember we need to ignore letter case and punctuation
decrypted = [letter if letter in punctuation + " " else cipher[letter] for letter in ciphertext.lower()]
return "".join(decrypted)
if __name__ == "__main__":
cipher_text = "Wuhdw brxu sdvvzrug olnh brxu wrrwkeuxvk. Grq'w ohw dqbergb hovh xvh lw, dqg jhw d qhz rqh hyhub vla prqwkv. Foliirug Vwroo"
for n in range(KEY_SPACE):
cipher = create_caesar_cipher(n)
plaintext = decrypt(cipher, cipher_text)
print("{}:\t{}".format(n, plaintext))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment