def encrypt(self):
if not os.path.exists(self.filename):
raise Exception('File does not exist!')
# Generate a random salt
salt = Fernet.generate_key()
# Calculate the HMAC of the salt using the key
hmac_digest = hmac.new(self.key, salt, hashlib.sha256).digest()
# Encrypt the data with the salt and key
fernet = Fernet(salt + self.key)
with open(self.filename, 'rb') as file:
file_data = file.read()
encrypted_data = fernet.encrypt(file_data)
# Write the encrypted data to the file
with open(self.filename, 'wb') as file:
file.write(encrypted_data)
def decrypt(self):
if not os.path.exists(self.filename):
raise Exception('File does not exist!')
# Read the salt and HMAC digest from the separate file
with open(self.filename + '.salt', 'rb') as file:
k_sz = Fernet.key_size
salt = file.read(k_sz)
hmac_digest = file.read(hashlib.sha256().digest_size)
here in the decrypt function i am trying to get the salt and hmac value stored in the encrypted file. But receiving the error
File "C:\python programs\test2.py", line 61, in decrypt
k_sz = Fernet.key_size
^^^^^^^^^^^^^^^
AttributeError: type object 'Fernet' has no attribute 'key_size'
i have tried .KEY_SIZE and Fernet(key=None).key_size then it says key value as none not expected