forked from tanupoo/lorawan-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes_ecb.py
52 lines (46 loc) · 1.29 KB
/
aes_ecb.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
#
# a wrapper module for pycryptodome.
#
from Crypto.Cipher import AES
class AES_ECB():
def __init__(self, key):
"""
key: 8 bytes of bytearray
"""
self.aes_ecb = AES.new(key, AES.MODE_ECB)
def encrypt(self, data):
"""
data: any size of bytearray. expanded into 16 bytes if less.
"""
blk_list = []
for i in range(0,len(data),16):
blk = data[i:i+16]
blk += b"\x00"*(16-len(blk))
blk_list.append(self.aes_ecb.encrypt(bytes(blk)))
return b"".join(blk_list)
def decrypt(self, enc_data):
"""
enc_data: in bytearray, must be multiple of 16.
"""
blk_list = []
for i in range(0,len(enc_data),16):
blk_list.append(self.aes_ecb.decrypt(data[i:i+16]))
return b"".join(blk_list)
def aes128_encrypt(key, plain_data):
"""
one time encryper.
it's used mainly for key generation.
key: in bytes.
plain_data: in bytes.
"""
cipher = AES_ECB(key)
return cipher.encrypt(plain_data)
def aes128_decrypt(key, enc_data):
"""
one time encryper.
it's used mainly for key generation.
key: in bytes.
plain_data: in bytes.
"""
cipher = AES_ECB(key)
return cipher.decrypt(enc_data)