23 lines
736 B
Python
23 lines
736 B
Python
from Crypto.Cipher import AES
|
|
from Crypto.Util.Padding import pad, unpad
|
|
import hashlib
|
|
|
|
|
|
class AESCipher:
|
|
def __init__(self, seed):
|
|
assert len(seed) == 32, "Seed must be 32 bytes long"
|
|
self.private_key = hashlib.sha256(seed).digest()
|
|
|
|
def encrypt(self, data: bytes) -> bytes:
|
|
cipher = AES.new(self.private_key, AES.MODE_CBC)
|
|
ct_bytes = cipher.encrypt(pad(data, AES.block_size))
|
|
return cipher.iv + ct_bytes
|
|
|
|
def decrypt(self, encrypted_data: bytes) -> bytes:
|
|
iv = encrypted_data[:AES.block_size]
|
|
ct = encrypted_data[AES.block_size:]
|
|
cipher = AES.new(self.private_key, AES.MODE_CBC, iv)
|
|
pt = unpad(cipher.decrypt(ct), AES.block_size)
|
|
return pt
|
|
|