Skip to content

Instantly share code, notes, and snippets.

@LolWalid
Created December 13, 2017 20:59
Show Gist options
  • Save LolWalid/5e3089c79061fe8992768d49d4ee7f1b to your computer and use it in GitHub Desktop.
Save LolWalid/5e3089c79061fe8992768d49d4ee7f1b to your computer and use it in GitHub Desktop.
Paris Workshop ROT 13
class EncryptionEngine
def self.encrypt(string)
results = ''
string.each_char do |l|
results += rot(l)
end
results
end
def self.decrypt(string)
results = ''
string.each_char do |l|
results += rot(l, 13)
end
results
end
def self.rot(letter, num = 13)
case letter
when /^([a-m]|[A-M])$/
x = (letter.ord + num).chr
when /^([n-z]|[N-Z])$/
x = (letter.ord - num).chr
else
letter
end
end
end
def main
test1
test2
end
def test1
text = 'HELLO friend'
if 'URYYB sevraq' == EncryptionEngine.encrypt(text)
puts '.'
else
puts 'TEST 1 failed'
puts "actual: #{EncryptionEngine.encrypt(text)}"
end
end
def test2
text = 'URYYB sevraq'
if 'HELLO friend' == EncryptionEngine.decrypt(text)
puts '.'
else
puts 'TEST 2 failed'
puts "actual: #{EncryptionEngine.decrypt(text)}"
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment