Make decrypt_file return the plaintext content

Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
Tiago Garcia 2024-12-11 23:15:59 +00:00
parent 62272039d6
commit ee825dcf39
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
1 changed files with 14 additions and 2 deletions

View File

@ -35,6 +35,8 @@ def encrypt_file(input_file, output_file=None):
# Function to decrypt a file # Function to decrypt a file
def decrypt_file(key, input_file, output_file=None): def decrypt_file(key, input_file, output_file=None):
plaintext_content = b""
with open(input_file, 'rb') as infile: with open(input_file, 'rb') as infile:
# Read the IV from the input file # Read the IV from the input file
iv = infile.read(16) iv = infile.read(16)
@ -46,9 +48,19 @@ def decrypt_file(key, input_file, output_file=None):
while chunk := infile.read(2048): while chunk := infile.read(2048):
plaintext = decryptor.update(chunk) plaintext = decryptor.update(chunk)
outfile.write(plaintext) outfile.write(plaintext)
plaintext_content += plaintext
# Finalize decryption # 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