Advertisement
Shiyan12

Untitled

May 4th, 2021
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. f = open('Соответствие.txt', 'r')
  2. d1 = dict()
  3. d2 = dict()
  4. while True:
  5.     line = f.readline()
  6.     line = line.strip()
  7.     if not(line):
  8.         break
  9.     letter, number = line.split(' - ')
  10.     d1[letter] = number
  11.     d2[number] = letter
  12. f.close()
  13.  
  14. def F(block, key):
  15.     s = ''
  16.     for i in range(32):
  17.         s += str(int(block[i]) ^ int(key[i]))
  18.     f = open('Значения S-блоков.txt', 'r')
  19.     lines = [line.strip() for line in f.readlines()]
  20.     lines = [line.split() for line in lines]
  21.     i = 0
  22.     j = 0
  23.     output = ''
  24.     while j < len(s):
  25.         x = lines[i][int(s[j:j+4], 2)]
  26.         b = bin(int(x))[2:]
  27.         while len(b) < 4:
  28.             b = '0' + b
  29.         output += b
  30.         i += 1
  31.         j += 4
  32.     return output[11:] + output[:11]
  33.  
  34. 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]
  35.  
  36. def magma(mode, block, encryption_key):
  37.     left, right = block[:32], block[32:]
  38.     round_of_encryption = 1
  39.     while round_of_encryption <= 32:
  40.         if mode == '1':
  41.             x = encryption_key[l[round_of_encryption-1]]
  42.         elif mode == '2':
  43.             x = encryption_key[l[len(l)-round_of_encryption]]
  44.         key = bin(int(x))[2:]
  45.         while len(key) < 32:
  46.             key = '0' + key
  47.         f = F(right, key)
  48.         new_left = ''
  49.         for i in range(32):
  50.             new_left += str(int(left[i]) ^ int(f[i]))
  51.         if round_of_encryption <= 31:
  52.             left, right = right, new_left
  53.         round_of_encryption += 1
  54.     return left + right
  55.        
  56. while True:
  57.     print('1. Зашифровать открытый текст')
  58.     print('2. Расшифровать шифротекст')
  59.  
  60.     mode = input('>>> ')
  61.     if mode == '1':
  62.         input_text = input('Открытый текст: ')
  63.         encryption_key = input('Ключ шифрования в числовом формате: ')
  64.     elif mode == '2':
  65.         input_text = input('Закрытый текст: ')
  66.         encryption_key = input('Ключ шифрования в числовом формате: ')
  67.     else:
  68.         continue
  69.    
  70.     b = ''
  71.     for letter in input_text:
  72.         b += d1[letter]
  73.     i = 0
  74.     while len(b)%64 != 0:
  75.         letter = input_text[i%len(input_text)]
  76.         b += d1[letter]
  77.         i += 1
  78.     i = 0
  79.     output_text = ''
  80.     while i < len(b):
  81.         input_block = b[i:i+64]
  82.         output_block = magma(mode, input_block, encryption_key)
  83.         j = 0
  84.         k = 0
  85.         while k < len(output_block):
  86.             output_text += d2[output_block[k:k+8]]
  87.             j += 1
  88.             k += 8
  89.         i += 64
  90.  
  91.     if mode == '1':
  92.         f = open('Шифр-текст.txt', 'w')
  93.     elif mode == '2':
  94.         f = open('Расшифрованный текст.txt', 'w')
  95.     f.write(output_text)
  96.     f.close()
  97.        
  98.  
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement