Created
May 1, 2014 10:29
-
-
Save spellancer/6ecc11e19b2aed1f896e to your computer and use it in GitHub Desktop.
Cipher Caesar
This file contains hidden or 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
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