Skip to content

Instantly share code, notes, and snippets.

@jasonrdsouza
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonrdsouza/a3f2657822f98ef48587 to your computer and use it in GitHub Desktop.
Save jasonrdsouza/a3f2657822f98ef48587 to your computer and use it in GitHub Desktop.
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