import os from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend # Function to encrypt a file using a salt def encrypt_file(input_file, output_file=None): key = os.urandom(16) iv = os.urandom(16) cipher = Cipher(algorithms.AES(key), modes.CFB(iv)) encryptor = cipher.encryptor() with open(input_file, 'rb') as f: plaintext = f.read() ciphertext = encryptor.update(plaintext) + encryptor.finalize() ciphertext = iv + ciphertext if output_file is not None: with open(output_file, 'wb') as f: f.write(ciphertext) print(iv.hex()) return key, ciphertext, iv # Function to decrypt a file def decrypt_file(nonce, key, input_file, output_file=None): with open(input_file, 'rb') as f: encrypted_data = f.read() ciphertext = encrypted_data cipher = Cipher(algorithms.AES(key), modes.CFB(nonce)) decryptor = cipher.decryptor() plaintext = decryptor.update(ciphertext) + decryptor.finalize() if output_file is not None: with open(output_file, 'wb') as f: f.write(plaintext) return plaintext.hex()