Created
March 1, 2014 22:13
-
-
Save richcsmith/9298280 to your computer and use it in GitHub Desktop.
encryption.rb
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
#create an encryption algorithm (steps to mask a word) | |
encryption = {"a"=>"c", "b"=>"d", "c"=>"e", "d"=>"f", "e"=>"g", | |
"f"=>"h", "g"=>"i", "h"=>"j", "i"=>"k", "j"=>"l", | |
"k"=>"m", "l"=>"n", "m"=>"o", "n"=>"p", "o"=>"q", | |
"p"=>"r", "q"=>"s", "r"=>"t", "s"=>"u", "t"=>"v", | |
"u"=>"w", "v"=>"x", "w"=>"y", "x"=>"z", "y"=>"a", "z"=>"b"} | |
decryption = encryption.invert | |
#ask the user for a word | |
puts "Please type a word." | |
users_word = gets.chomp | |
#break the word up into a collection of its letters and save it to an array | |
split_word = users_word.split("") | |
#create a new array called secret_word as a placeholder to store each encrypted letter | |
secret_word = [] | |
#iterate through each letter in word | |
split_word.each do |letter| | |
#lookup the letter's encrypted value | |
encrypted_letter = encryption[letter] | |
#store the encrypted value in secret_word array | |
secret_word << encrypted_letter | |
#join the secret_word array into one word and print to screen | |
end | |
puts "" | |
puts "Your encrypted word is:" | |
encrypted_word = secret_word.join("") | |
puts "#{encrypted_word}" | |
# Decrypt the encrypted word and print the original word | |
decrypted_word = [] | |
encrypted_word = encrypted_word.split("") | |
encrypted_word.each do |letter| | |
decrypted_letter = decryption[letter] | |
decrypted_word << decrypted_letter | |
end | |
puts "" | |
puts "Your decrypted word is:" | |
decrypted_word = decrypted_word.join("") | |
puts "#{decrypted_word}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment