To encrypt a file using AES-256 encryption with a password in Python, you can use the cryptography
library. Here's an example code:
main.py650 chars28 lines
To encrypt a file, you can call the function like this:
main.py43 chars2 lines
This will encrypt the file example.txt
using the password my_password
and save the encryption key in a new file named key.key
, which will be returned by the function.
Note that this example uses the same key for all encrypted files. In a real-world scenario, you would want to use a different key for each encrypted file to improve security, and keep the keys secure by storing them in a secure location such as a vault or a key management system.
gistlibby LogSnag