-
Notifications
You must be signed in to change notification settings - Fork 55
/
magic_box.py
77 lines (61 loc) · 1.89 KB
/
magic_box.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
from Crypto.Cipher import AES, DES
algos = ["AES", "DES"]
modes = ["ECB", "CBC", "OFB", "CFB"]
aes_mapping_mode = {
"ECB": AES.MODE_ECB,
"CBC": AES.MODE_CBC,
"OFB": AES.MODE_OFB,
"CFB": AES.MODE_CFB,
}
des_mapping_mode = {
"ECB": DES.MODE_ECB,
"CBC": DES.MODE_CBC,
"OFB": DES.MODE_OFB,
"CFB": DES.MODE_CFB,
}
def crc128(data, poly=0x883ddfe55bba9af41f47bd6e0b0d8f8f):
crc = (1 << 128) - 1
for b in data:
crc ^= b
for _ in range(8):
crc = (crc >> 1) ^ (poly & -(crc & 1))
return crc ^ ((1 << 128) - 1)
'''
Unencryptable World of symmetric cryptography
'''
class Magic_box():
api = None
mode = None
key = None
IV = None
def __init__(self, algo, mode, keys):
if algo == "AES":
try:
if len(keys) == 16:
assert mode == "ECB"
self.api = AES.new(keys[:16], aes_mapping_mode[mode])
elif len(keys) == 32:
self.api = AES.new(
keys[:16], aes_mapping_mode[mode], keys[16:])
else:
assert False, "Invalid parameters"
except:
assert False, "Invalid parameters"
elif algo == "DES":
try:
if len(keys) == 8:
assert mode == "ECB"
self.api = DES.new(keys[:8], des_mapping_mode[mode])
elif len(keys) == 16:
self.api = DES.new(
keys[:8], des_mapping_mode[mode], keys[8:])
else:
assert False, "Invalid parameters"
except:
assert False, "Invalid parameters"
else:
assert False, "Not implemented error"
def auto_dec(self, msg):
return self.api.decrypt(msg)
def auto_enc(self, msg):
return self.api.encrypt(msg)