Skip to content

Instantly share code, notes, and snippets.

@stanwu
Created September 9, 2020 14:43
Show Gist options
  • Select an option

  • Save stanwu/cb0f116aa83f348c0217cf86fe979ead to your computer and use it in GitHub Desktop.

Select an option

Save stanwu/cb0f116aa83f348c0217cf86fe979ead to your computer and use it in GitHub Desktop.
Full Code Example (encode)
from cryptography.fernet import Fernet
def generate_key():
"""
Generates a key and save it into a file
"""
key = Fernet.generate_key()
with open("secret.key", "wb") as key_file:
key_file.write(key)
def load_key():
"""
Load the previously generated key
"""
return open("secret.key", "rb").read()
def encrypt_message(message):
"""
Encrypts a message
"""
key = load_key()
encoded_message = message.encode()
f = Fernet(key)
encrypted_message = f.encrypt(encoded_message)
print(encrypted_message)
if __name__ == "__main__":
encrypt_message("encrypt this message")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment