Last active
August 29, 2015 14:14
-
-
Save eric-wood/4733901a49f3f7b2c611 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
@alphabet = [" ","a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", | |
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] | |
def encrypt(message) | |
key_length = message.length | |
message.downcase.split("").map! { |x| | |
idx_number = @alphabet.index(x) | |
finish = idx_number.to_i + key_length.to_i | |
unless finish < 27 | |
finish = idx_number.to_i + key_length.to_i - 27 | |
end | |
x = @alphabet[finish] | |
}.join("") | |
end | |
def decrypt(message) | |
key_length = message.length | |
message.downcase.split("").map! { |x| | |
idx_number = @alphabet.index(x) | |
finish = idx_number.to_i - key_length.to_i | |
if finish.to_i < 0 | |
finish = 27 + (idx_number.to_i - key_length.to_i) | |
end | |
x = @alphabet[finish] | |
}.join("") | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment