seg/trabalho4/AES.py

24 lines
666 B
Python

from Crypto.Cipher import AES
import base64
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())
return base64.b64encode(ciphertext)
def decrypt(self, enc):
if type(enc) == str:
enc = eval(enc)
enc = base64.b64decode(enc)
cipher = AES.new(self.key, AES.MODE_EAX, nonce=self.nonce)
plaintext = cipher.decrypt(enc)
return plaintext.decode()