To encrypt a file in OpenSSL in Python, you can use the cryptography
library. Here's how:
main.py596 chars23 lines
In the above example, a new symmetric encryption key is generated using Fernet.generate_key()
. This key is used to create a Fernet
object, which is used to encrypt the contents of the original file. The encrypted bytes are then written to a new file, and the encryption key is saved to a separate file.
To decrypt the encrypted file, you can use the same key and Fernet
object:
main.py503 chars20 lines
In the above code, the encryption key is read from the file that was saved earlier. The Fernet
object is created with the key, and then used to decrypt the contents of the encrypted file. The decrypted bytes are then written to a new file.
gistlibby LogSnag