Files
labs-information-security/lab5-account-manager-security/core/crypto_manager.py
T

24 lines
784 B
Python
Raw Normal View History

2026-07-12 14:06:45 +04:00
from cryptography.fernet import Fernet
from pathlib import Path
class CryptoManager:
"""
Класс для симметричного шифрования и расшифрования файлов.
"""
def generate_key(self) -> bytes:
return Fernet.generate_key()
def encrypt_file(self, file_path: str, key: bytes, output_path: str) -> None:
fernet = Fernet(key)
data = Path(file_path).read_bytes()
encrypted = fernet.encrypt(data)
Path(output_path).write_bytes(encrypted)
def decrypt_file(self, file_path: str, key: bytes, output_path: str) -> None:
fernet = Fernet(key)
data = Path(file_path).read_bytes()
decrypted = fernet.decrypt(data)
Path(output_path).write_bytes(decrypted)