Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tlowrimore
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlowrimore/9405897 to your computer and use it in GitHub Desktop.
Save tlowrimore/9405897 to your computer and use it in GitHub Desktop.
A Naive (Somewhat Readable) Vigenére Cipher
module Vigenere
def self.encrypt(key, phrase)
exec key, phrase
end
def self.decrypt(key, phrase)
exec key, phrase, -1
end
def self.exec(key, phrase, direction=1)
kl = key.length
pl = phrase.length
offset = 10
base = 36
chs = pl.times.map do |i|
ch = ((phrase[i].to_i(base) - offset) + (key[i%kl].to_i(base) - offset) * direction) % 26
(ch + offset).to_s(base)
end
chs.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment