-
Notifications
You must be signed in to change notification settings - Fork 0
/
p059.py
58 lines (42 loc) · 1.34 KB
/
p059.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
import itertools
def attack1(filename, pass_len):
with open(filename) as fopen:
msg = fopen.readline().strip('\n')
msg = msg.split(',')
array = [[int(msg[i]) for i in range(len(msg)) if i % pass_len == j] for j in range(pass_len)]
for i in range(ord('a'), ord('z') + 1):
for j in range(ord('a'), ord('z') + 1):
for k in range(ord('a'), ord('z') + 1):
msg = ""
for x in range(len(array[0])):
msg += chr(array[0][x] ^ i)
if x < len(array[1]):
msg += chr(array[1][x] ^ j)
if x < len(array[2]):
msg += chr(array[2][x] ^ k)
if " a " not in msg:
continue
if " is " not in msg:
continue
return msg
def attack2(filename, pass_len):
with open(filename) as fopen:
msg = fopen.readline().strip('\n')
msg = msg.split(',')
freq = {}
for i in range(ord('a'), ord('z') + 1):
freq[i] = 0
for j in range(len(msg)):
if int(msg[j]) ^ i == ord(' '):
freq[i] += 1
vals = sorted(freq, key = lambda x: freq[x], reverse=True)
keys = [vals[i] for i in range(pass_len)]
for i in itertools.permutations(keys):
s = ""
for j in range(len(msg)):
s += chr(int(msg[j]) ^ i[j % len(i)])
if " is " not in s:
continue
if " the " not in s:
continue
return s