Created
March 15, 2014 02:00
-
-
Save annawinkler/9560786 to your computer and use it in GitHub Desktop.
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
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