This repository has been archived by the owner on Dec 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
des.py
executable file
·144 lines (110 loc) · 4.23 KB
/
des.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
from Crypto.Cipher import DES
import logging
from six.moves import input # python2/3 compatibility
import functools
logger = logging.getLogger()
def encrypt(key, plain_text):
"""Encrypt using single-round-DES"""
cipher = DES.new(key, DES.MODE_ECB)
return cipher.encrypt(plain_text)
def decrypt(key, cipher_text):
"""Decrypt using single-round DES"""
cipher = DES.new(key, DES.MODE_ECB)
return cipher.decrypt(cipher_text)
def nth_key(index):
"""Gets the nth key with respect to parity bits"""
keystring = []
for i in range(8):
key_byte = index & 127 # 127 == int('1111111', 2)
hamming_weight = bin(key_byte).count('1')
if hamming_weight % 2 == 0:
keystring.append(((key_byte << 1) + 1))
else:
keystring.append((key_byte << 1))
index >>= 7
return bytes(keystring[::-1]) # reverse order
def _precompute(plain_text, key):
"""helper function used for multiprocessing"""
return (encrypt(key, plain_text), key)
def meet_in_the_middle(nbits, text_pairs, pool=None):
"""Implements a meet-in-the-middle attack on DES.
Arguments:
nbits -- the number of bits to attack
text_pairs -- a list of (plain text, cipher text)-tuples
pool -- (optional) threadpool
"""
plain_text, cipher_text = text_pairs[0]
# generator for the key candidates
key_generator = (nth_key(i) for i in range(0, 2**nbits))
logger.info("Pre-computing")
if pool is None:
table = dict([_precompute(plain_text, key) for key in key_generator])
else:
table = dict(pool.map(functools.partial(_precompute, plain_text),
key_generator, 1000))
logger.info("precomputed {} items".format(len(table)))
logger.info("Cracking cipher_text")
# TODO parallelising this bit is a bit harder because you might
# want to return earlier.
for key in key_generator:
candidate = decrypt(key, cipher_text)
if candidate in table:
logger.info("Found key: ({}, {})".format(table[candidate], key))
for plain, cipher in text_pairs[1:]:
if not decrypt(key, cipher) == encrypt(key, plain):
continue # incorrect candidate
return (table[candidate], key)
def run(argv=None):
"""Run the program
Usage: des.py [options] <bits>
It will ask you for further inputs
Options::
-h,--help Show this help
-v,--verbose Increase verbosity
--test Generate test strings
"""
import sys
import docopt
import textwrap
from binascii import unhexlify, hexlify
from multiprocessing import Pool
argv = sys.argv[1:]
args = docopt.docopt(textwrap.dedent(run.__doc__), argv)
nbits = int(args['<bits>'])
# set up logging
level = logging.WARN
if args['--verbose']:
level = logging.INFO
logging.basicConfig(level=level)
if args['--test']:
from random import randint
key1 = nth_key(randint(0, 2**nbits))
key2 = nth_key(randint(0, 2**nbits))
plain_text = bytes((randint(0, 255) for i in range(8)))
cipher_text = encrypt(key2, encrypt(key1, plain_text))
print("key: ({}, {})".format(hexlify(key1).decode('utf-8'),
hexlify(key2).decode('utf-8')))
print("plain text: {}".format(hexlify(plain_text).decode('utf-8')))
print("cipher text: {}".format(hexlify(cipher_text).decode('utf-8')))
return
input_more = True
pairs = []
while input_more:
plain_text = unhexlify(
input("Please input the plain text, hex encoded: "
).strip().encode('utf-8'))
cipher_text = unhexlify(
input("Please input the cipher text, hex encoded: "
).strip().encode('utf-8'))
pairs.append((plain_text, cipher_text))
if 'y' not in input("Do you want to supply more texts? [y/N]: "):
input_more = False
with Pool() as p:
keys = meet_in_the_middle(nbits, pairs, pool=p)
if keys:
print("Found keys: ({}, {})".format(*keys))
else:
print("Did not find keys!")
if __name__ == '__main__':
run()