Hi ππ
Welcome in a new post, Today i will show you how to Encrypt And Decrypt files in Python and module called cryptography.
step 1
install cryptography
pip install cryptography
step 2
Generate key to use it in encrypt and decrypt process
from cryptography.fernet import Fernet
from os import remove
# #generate the key
# key = Fernet.generate_key()
# print(key)
Encyrept file
f = Fernet(key)
def encrypt(fname):
with open(fname, 'rb') as f1:
with open(fname + '.enc', 'wb') as f2:
f2.write(f.encrypt(f1.read()))
remove(fname)
Decrypt file
def decrypt(fname):
with open(fname, 'rb') as f1:
with open(fname.replace('.enc', ''), 'wb') as f2:
f2.write(f.decrypt(f1.read()))
remove(fname)
Now we're done π€
Don't forget to like and follow π
Support me on PayPal π€
https://www.paypal.com/paypalme/amr396