Fernet Encryption in Python

We will be using python cryptography library

  • install cryptography library using pip install cryptography
from cryptography.fernet import Fernet

KEY = ""  # Fernet compatible key or;
          # you can generate key using Fernet.generate_key()

CIPHER_SUITE = Fernet(KEY)

def encrypt(plain):
    encoded_text = CIPHER_SUITE.encrypt(plain.encode())
    return encoded_text.decode()

def decrypt(encoded):
    decoded_text = CIPHER_SUITE.decrypt(encoded.encode())
    return decoded_text.decode()