Advertisement
nq1s788

лаба

Dec 1st, 2023
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. from math import ceil
  2.  
  3.  
  4. def to_encrypt_6(s, key): #прямая изгородь
  5.     answer = [''] * key
  6.     dir = 1
  7.     cur = 0
  8.     for e in s:
  9.         answer[cur] += e
  10.         cur += dir
  11.         if cur == 0 or cur == key - 1:
  12.             dir *= -1
  13.     return ''.join(answer)
  14.  
  15.  
  16. def to_encrypt_7(s, key): #обратная изгородь
  17.     answer = [''] * key
  18.     dir = -1
  19.     cur = key - 1
  20.     for e in s:
  21.         answer[cur] += e
  22.         cur += dir
  23.         if cur == 0 or cur == key - 1:
  24.             dir *= -1
  25.     return ''.join(answer)
  26.  
  27.  
  28. def to_encrypt_8(s, key): #перестановочная прямая
  29.     key = sorted([(key[i], i) for i in range(len(key))])
  30.     answ = ''
  31.     mx = []
  32.     for i in range(0, len(s), len(key)):
  33.         mx.append(s[i:i + len(key)])
  34.     for e in mx:
  35.         for ind in key:
  36.             if ind[1] < len(e):
  37.                 answ += e[ind[1]]
  38.     return answ
  39.  
  40.  
  41. def to_encrypt_9(s, key): #перестановочная обратная
  42.     key = sorted([(key[i], i) for i in range(len(key))])
  43.     mx = [''] * ceil(len(s) / len(key))
  44.     cur = len(mx) - 1
  45.     for i in range(0, len(s), len(key)):
  46.         mx[cur] = s[i:i + len(key)][::-1]
  47.         if len(mx[cur]) < len(key):
  48.             mx[cur] = '0' * (len(key) - len(mx[cur])) + mx[cur]
  49.         cur -= 1
  50.     answ = ''
  51.     for e in mx:
  52.         for ind in key:
  53.             if e[ind[1]] != '0':
  54.                 answ += e[ind[1]]
  55.     return answ
  56.  
  57.  
  58. def to_encrypt_10(s, key): #redefence прямой
  59.     answer = [''] * len(key)
  60.     dir = 1
  61.     cur = 0
  62.     for e in s:
  63.         answer[cur] += e
  64.         cur += dir
  65.         if cur == 0 or cur == len(key) - 1:
  66.             dir *= -1
  67.     key = sorted([(key[i], i) for i in range(len(key))])
  68.     redefence = ''
  69.     for e in key:
  70.         redefence += answer[e[1]]
  71.     return redefence
  72.  
  73.  
  74. def to_encrypt_11(s, key): #redefence обратный
  75.     answer = [''] * len(key)
  76.     dir = -1
  77.     cur = len(key) - 1
  78.     for e in s:
  79.         answer[cur] += e
  80.         cur += dir
  81.         if cur == 0 or cur == len(key) - 1:
  82.             dir *= -1
  83.     key = sorted([(key[i], i) for i in range(len(key))])
  84.     redefence = ''
  85.     for e in key:
  86.         redefence += answer[e[1]]
  87.     return redefence
  88.  
  89.  
  90. print(to_encrypt_6('изгородь', 3))
  91. print(to_encrypt_7('изгородь', 3))
  92. print(to_encrypt_8('перестановка', 'слово'))
  93. print(to_encrypt_9('перестановка', 'слово'))
  94. print(to_encrypt_10('изгородь', 'сон'))
  95. print(to_encrypt_11('изгородь', 'сон'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement