Skip to content

Instantly share code, notes, and snippets.

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