Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- f = open('Соответствие.txt', 'r')
- d1 = dict()
- d2 = dict()
- while True:
- line = f.readline()
- line = line.strip()
- if not(line):
- break
- letter, number = line.split(' - ')
- d1[letter] = number
- d2[number] = letter
- f.close()
- def F(block, key):
- s = ''
- for i in range(32):
- s += str(int(block[i]) ^ int(key[i]))
- f = open('Значения S-блоков.txt', 'r')
- lines = [line.strip() for line in f.readlines()]
- lines = [line.split() for line in lines]
- i = 0
- j = 0
- output = ''
- while j < len(s):
- x = lines[i][int(s[j:j+4], 2)]
- b = bin(int(x))[2:]
- while len(b) < 4:
- b = '0' + b
- output += b
- i += 1
- j += 4
- return output[11:] + output[:11]
- l = [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 0]
- def magma(mode, block, encryption_key):
- left, right = block[:32], block[32:]
- round_of_encryption = 1
- while round_of_encryption <= 32:
- if mode == '1':
- x = encryption_key[l[round_of_encryption-1]]
- elif mode == '2':
- x = encryption_key[l[len(l)-round_of_encryption]]
- key = bin(int(x))[2:]
- while len(key) < 32:
- key = '0' + key
- f = F(right, key)
- new_left = ''
- for i in range(32):
- new_left += str(int(left[i]) ^ int(f[i]))
- if round_of_encryption <= 31:
- left, right = right, new_left
- round_of_encryption += 1
- return left + right
- while True:
- print('1. Зашифровать открытый текст')
- print('2. Расшифровать шифротекст')
- mode = input('>>> ')
- if mode == '1':
- input_text = input('Открытый текст: ')
- encryption_key = input('Ключ шифрования в числовом формате: ')
- elif mode == '2':
- input_text = input('Закрытый текст: ')
- encryption_key = input('Ключ шифрования в числовом формате: ')
- else:
- continue
- b = ''
- for letter in input_text:
- b += d1[letter]
- i = 0
- while len(b)%64 != 0:
- letter = input_text[i%len(input_text)]
- b += d1[letter]
- i += 1
- i = 0
- output_text = ''
- while i < len(b):
- input_block = b[i:i+64]
- output_block = magma(mode, input_block, encryption_key)
- j = 0
- k = 0
- while k < len(output_block):
- output_text += d2[output_block[k:k+8]]
- j += 1
- k += 8
- i += 64
- if mode == '1':
- f = open('Шифр-текст.txt', 'w')
- elif mode == '2':
- f = open('Расшифрованный текст.txt', 'w')
- f.write(output_text)
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement