From 60370f3df7cc24b1b847ad3b5b66a352de7fcdf5 Mon Sep 17 00:00:00 2001 From: RubenCGomes Date: Wed, 20 Nov 2024 19:46:18 +0000 Subject: [PATCH] nonce + salt generated in function --- delivery1/lib/symmetric_encryption.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/delivery1/lib/symmetric_encryption.py b/delivery1/lib/symmetric_encryption.py index ada1660..dfccb66 100644 --- a/delivery1/lib/symmetric_encryption.py +++ b/delivery1/lib/symmetric_encryption.py @@ -17,9 +17,12 @@ def derive_key(salt): # Function to encrypt a file using a salt -def encrypt_file(salt, input_file, output_file): +def encrypt_file(input_file, output_file=None): + salt = os.urandom(16) key = derive_key(salt) iv = os.urandom(16) + nonce = os.urandom(16) + cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend()) encryptor = cipher.encryptor() @@ -28,8 +31,11 @@ def encrypt_file(salt, input_file, output_file): ciphertext = encryptor.update(plaintext) + encryptor.finalize() - with open(output_file, 'wb') as f: - f.write(salt + iv + ciphertext) + if output_file is not None: + with open(output_file, 'wb') as f: + f.write(salt + iv + ciphertext) + + return salt + iv + ciphertext, nonce # Function to decrypt a file @@ -40,6 +46,7 @@ def decrypt_file(input_file, output_file=None): salt = encrypted_data[:16] iv = encrypted_data[16:32] ciphertext = encrypted_data[32:] + key = derive_key(salt) cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend()) decryptor = cipher.decryptor()