removed password param
This commit is contained in:
parent
9cbb6ba721
commit
b6c57ed294
|
@ -5,7 +5,7 @@ from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||||
from cryptography.hazmat.backends import default_backend
|
from cryptography.hazmat.backends import default_backend
|
||||||
|
|
||||||
# Function to derive a 256-bit key from a password and salt
|
# Function to derive a 256-bit key from a password and salt
|
||||||
def derive_key(passwd, salt):
|
def derive_key(salt):
|
||||||
kdf = PBKDF2HMAC(
|
kdf = PBKDF2HMAC(
|
||||||
algorithm=hashes.SHA256(),
|
algorithm=hashes.SHA256(),
|
||||||
length=32,
|
length=32,
|
||||||
|
@ -13,14 +13,12 @@ def derive_key(passwd, salt):
|
||||||
iterations=10000,
|
iterations=10000,
|
||||||
backend=default_backend()
|
backend=default_backend()
|
||||||
)
|
)
|
||||||
key = kdf.derive(passwd.encode())
|
return kdf.derive(b'')
|
||||||
return key.hex()
|
|
||||||
|
|
||||||
|
|
||||||
# Function to encrypt a file using a password
|
# Function to encrypt a file using a salt
|
||||||
def encrypt_file(passwd, input_file, output_file):
|
def encrypt_file(salt, input_file, output_file):
|
||||||
salt = os.urandom(16)
|
key = derive_key(salt)
|
||||||
key = derive_key(passwd, salt)
|
|
||||||
iv = os.urandom(16)
|
iv = 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()
|
||||||
|
@ -34,15 +32,15 @@ def encrypt_file(passwd, input_file, output_file):
|
||||||
f.write(salt + iv + ciphertext)
|
f.write(salt + iv + ciphertext)
|
||||||
|
|
||||||
|
|
||||||
# Function to decrypt a file using a password
|
# Function to decrypt a file
|
||||||
def decrypt_file(passwd, input_file, output_file=None):
|
def decrypt_file(input_file, output_file=None):
|
||||||
with open(input_file, 'rb') as f:
|
with open(input_file, 'rb') as f:
|
||||||
encrypted_data = f.read()
|
encrypted_data = f.read()
|
||||||
|
|
||||||
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(passwd, 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