To create a file encryption script in Python, the steps are:
Here is the sample code:
main.py935 chars37 lines
In this code, we are using the cryptography module for encryption, which provides strong encryption algorithms. The generate_key()
function generates a random secret key and writes it to a file. The load_key()
function reads the secret key from the file. The encrypt_file()
function accepts a filename and a secret key as inputs, reads the content of the file, encrypts it using the secret key, and writes the encrypted content back to the file. The decrypt_file()
function does the reverse, reads the encrypted content from the file, decrypts it using the secret key, and writes the decrypted content back to the file.
To use this script to encrypt a file, simply call encrypt_file(filename, key)
with the name of the file you want to encrypt and the secret key. To decrypt the file, call decrypt_file(filename, key)
with the same filename and secret key.
Note that you should keep the secret key secure, as losing it will prevent you from decrypting the encrypted files.
gistlibby LogSnag