22 lines
652 B
Python
22 lines
652 B
Python
|
from Crypto.Cipher import AES
|
||
|
|
||
|
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 ciphertext
|
||
|
|
||
|
def decrypt(self, enc):
|
||
|
if type(enc) == str:
|
||
|
enc = eval(enc)
|
||
|
|
||
|
cipher = AES.new(self.key, AES.MODE_EAX, nonce=self.nonce)
|
||
|
|
||
|
plaintext = cipher.decrypt(enc)
|
||
|
print('plaintext', type(plaintext), plaintext)
|
||
|
return plaintext.decode()
|