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
| import Crypto.Util.strxor as xo import codecs import numpy as np
def ischr(x): if ord('a') <= x <= ord('z'): return True if ord('A') <= x <= ord('Z'): return True return False
def infer(p_index, p_pos): if msg[p_index, p_pos] != 0: return msg[p_index, p_pos] = ord(' ') for x in range(len(c)): if x != p_index: msg[x][p_pos] = xo.strxor(c[x], c[p_index])[p_pos] ^ ord(' ')
dat = []
def get_space(): for t_index, x in enumerate(c): res = [xo.strxor(x, y) for y in c if x != y] f = lambda t_pos: len(list(filter(ischr, [s[t_pos] for s in res]))) cnt = [f(t_pos) for t_pos in range(len(x))] for t_pos in range(len(x)): dat.append((f(t_pos), t_index, t_pos))
c = [codecs.decode(x.strip().encode(), 'hex') for x in open('t3mpt.txt', 'r').readlines()]
msg = np.zeros([len(c), len(c[0])], dtype=int)
get_space()
dat = sorted(dat)[::-1] for w, index, pos in dat: infer(index, pos)
def know(p_index, p_pos, ch): msg[p_index, p_pos] = ord(ch) for x in range(len(c)): if x != p_index: msg[x][p_pos] = xo.strxor(c[x], c[p_index])[p_pos] ^ ord(ch)
know(0, 1, 'o') know(0, 4, 'y') know(0, 12, ' ') know(0, 13, 't') know(0, 17, 's') know(0, 19, ' ') know(1, 25, 'e')
print('\n'.join([''.join([chr(c) for c in x]) for x in msg]))
key = xo.strxor(c[0], ''.join([chr(c) for c in msg[0]]).encode()) print(key)
|