-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscript.py
69 lines (52 loc) · 2.02 KB
/
script.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
from helpers import parse_text, byteslist_fromhex
from manytimepad import find_key, decode
import argparse
import textwrap
if __name__ == "__main__":
# -------------------
# PARSE ARGUMENT
# -------------------
parser = argparse.ArgumentParser(description="Decrypts a target ciphertext, \
given a bunch on intercepted ciphertexts encrypted with the same unknown key. \
Ciphertexts may or may not have random errors.")
parser.add_argument('filepath', help="Path to ciphertexts. Ciphers must be hex-encoded and \
separated by a newline, the first cipher is the target")
parser.add_argument("--i", dest="iterations", type=int, default=42,
help="Number of iterations for frequency analysis prior to generating key" )
parser.add_argument('-v', dest="verbose", action='store_true', default=False,
help='Display more information')
args = parser.parse_args()
# --------------------
# FIND KEY AND DECRYPT
# --------------------
cipherstrings = parse_text(args.filepath)
ciphers = byteslist_fromhex(cipherstrings)
print("Finding key... ", end="")
key = find_key(ciphers, iterations=args.iterations)
print("Decrypting target ciphertext...", end="")
message = decode(ciphers[0], key)
print(" done.")
print("--")
print(message)
print("--")
# --------------------
# DISPLAY MORE INFORMATION
# --------------------
n = 40
if args.verbose is True:
print()
print("filepath: ", args.filepath)
print("iterations: ", args.iterations)
print()
print("key:")
ks = textwrap.wrap(key.hex(), width=2*n)
for k in ks:
print(" ", k)
for i, cipherstring in enumerate(cipherstrings):
message = decode(ciphers[i], key)
ms = textwrap.wrap(message, width=n)
cs = textwrap.wrap(cipherstring, width=n)
print()
print("#", i)
for c, m in zip(cs, ms):
print(" ", c, " ", m)