forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryptor.py
More file actions
31 lines (25 loc) · 984 Bytes
/
Copy pathencryptor.py
File metadata and controls
31 lines (25 loc) · 984 Bytes
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
29
30
31
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import os
BLOCK_SIZE = 16
def encrypt_frame(frame_bytes, key, iv):
if not frame_bytes:
raise ValueError("Frame bytes cannot be empty")
if len(key) != 32:
raise ValueError("Key must be 32 bytes for AES-256")
if len(iv) != 16:
raise ValueError("IV must be 16 bytes")
cipher = AES.new(key, AES.MODE_CBC, iv)
return cipher.encrypt(pad(frame_bytes, BLOCK_SIZE))
def decrypt_frame(enc_bytes, key, iv):
if not enc_bytes:
raise ValueError("Encrypted bytes cannot be empty")
if len(key) != 32:
raise ValueError("Key must be 32 bytes for AES-256")
if len(iv) != 16:
raise ValueError("IV must be 16 bytes")
cipher = AES.new(key, AES.MODE_CBC, iv)
try:
return unpad(cipher.decrypt(enc_bytes), BLOCK_SIZE)
except ValueError as e:
raise ValueError("Decryption failed: Invalid padding or corrupted data") from e