Skip to content

Instantly share code, notes, and snippets.

@spellancer
Last active August 29, 2015 14:00
Show Gist options
  • Save spellancer/11056230 to your computer and use it in GitHub Desktop.
Save spellancer/11056230 to your computer and use it in GitHub Desktop.
# Нанян Саркис АК5-81
# Лабораторная работа № 4. Защита информации
km = false
def kword(sz)
kk = false
while !kk
keyword = gets
keyword.chomp!
if keyword.length == sz
kk = true
else
puts 'Ключевое слово неверной длины! Попробуйте еще раз!'
end
end
a =[]
keyword.each_char {|i| a << i.ord}
b = a.sort
c = (0..b.length-1).to_a
h = Hash[c.zip b]
h.each {|k,v| a[a.index(v)] = k}
return a
end
def tprint(table)
table.each do |i|
puts i.each { |j| j }.join(" ")
end
end
while !km
k = false
while !k
print 'Введите 1 для шифрования или 2 для расшифровки: '
r = gets.to_i
if r==1 || r==2
k =true
elsif r ==0
km = true
end
k = false
puts 'Выберите метод шифровки:'
puts '1 - шифр одиночной перестановки'
puts '2 - шифр двоичной перестановки'
puts '3 - магический квадрат'
while !k
print "Введите 1,2 или 3: "
m = gets.to_i
if m==1 || m==2 || m==3
k =true
end
end
if k && r==1
puts 'Введите исходный текст:'
text = gets
end
#=begin
if m ==1 && r==1
text.gsub!(/\s+/, '')
while text.length % 5 !=0
text << '-'
end
text = text.scan(/.{5}/)
table = []
text.map {|i| table.push(i.split('')) }
puts "Введите ключевое слово (длиной #{table.size} символа):"
a = kword(table.size)
table2 = []
h = Hash[a.zip table]
print h
puts
for i in 0..table.size
table2.push(h[i])
end
table2 = table2.compact
puts
tprint (table2)
puts
puts "Исходный текст: "
table = table.transpose
tprint(table)
puts "--------------"
table2 = table2.transpose
puts "Зашифрованный текст:"
puts "Ключ шифрования #{a}"
tprint(table2)
# tprint(table2)
#tprint(table)
puts "\n"
res = ''
#table.map{|i| res << i.to_s }
res = table2.join("")
puts "Зашифрованное сообщение: #{res.upcase}"
elsif m==1 && r==2
print 'Введите зашифрованный текст: '
msg = gets
puts
print 'Введите длину ключа: '
sz = gets.to_i
msg = msg.scan(/.{#{sz}}/)
table = []
msg.map {|i| table.push(i.split('')) }
puts
print 'Введите ключ: '
a = kword(sz)
table = table.transpose
tprint(table)
puts
table2 = []
a.map {|i| table2.push(table[i])}
table2 = table2.transpose
table2 = table2.compact
tprint(table2)
table2 = table2.transpose
res = table2.join("")
res = res.tr('-','')
puts "\nЗашифрованное исходное сообщение: #{res}"
elsif m == 2 && r == 1
text.gsub!(/\s+/, '')
while text.length % 5 !=0
text << '-'
end
text = text.scan(/.{5}/)
table = []
text.map {|i| table.push(i.split('')) }
puts "Введите первое ключевое слово (длиной #{table.size} символа):"
a = kword(table.size)
table2 = []
table3 = []
h = Hash[a.zip table]
print h
puts "\n----------"
for i in 0..table.size
table2.push(h[i])
end
table2 = table2.compact
table2 = table2.transpose
puts "\nВведите второе ключевое слово (длиной #{table2.size} символа):"
a2 = kword(table2.size)
h2 = Hash[a2.zip table2]
print h2
puts "----------"
puts "table2 зашифрованный"
table2 = table2.transpose
tprint (table2)
puts
for i in 0..table2.size
table3.push(h2[i])
end
table3 = table3.compact
puts "TABLE3 = #{table3}"
table3 = table3.transpose
tprint (table3)
puts "Ключ шифрования 1 #{a}"
puts "Ключ шифрования 2 #{a2}"
puts "Исходный текст: "
tprint(table)
puts "--------------"
#puts "Ключ шифрования #{a}"
puts "Зашифрованный текст:"
tprint(table3)
puts "\n"
res = ''
res = table3.join("")
puts "Зашифрованное сообщение: #{res.upcase}"
elsif m == 2 && r == 2
print 'Введите зашифрованный текст: '
msg = gets
puts
print 'Введите длину 1 ключа: '
sz = gets.to_i
#msg = msg.scan(/.{#{sz}}/)
msg = msg.scan(/.{5}/)
table = []
msg.map {|i| table.push(i.split('')) }
puts
print 'Введите ключ: '
a = kword(sz)
tprint(table)
puts
table2 = []
a.map {|i| table2.push(table[i])}
table3 = []
tprint(table2)
print 'Введите длину 2 ключа: '
sz2 = gets.to_i
print 'Введите ключ: '
a2 = kword(sz2)
table2 = table2.transpose
table2 = table2.compact
puts "a2 - #{a2}, table2 - #{table2}"
a2.map {|i| table3.push(table2[i])}
table3 = table3.compact
table3 = table3.transpose
puts "Зашифрованная таблица: "
tprint(table3)
puts
res = table3.join("")
res = res.tr('-','')
puts "\nЗашифрованное исходное сообщение: #{res}"
elsif m == 3 && r == 1
text.gsub!(/\s+/, '')
if text.length < 16
kc = false
else
kc = true
end
while !kc
puts 'Введите исходный текст длиной 16 символов:'
text = gets
text.gsub!(/\s+/, '')
#print text
if text.length >=16
kc = true
end
end
#puts "SIZE: #{text.length}"
text = text[0..15]
puts "\nИсходный текст: #{text}."
magic = [[16,3,2,13],[5,10,11,8],[9,6,7,12],[4,15,14,1]]
puts 'Магический квадрат (ключ): '
tprint (magic)
puts
table = []
res = ''
magic.each do |i|
i.each do |j|
a = text[j-1]
res << a
end
end
puts "Зашифрованный текст: #{res}"
res = res.scan(/.{4}/)
res.map {|i| table.push(i.split('')) }
tprint (table)
elsif m == 3 && r == 2
puts 'Введите зашифрованное сообщение: '
msg = gets
table = []
msg = msg.scan(/.{4}/)
msg.map {|i| table.push(i.split('')) }
ht = Hash.new {|h,k| h[k]=""}
magic = [[16,3,2,13],[5,10,11,8],[9,6,7,12],[4,15,14,1]]
for i in 0..3 do
for j in 0..3 do
k = magic[i][j]
val = table[i][j]
ht[k] << val
end
end
print ht
puts
res = ""
for i in 1..16
#print ht[i]
res << ht[i].to_s
end
#res = res.join("")
print "Исходное расшифрованное сообщение: "
puts res
end
end
puts "Желаете продолжить работу?"
print '0/1 : '
e = gets.to_i
if e ==0
km = true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment