Created
November 12, 2021 16:32
-
-
Save Niall47/b2fac37e9e1c773eb7c0c3946ef5004f 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
=begin | |
Atbash cipher | |
The Atbash cipher is a type of ancient encryption created to encode messages in Hebrew. | |
At its heart is a very simple substitution cipher that transposes all the letters in the alphabet backwards. The first letter in the alphabet is replaced by the the last letter so A -> Z, B -> Y etc. | |
Plain: abcdefghijklmnopqrstuvwxyz | |
Cipher: zyxwvutsrqponmlkjihgfedcba | |
It is obviously a very weak encryption mechanism! | |
It is also an example of a symmetric encryption - the same key (our algorithm in this case) is used to both encrypt and decrypt the message. | |
Examples | |
Encoding test gives gvhg | |
Encoding gvhg gives test | |
Your algorithm should | |
convert all input to lower case | |
preserve spaces (don't convert them) | |
output the encoded message | |
=end | |
input_text = ARGV[0].downcase.chars # Take input and convert to array | |
alphabet = ('a'..'z').to_a | |
cipher = alphabet.reverse # Create each part of the cipher | |
cipher_hash = Hash[*alphabet.zip(cipher).flatten] # Marry up the matching values and convert into a hash | |
output = '' | |
input_text.each do | letter | | |
# If we find the letter in the hash we use if, if not its a special character and we add that in as is | |
cipher_hash[letter] != nil ? output += cipher_hash[letter] : output += letter | |
end | |
puts output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment