I used cryptography library like as following:
from cryptography.fernet import Fernet
# key generation
key = Fernet.generate_key()
# string the key in a file
with open('filekey.key', 'wb') as filekey:
filekey.write(key)
# opening the key
with open('filekey.key', 'rb') as filekey:
key = filekey.read()
# using the generated key
fernet = Fernet(key)
# opening the original file to encrypt
with open('test.py', 'rb') as file:
original = file.read()
# encrypting the file
encrypted = fernet.encrypt(original)
# opening the file in write mode and
# writing the encrypted data
with open('test.py', 'wb') as encrypted_file:
encrypted_file.write(encrypted)
Then file called filekey.key generated like as image below it contains the encryption key: enter image description here
Finally when I run the server it show NotFound error because the module what I encrypted doesn't exist like below:
FileNotFoundError: [Errno 2] No such file or directory: 'test.py' - - -