nonce + salt generated in function
This commit is contained in:
parent
7fd5f1bcd8
commit
60370f3df7
|
@ -17,9 +17,12 @@ def derive_key(salt):
|
||||||
|
|
||||||
|
|
||||||
# Function to encrypt a file using a 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)
|
key = derive_key(salt)
|
||||||
iv = os.urandom(16)
|
iv = os.urandom(16)
|
||||||
|
nonce = os.urandom(16)
|
||||||
|
|
||||||
cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())
|
cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())
|
||||||
encryptor = cipher.encryptor()
|
encryptor = cipher.encryptor()
|
||||||
|
|
||||||
|
@ -28,9 +31,12 @@ def encrypt_file(salt, input_file, output_file):
|
||||||
|
|
||||||
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
|
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
|
||||||
|
|
||||||
|
if output_file is not None:
|
||||||
with open(output_file, 'wb') as f:
|
with open(output_file, 'wb') as f:
|
||||||
f.write(salt + iv + ciphertext)
|
f.write(salt + iv + ciphertext)
|
||||||
|
|
||||||
|
return salt + iv + ciphertext, nonce
|
||||||
|
|
||||||
|
|
||||||
# Function to decrypt a file
|
# Function to decrypt a file
|
||||||
def decrypt_file(input_file, output_file=None):
|
def decrypt_file(input_file, output_file=None):
|
||||||
|
@ -40,6 +46,7 @@ def decrypt_file(input_file, output_file=None):
|
||||||
salt = encrypted_data[:16]
|
salt = encrypted_data[:16]
|
||||||
iv = encrypted_data[16:32]
|
iv = encrypted_data[16:32]
|
||||||
ciphertext = encrypted_data[32:]
|
ciphertext = encrypted_data[32:]
|
||||||
|
|
||||||
key = derive_key(salt)
|
key = derive_key(salt)
|
||||||
cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())
|
cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())
|
||||||
decryptor = cipher.decryptor()
|
decryptor = cipher.decryptor()
|
||||||
|
|
Loading…
Reference in New Issue