From ee825dcf39656cc62c3f9f2e336861113dce9c52 Mon Sep 17 00:00:00 2001 From: Tiago Garcia Date: Wed, 11 Dec 2024 23:15:59 +0000 Subject: [PATCH] Make decrypt_file return the plaintext content Signed-off-by: Tiago Garcia --- delivery2/lib/symmetric_encryption.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/delivery2/lib/symmetric_encryption.py b/delivery2/lib/symmetric_encryption.py index 040a697..be4d385 100644 --- a/delivery2/lib/symmetric_encryption.py +++ b/delivery2/lib/symmetric_encryption.py @@ -35,6 +35,8 @@ def encrypt_file(input_file, output_file=None): # Function to decrypt a file def decrypt_file(key, input_file, output_file=None): + plaintext_content = b"" + with open(input_file, 'rb') as infile: # Read the IV from the input file iv = infile.read(16) @@ -46,9 +48,19 @@ def decrypt_file(key, input_file, output_file=None): while chunk := infile.read(2048): plaintext = decryptor.update(chunk) outfile.write(plaintext) + plaintext_content += plaintext # Finalize decryption - outfile.write(decryptor.finalize()) + final_chunk = decryptor.finalize() + outfile.write(final_chunk) + plaintext_content += final_chunk + else: + while chunk := infile.read(2048): + plaintext = decryptor.update(chunk) + plaintext_content += plaintext - return True + # Finalize decryption + plaintext_content += decryptor.finalize() + + return plaintext_content