-
Notifications
You must be signed in to change notification settings - Fork 0
/
task1.py
80 lines (66 loc) · 2.37 KB
/
task1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from utils import *
import cyphers.permutation as perm
def get_seqs(enc, length) -> dict:
seqs = {}
for idx in range(len(enc) - length):
seq = enc[idx: idx + length]
if seq not in seqs:
seqs[seq] = (idx, [])
else:
seqs[seq][1].append(idx - seqs[seq][0])
return {key: gcd(seqs[key][1]) for key in seqs if len(seqs[key][1])}
def _kasiski(enc, length):
seqs = get_seqs(enc, length)
possible = list(set(seqs.values()))
possible.sort()
return possible
def kasiski(enc, start, end):
results = [_kasiski(enc, length) for length in range(start, end + 1)]
report = []
for idx, possible in zip(range(start, end + 1), results):
res_str = 'Длина: {:>2} | Возможные длины ключей: {}'.format(idx, possible)
report.append(res_str)
print(res_str)
write('kasiski.txt', '\n'.join(report))
def _brute(length, container: list, idx_cur):
free = [idx for idx in range(length) if idx not in container + [idx_cur, 0]]
for idx_next in free:
container[idx_cur] = idx_next
if container.count(None) > 1:
yield from _brute(length, container, idx_next)
else:
container[idx_next] = 0
if not perm.is_monocycle_key(container):
print('ОШИБКА: неверно сгенерирован ключ длины {}: {}'.format(length, perm.str_key(container)))
yield container
container[idx_next] = None
container[idx_cur] = None
def brute(enc, length):
container = [None] * length
f = get_file_write('bruted.txt')
for key in _brute(length, container, 0):
print(perm.str_key(key) + '\r', end='')
dec = perm.decrypt(enc, key)
f.write(perm.str_key(key) + '\n')
f.write(dec + '\n\n')
f.close()
def main():
if op == 'gen':
perm.exec_gen_mono_key()
elif op == 'enc':
perm.exec_encrypt()
elif op == 'dec':
perm.exec_decrypt()
elif op == 'kas':
enc = read(argv[2]).lower()
print('Количество символов: {}'.format(len(enc)))
start, end = int(argv[3]), int(argv[4])
kasiski(enc, start, end)
elif op == 'brute':
enc = read(argv[2])
length = int(argv[3])
brute(enc, length)
else:
print_wrong_op()
if __name__ == '__main__':
main()