-
Notifications
You must be signed in to change notification settings - Fork 3
/
CipherAdapter.py
120 lines (103 loc) · 3.53 KB
/
CipherAdapter.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
from Crypto.Util.strxor import strxor
class _BlockCipher(object):
"""
BlockCipher baseclass.
Derive from this class to add the basic cipher modes
to a cipher.
The subclass needs to implement: encrypt_block and decrypt_block.
"""
MODE_ECB = 1
MODE_CBC = 2
MODE_CFB = 3
# MODE_PGP = 4 is no longer supported by PyCrypto
MODE_OFB = 5
MODE_CTR = 6
@classmethod
def new(cls, key, *args, **kwargs):
return cls(key, *args, **kwargs)
def __init__(self, *args, **kwargs):
def getarg(pos, name, default):
if 0<=pos<len(args):
return args[pos]
if name in kwargs:
return kwargs[name]
return default
self.mode = getarg(0, 'mode', 1)
self.IV = getarg(1, 'IV', b'\x00' * self.block_size)
self.counter = getarg(-1, 'counter', None)
def encrypt(self, data):
result = b''
iv = self.IV
for o in range(0, len(data), self.block_size):
blk = data[o:o+self.block_size]
if self.mode == self.MODE_ECB:
pass
elif self.mode == self.MODE_CBC:
blk = strxor(blk, iv)
elif self.mode == self.MODE_CFB:
blk2 = blk
blk = iv
elif self.mode == self.MODE_OFB:
blk2 = blk
blk = iv
elif self.mode == self.MODE_CTR:
blk2 = blk
blk = self.counter()
else:
raise Exception("unsupported cipher mode %d" % self.mode)
res = self.encrypt_block(blk)
if self.mode == self.MODE_ECB:
pass
elif self.mode == self.MODE_CBC:
iv = res
elif self.mode == self.MODE_CFB:
res = strxor(res, blk2)
iv = res
elif self.mode == self.MODE_OFB:
iv = res
res = strxor(res, blk2)
elif self.mode == self.MODE_CTR:
res = strxor(res[:len(blk2)], blk2)
result += res
self.IV = iv
return result
def decrypt(self, data):
result = b''
iv = self.IV
for o in range(0, len(data), self.block_size):
blk = data[o:o+self.block_size]
if self.mode == self.MODE_ECB:
pass
elif self.mode == self.MODE_CBC:
pass
elif self.mode == self.MODE_CFB:
blk2 = blk
blk = iv
elif self.mode == self.MODE_OFB:
blk2 = blk
blk = iv
elif self.mode == self.MODE_CTR:
blk2 = blk
blk = self.counter()
else:
raise Exception("unsupported cipher mode %d" % self.mode)
if self.mode in (self.MODE_CFB, self.MODE_CTR, self.MODE_OFB):
res = self.encrypt_block(blk)
else:
res = self.decrypt_block(blk)
if self.mode == self.MODE_ECB:
pass
elif self.mode == self.MODE_CBC:
res = strxor(res, iv)
iv = blk
elif self.mode == self.MODE_CFB:
iv = blk2
res = strxor(res, blk2)
elif self.mode == self.MODE_OFB:
iv = res
res = strxor(res, blk2)
elif self.mode == self.MODE_CTR:
res = strxor(res[:len(blk2)], blk2)
result += res
self.IV = iv
return result