Skip to content

Instantly share code, notes, and snippets.

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