Skip to content

Instantly share code, notes, and snippets.

@eric-wood
Last active August 29, 2015 14:14
Show Gist options
  • Save eric-wood/4733901a49f3f7b2c611 to your computer and use it in GitHub Desktop.
Save eric-wood/4733901a49f3f7b2c611 to your computer and use it in GitHub Desktop.
@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