seg/trabalho4/AES.py

24 lines
666 B
Python
Raw Normal View History

2023-10-22 19:56:01 +00:00
from Crypto.Cipher import AES
2023-10-22 20:15:12 +00:00
import base64
2023-10-22 19:56:01 +00:00
class AESCipher(object):
def __init__(self, key):
self.key = key
self.nonce = b'\xeb\x06\xfa\xa5C{3\x9d\x9dj\x18\xa2V\xe5\xcb\xc2'
def encrypt(self, raw):
cipher = AES.new(self.key, AES.MODE_EAX, nonce=self.nonce)
ciphertext, _ = cipher.encrypt_and_digest(raw.encode())
2023-10-22 20:15:12 +00:00
return base64.b64encode(ciphertext)
2023-10-22 19:56:01 +00:00
def decrypt(self, enc):
if type(enc) == str:
enc = eval(enc)
2023-10-22 20:15:12 +00:00
enc = base64.b64decode(enc)
2023-10-22 19:56:01 +00:00
cipher = AES.new(self.key, AES.MODE_EAX, nonce=self.nonce)
plaintext = cipher.decrypt(enc)
return plaintext.decode()