-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleDES.py
44 lines (36 loc) · 1.32 KB
/
DoubleDES.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
from utils.pyDes import *
import random
message = "01234567"
key_11 = random.randrange(0, 256)
key_1 = bytes([key_11, 0, 0, 0, 0, 0, 0, 0])
key_21 = random.randrange(0, 256)
key_2 = bytes([key_21, 0, 0, 0, 0, 0, 0, 0])
iv = bytes([0] * 8)
k1 = des(key_1, ECB, iv, pad=None, padmode=PAD_PKCS5)
k2 = des(key_2, ECB, iv, pad=None, padmode=PAD_PKCS5)
# Alice sending the encrypted message
cipher = k1.encrypt(k2.encrypt(message))
print("Key 11:", key_11)
print("Key 21:", key_21)
print("Encrypted:", cipher)
# This is Bob
message = k2.decrypt(k1.decrypt(cipher))
print("Decrypted:", message)
# Eve's attack on Double DES
lookup = {}
for i in range(256):
key = bytes([i, 0, 0, 0, 0, 0, 0, 0])
k = des(key, ECB, iv, pad=None, padmode=PAD_PKCS5)
lookup[k.encrypt((message))] = i
for i in range(256):
key = bytes([i, 0, 0, 0, 0, 0, 0, 0])
k = des(key, ECB, iv, pad=None, padmode=PAD_PKCS5)
if (k.decrypt(cipher)) in lookup:
print("Key 11:", i)
print("Key 21:", lookup[k.decrypt(cipher)])
key_1 = bytes([i, 0, 0, 0, 0, 0, 0, 0])
key_2 = bytes([lookup[k.decrypt(cipher)], 0, 0, 0, 0, 0, 0, 0])
k1 = des(key_1, ECB, iv, pad=None, padmode=PAD_PKCS5)
k2 = des(key_2, ECB, iv, pad=None, padmode=PAD_PKCS5)
print('Eve break double DES', k2.decrypt(k1.decrypt(cipher)))
break