how do i solve this error ValueError: Fernet key must be 32 url-safe base64-encoded…
- User
how do i solve this error ValueError: Fernet key must be 32 url-safe base64-encoded bytes. in this code from cryptography.fernet import Fernet input_file_path = r'C:\Users\Abu Bakr Siddique\Desktop\Test.txt' encrypted_file_path = r'C:\Users\Abu Bakr Siddique\Desktop\Test2.txt' decrypted_file_path = r'C:\Users\Abu Bakr Siddique\Desktop\Test3.txt'
def generate_key(): """""" #Generates a key and save it into a file# """""" key = Fernet.generate_key() with open("input_file_path", "wb") as key_file: key_file.write(key) return key
def load_key(): """""" #Loads the key from the current directory named secret.key# """""" return open("input_file_path", "rb").read()
def encrypt_file(encrypted_file_path, key): """""" #Given a filename (str) and key (bytes), it encrypts the file and write it# """""" f = Fernet(key) with open(input_file_path, "rb") as file: # read all file data file_data = file.read() encrypted_data = f.encrypt(file_data) with open(encrypted_file_path, "wb") as file: file.write(encrypted_data)
def decrypt_file(decrypted_file_path, key): """""" #Given a filename (str) and key (bytes), it decrypts the file and write it# """""" f = Fernet(key) with open(decrypted_file_path, "rb") as file: # read the encrypted data encrypted_data = file.read() decrypted_data = f.decrypt(encrypted_data) with open(decrypted_file_path, "wb") as file: file.write(decrypted_data)
#Encrypt the file encrypt_file(input_file_path, "wb") print(f"{input_file_path} has been encrypted.")
#Decrypt the file decrypt_file(decrypted_file_path, "rb") print(f"{decrypted_file_path} has been decrypted.")
#Encrypt the file again encrypt_file(encrypted_file_path, "wb") print(f"{encrypted_file_path} has been encrypted again.")
- Assistant