Skip to content

Instantly share code, notes, and snippets.

@spellancer
Created May 1, 2014 10:29
Show Gist options
  • Save spellancer/6ecc11e19b2aed1f896e to your computer and use it in GitHub Desktop.
Save spellancer/6ecc11e19b2aed1f896e to your computer and use it in GitHub Desktop.
Cipher Caesar
alpha = ('a'..'z').to_a
#num = (0..25).to_a
print alpha
puts
puts "Шифр Цезаря"
print "Введите исходный текст: "
text = gets
text.gsub!(/\s+/, '')
print "Введите ключ: "
key = gets.to_i
res = ''
text.each_char do |val|
i = alpha.index(val)
i += key
if i > 25
i = i - 25
end
res << alpha[i]
end
puts "Зашифрованное сообщение: #{res}"
puts "\nРасшифровка"
print "Введите зашифрованный текст: "
t = gets
t.gsub!(/\s+/, '')
t.chomp!
r = ''
puts
print "Введите ключ: "
kw = gets.to_i
t.each_char.with_index do |val,kk|
i = alpha.index(val)
i -= kw
if i < 0
i = i + 25
#puts "new I + 25 = #{i}"
end
r << alpha[i]
end
puts "Исходное сообщение: #{r}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment