Shrawan Poudel
Shrawan's Blog

Shrawan's Blog

Fernet Encryption in Python

Shrawan Poudel
·Mar 16, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

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()