forked from tecnico-sec/Python-Crypto-Details
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAESCipherByteArrayMixer.py
More file actions
42 lines (32 loc) · 1.2 KB
/
Copy pathAESCipherByteArrayMixer.py
File metadata and controls
42 lines (32 loc) · 1.2 KB
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
import sys
import traceback
from AESKeyGenerator import AESKeyGenerator
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
class AESCipherByteArrayMixer:
def __init__(self, opmode):
self.opmode = opmode # ENCRYPT DECRYPT
self.keyFile = None
self.mode = None # CBC OFB ECB
def setParameters(self, keyfile, mode):
self.keyFile = keyfile
self.mode = mode
def mix(self, byteArray, byteArray2):
global cipher
iv = b'0000000000000000'
try:
key = AESKeyGenerator.read(self.keyFile)
if self.mode == 'ECB':
cipher = AES.new(key, AES.MODE_ECB)
elif self.mode == 'CBC':
cipher = AES.new(key, AES.MODE_CBC, iv)
elif self.mode == 'OFB':
cipher = AES.new(key, AES.MODE_OFB, iv)
print("Ciphering ...")
if self.opmode == 'Cipher.ENCRYPT_MODE':
return cipher.encrypt(pad(byteArray,16))
elif self.opmode == 'Cipher.DECRYPT_MODE':
return unpad(cipher.decrypt(byteArray), 16)
except Exception:
traceback.print_exc(file=sys.stdout)
return None