-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpartial_binary.py
73 lines (61 loc) · 2.04 KB
/
partial_binary.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
# Probable plaintext decryption of XOR-encrypted files with a key of 512 bytes (for E-Safenet)
# Copyright (C) 2014 Jan Laan, Cedric Van Bockhaven
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file LICENSE. if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import itertools
from os.path import commonprefix
# Recipe for pairwise iteration
def pairwise(iterable):
a, b = itertools.tee(iterable)
next(b, None)
return itertools.izip(a, b)
def find_binary_key(text):
r = text[512:]
chunks = [r[x:x+512] for x in range(0,len(r),512)]
store = [None]*512
for i in range(512):
store[i] = {}
for offset in range(512):
ochunks = [x[offset:] for x in chunks]
ochunks.sort()
for s1, s2 in pairwise(ochunks):
pfx = commonprefix([s1,s2])
if len(pfx)>16:
skip = False
for ofs in store:
for stored in ofs.keys():
if pfx in stored:
skip = True
break
if not skip:
store[offset][pfx] = store[offset].get(pfx, 0) + 1
for ofs in range(len(store)):
for stored in store[ofs].keys():
if len(stored) < len(pfx) and stored in pfx:
del(store[ofs][stored])
key = ['\0']*512
i = 0
order = [None]*512
for ofs in range(len(store)):
for k in store[ofs].keys():
if order[len(k)-1] == None:
order[len(k)-1] = []
order[len(k)-1].append([k, ofs])
for o in order:
if o != None:
for k, offset in o:
key[offset:offset+len(k)] = k
arr = [ord(l) for l in key]
return arr