Python encrypt and decrypt
file encrypt.py
# pip install cryptography
import sys
from cryptography.fernet import Fernet
import hashlib
import base64
path = ''
key = ''
if __name__ == "__main__":
path = str(sys.argv[1])
key = base64.b64encode(hashlib.md5(str(sys.argv[2]).encode()).hexdigest().encode())
cipher_suite = Fernet(key)
file = open(path, 'r')
s = file.read()
file.close()
cipher_text = cipher_suite.encrypt(s.encode())
file2 = open(path, 'w+')
file2.write((cipher_text.decode()))
file2.close()
file decrypt.py
# pip install cryptography
import sys
from cryptography.fernet import Fernet
import hashlib
import base64
path = ''
key = ''
if __name__ == "__main__":
path = str(sys.argv[1])
key = base64.b64encode(hashlib.md5(str(sys.argv[2]).encode()).hexdigest().encode())
cipher_suite = Fernet(key)
file = open(path, 'r')
s = file.read()
file.close()
plain_text = cipher_suite.decrypt(s.encode())
file2 = open(path, 'w+')
file2.write((plain_text.decode()))
file2.close()
file data.txt
secret words
run in terminal
python encrypt.py data.txt your_password
python decrypt.py data.txt your_password
Comments
Post a Comment