Created
March 15, 2014 02:00
-
-
Save annawinkler/9560788 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, letters) | |
# Represent each plaintext letter as a corresponding number | |
# for example a=>0, b=>1, ... y=>25, z=>26, '<space>'=>27 | |
ciphertext_as_numbers = [] | |
ciphertext.downcase.each_char do |l| | |
ciphertext_as_numbers << letters.index(l) | |
end | |
# encryption... Caesar cipher shift | |
# add the key to each number and mod by 27 | |
decrypted = [] | |
ciphertext_as_numbers.each do |n| | |
decrypted << ((n-key) % letters.length) | |
end | |
# convert the shifted/encrypted numbers back to letters | |
plaintext = "" | |
decrypted.each do |n| | |
plaintext << letters[n] | |
end | |
plaintext | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment