kod (960709)
Текст из файла
module Algorithms
class Functions
def self.do_xor(first, second)
show_bit_representation(first)
show_bit_representation(second)
len = first.length
(0..len - 1).each do |_|
first_char = first[_].ord
second_char = second[_].ord
first_char ^= second_char
first[_] = first_char.chr
end
first
end
def self.affect_with_key_shift(key)
shifted_key = key
ind = 1
Algorithms::AlgorithmParameters.get_key_shift.each do |_|
bit = get_bit_by_number(_, key)
shifted_key = set_bit_by_number(ind, bit, shifted_key)
ind += 1
end
shifted_key
end
def self.affect_with_key_permutation(block)
permutated_block = "\0" * 6
ind = 1
Algorithms::AlgorithmParameters.get_key_permutation.each do |_|
bit = get_bit_by_number(_, block)
permutated_block = set_bit_by_number(ind, bit, permutated_block)
ind += 1
end
permutated_block
end
def self.affect_with_extended_key_permutation(block)
permutated_block = "\0" * 7
ind = 1
Algorithms::AlgorithmParameters.get_extended_key_permutation.each do |_|
bit = Algorithms::Functions.get_bit_by_number(_, block)
permutated_block = set_bit_by_number(ind, bit, permutated_block)
ind += 1
end
permutated_block
end
def self.show_bit_representation(block)
block.chars.each do |char|
printf("%08d\n", char.ord.to_s(2))
end
end
def self.affect_with_first_permutation(block)
permutated_block = block
ind = 1
Algorithms::AlgorithmParameters.get_start_permutation.each do |_|
bit = Algorithms::Functions.get_bit_by_number(_, block)
permutated_block = Algorithms::Functions.set_bit_by_number(ind, bit, permutated_block)
ind += 1
end
permutated_block
end
def self.get_round_key(round_number)
if round_number == 1
key = Algorithms::AlgorithmParameters.get_key
show_bit_representation(key)
key = affect_with_extended_key_permutation(key)
show_bit_representation(key)
key_left = key[0..3]
char = key_left[3].ord & ~15
key_left[3] = char.chr
key_right = "\0" * 4
(3..5).each do |_|
char = ((key[_].ord & 15) << 4) | ((key[_ + 1].ord & ~15) >> 4)
key_right[_ - 3] = char.chr
end
char = key_right[3].ord & ~15
key_right[3] = char.chr
else
key_left = @@key_left
key_right = @@key_right
end
show_bit_representation(key_left)
show_bit_representation(key_right)
key_left = shift_key(key_left, round_number)
show_bit_representation(key_left)
key_right = shift_key(key_right, round_number)
show_bit_representation(key_right)
@@key_left = key_left
@@key_right = key_right
key = "\0" * 7
(0..3).each do |_|
key[_] = key_left[_]
end
char = key[3].ord
char |= (key_right[0].ord & ~15) >> 4
key[3] = char.chr
(0..2).each do |_|
char = ((key_right[_].ord & 15) << 4) | ((key_right[_ + 1].ord & ~15) >> 4)
key[4 + _] = char.chr
end
show_bit_representation(key)
key = affect_with_key_permutation(key)
show_bit_representation(key)
key
end
def self.shift_key(key, round_number)
Algorithms::AlgorithmParameters.get_shift_count[round_number - 1].times do
key = affect_with_key_shift(key)
end
key
end
def self.create_round_keys
keys = Array.new
16.times do |_|
key = get_round_key(_ + 1)
keys.push(key)
end
Algorithms::AlgorithmParameters.set_round_keys(keys)
end
def self.get_bit_by_number(number, block)
char = block[(number - 1) / 8].ord
char & (1 << (7 - (number - 1) % 8)) != 0
end
def self.set_bit_by_number(number, bit, block)
unfreezed_copy = block.dup
char = unfreezed_copy[(number - 1) / 8].ord
if !bit
char &= ~(1 << (7 - (number - 1) % 8))
else
char |= (1 << (7 - (number - 1) % 8))
end
unfreezed_copy[(number - 1) / 8] = char.chr
unfreezed_copy
end
def self.feistel_function(block, key)
show_bit_representation(block)
block = extend(block)
show_bit_representation(block)
block = do_xor(block, key)
show_bit_representation(block)
subblocks = ["\0"] * 8
char = block[0].ord & ~3
subblocks[0] = char.chr
char = ((block[0].ord & 3) << 6) | ((block[1].ord & ~15) >> 2)
subblocks[1] = char.chr
char = ((block[1].ord & 15) << 4) | ((block[2].ord & ~63) >> 4)
subblocks[2] = char.chr
char = ((block[2].ord & 63) << 2)
subblocks[3] = char.chr
char = block[3].ord & ~3
subblocks[4] = char.chr
char = ((block[3].ord & 3) << 6) | ((block[4].ord & ~15) >> 2)
subblocks[5] = char.chr
char = ((block[4].ord & 15) << 4) | ((block[5].ord & ~63) >> 4)
subblocks[6] = char.chr
char = ((block[5].ord & 63) << 2)
subblocks[7] = char.chr
(0..7).each do |_|
show_bit_representation(subblocks[_])
end
(0..7).each do |_|
subblocks[_] = replace_subblock_with_s_box(_, subblocks[_])
show_bit_representation(subblocks[_])
end
block = "\0" * 4
(0..3).each do |_|
char = subblocks[2 * _].ord << 4 | subblocks[2 * _ + 1].ord
block[_] = char.chr
end
show_bit_representation(block)
block = affect_with_feitsel_permutation(block)
show_bit_representation(block)
block
end
def self.extend(block)
extended_block = "\0" * 6
bit = get_bit_by_number(32, block)
extended_block = set_bit_by_number(1, bit, extended_block)
ind = 2
(1..5).each do |_|
bit = get_bit_by_number(_, block)
extended_block = set_bit_by_number(ind, bit, extended_block)
ind += 1
end
bit_number = 4
while ind <= 42 do
6.times do
bit = get_bit_by_number(bit_number, block)
extended_block = set_bit_by_number(ind, bit, extended_block)
bit_number += 1
ind += 1
end
bit_number -= 2
end
(43..47).each do |_|
bit = get_bit_by_number(bit_number, block)
extended_block = set_bit_by_number(_, bit, extended_block)
bit_number += 1
end
# last bit is 1 bit from block
bit = get_bit_by_number(1, block)
extended_block = set_bit_by_number(48, bit, extended_block)
extended_block
end
def self.replace_subblock_with_s_box(number, subblock)
row = "\0"
bit = get_bit_by_number(1, subblock)
row = set_bit_by_number(7, bit, row)
bit = get_bit_by_number(6, subblock)
row = set_bit_by_number(8, bit, row)
column = (subblock.ord & 120) >> 3
Algorithms::AlgorithmParameters.get_s_boxes[number][row.ord][column].chr
end
def self.affect_with_feitsel_permutation(block)
permutated_block = block
ind = 1
Algorithms::AlgorithmParameters.get_feitsel_permutation.each do |_|
bit = get_bit_by_number(_, block)
permutated_block = set_bit_by_number(ind, bit, permutated_block)
ind += 1
end
permutated_block
end
def self.affect_with_inverse_permutation(block)
permutated_block = block
ind = 1
Algorithms::AlgorithmParameters.get_inverse_permutation.each do |_|
bit = get_bit_by_number(_, block)
permutated_block = set_bit_by_number(ind, bit, permutated_block)
ind += 1
end
permutated_block
end
end
end
module Algorithm
class Encoder
def self.encode(block)
if block.nil?
else
Algorithm::Functions.show_bit_representation(block)
block = Algorithm::Functions.affect_with_first_permutation(block)
Algorithm::Functions.show_bit_representation(block)
Algorithm::Functions.show_bit_representation(block[0..3])
Algorithm::Functions.show_bit_representation(block[4..-1])
16.times do |_|
right = do_round_with_right_subblock(_ + 1, block[0..3], block[4..-1])
(0..3).each do |__|
block[__] = block[4 + __]
end
(4..7).each do |__|
block[__] = right[__ - 4]
end
Algorithm::Functions.show_bit_representation(block[0..3])
Algorithm::Functions.show_bit_representation(block[4..-1])
end
Algorithm::Functions.show_bit_representation(block)
block = Algorithm::Functions.affect_with_inverse_permutation(block)
Algorithm::AlgorithmParameters.set_block_to_decode(block)
Algorithm::Functions.show_bit_representation(block)
end
end
def self.do_round_with_right_subblock(round_number, left, right)
key = Algorithm::AlgorithmParameters.get_round_keys[round_number - 1]
function = Algorithm::Functions.feistel_function(right, key)
right = Algorithm::Functions.do_xor(left, function)
Algorithm::Functions.show_bit_representation(right)
end
end
end
module Algorithms
class Parameters
def self.get_block_to_decode
@@block_to_decode
end
def self.set_block_to_decode(block)
@@block_to_decode = block
end
def self.get_block_to_encode
"SecretAA"
end
def self.get_round_keys
@@round_keys
end
def self.set_round_keys(keys)
@@round_keys = keys
end
def self.get_key
'MyKeyKey'
end
def self.get_start_permutation
[58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7]
end
def self.get_extended_key_permutation
[57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
Характеристики
Тип файла документ
Документы такого типа открываются такими программами, как Microsoft Office Word на компьютерах Windows, Apple Pages на компьютерах Mac, Open Office - бесплатная альтернатива на различных платформах, в том числе Linux. Наиболее простым и современным решением будут Google документы, так как открываются онлайн без скачивания прямо в браузере на любой платформе. Существуют российские качественные аналоги, например от Яндекса.
Будьте внимательны на мобильных устройствах, так как там используются упрощённый функционал даже в официальном приложении от Microsoft, поэтому для просмотра скачивайте PDF-версию. А если нужно редактировать файл, то используйте оригинальный файл.
Файлы такого типа обычно разбиты на страницы, а текст может быть форматированным (жирный, курсив, выбор шрифта, таблицы и т.п.), а также в него можно добавлять изображения. Формат идеально подходит для рефератов, докладов и РПЗ курсовых проектов, которые необходимо распечатать. Кстати перед печатью также сохраняйте файл в PDF, так как принтер может начудить со шрифтами.