-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_funcs.py
28 lines (21 loc) · 865 Bytes
/
aes_funcs.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
28
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
def encrypt_aes(key, plaintext):
# Create a new AES cipher object in CBC mode with a random IV
cipher = AES.new(key, AES.MODE_CBC)
iv = cipher.iv
# Pad the plaintext to be a multiple of the block size
padded_plaintext = pad(plaintext, AES.block_size)
# Encrypt the padded plaintext
ciphertext = cipher.encrypt(padded_plaintext)
# Prepend IV to ciphertext for use in decryption
return iv + ciphertext
# Decryption
def decrypt_aes(key, ciphertext):
# Get the vector from the start of the block
iv = ciphertext[:AES.block_size]
ciphertext = ciphertext[AES.block_size:]
cipher = AES.new(key, AES.MODE_CBC, iv)
padded_plaintext = cipher.decrypt(ciphertext)
plaintext = unpad(padded_plaintext, AES.block_size)
return plaintext