Jednoduché šifrovanie a dešifrovanie 2
Ukážkový program Jednoduché šifrovanie a dešifrovanie textu 2 v Ruby vrátane zdrojových kódov.
class String def encrypt(passwd) self.length.times do |i| self[i] = (self[i].ord + passwd[i % passwd.length ].ord - ("A".ord - 1)).chr self[i] = (self[i].ord - ("Z".ord - "A".ord + 1)).chr if (self[i].ord > "Z".ord) end return self end def decrypt(passwd) self.length.times do |i| self[i] = (self[i].ord - (passwd[i % passwd.length ].ord - ("A".ord - 1))).chr self[i] = (self[i].ord + ("Z".ord - "A".ord + 1)).chr if (self[i].ord < "A".ord) end return self end end class Translator def input # zadavani vstupu print "Zadejte text: " @text = gets.upcase.strip print "Zadejte heslo: " @passwd = gets.upcase.strip end def run puts "Vitejte v programu sifrovani" puts "============================" option = "" while (!["3"].include?(option)) do puts "1 - sifrovat text" puts "2 - desifrovat text" puts "3 - konec" option = gets.strip case option when "1" input puts @text.encrypt(@passwd) when "2" input puts @text.decrypt(@passwd) end end end end translator = Translator.new translator.run