nonce + salt generated in function

This commit is contained in:
RubenCGomes 2024-11-20 19:46:18 +00:00
parent 7fd5f1bcd8
commit 60370f3df7
1 changed files with 10 additions and 3 deletions

View File

@ -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,9 +31,12 @@ def encrypt_file(salt, input_file, output_file):
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
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
def decrypt_file(input_file, output_file=None):
@ -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()