Skip to content

Instantly share code, notes, and snippets.

@annawinkler
Created March 15, 2014 02:00
Show Gist options
  • Save annawinkler/9560786 to your computer and use it in GitHub Desktop.
Save annawinkler/9560786 to your computer and use it in GitHub Desktop.
def decrypt( ciphertext, key ):
""" given ciphertext and key, return decoded plaintext"""
# Represent each plaintext letter as a corresponding number
# for example a=>0, b=>1, ... y=>25, z=>26, '<space>'=>27
ciphertext_as_numbers = []
for l in ciphertext.lower():
ciphertext_as_numbers.append( letters.index(l) )
# encryption... Ceaser cipher shift
# add the key to each number and mod by 27
decrypted = []
for n in ciphertext_as_numbers:
decrypted.append( (n - key) % 27 )
# convert the shifted/encrypted numbers back to letters
plaintext = ""
for n in decrypted:
plaintext += letters[n]
return plaintext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment