-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes.py
28 lines (24 loc) · 1.11 KB
/
aes.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
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import os
def aes_encrypt(key, plaintext):
print(key)
#convert to 128 bit key
iv = os.urandom(16)
padder = padding.PKCS7(128).padder()
padded_data = padder.update(plaintext.encode()) + padder.finalize()
cipher = Cipher(algorithms.AES(key.encode()), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
cipher_text = encryptor.update(padded_data) + encryptor.finalize()
return iv + cipher_text
def aes_decrypt(key, ciphertext):
iv = ciphertext[:16]
cipher_text = ciphertext[16:]
cipher = Cipher(algorithms.AES(key.encode()), modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()
padded_data = decryptor.update(cipher_text) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
plaintext = unpadder.update(padded_data) + unpadder.finalize()
return plaintext.decode()