Last active
August 29, 2015 13:57
-
-
Save tlowrimore/9405897 to your computer and use it in GitHub Desktop.
A Naive (Somewhat Readable) Vigenére Cipher
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
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