Open In App

Fernet (symmetric encryption) using Cryptography module in Python

Cryptography is the practice of securing useful information while transmitting from one computer to another or storing data on a computer. Cryptography deals with the encryption of plaintext into ciphertext and decryption of ciphertext into plaintext. Python supports a cryptography package that helps us encrypt and decrypt data. The fernet module of the cryptography package has inbuilt functions for the generation of the key, encryption of plaintext into ciphertext, and decryption of ciphertext into plaintext using the encrypt and decrypt methods respectively. The fernet module guarantees that data encrypted using it cannot be further manipulated or read without the key. 

Methods Used:



Parameters:

  • data (bytes) – The plaintext to be encrypted.

Return value: A ciphertext that cannot be read or altered without the key. It is URL-safe base64-encoded and is referred to as Fernet token.



Parameters:

  • token (bytes) – The Fernet token (ciphertext) is passed for decryption.
  • ttl (int) – Optionally, one may provide an integer as second parameter in the decrypt method. The ttl denotes the time about how long a token is valid. If the token is older than ttl seconds (from the time it was originally created) an exception is thrown. If ttl is not passed as a parameter, then age of the token is not considered. If the token is somehow invalid, an exception is thrown.

Returns value:  Returns the original plaintext.

Steps to write the program:

At first, the cryptography package needs to be installed using the following command:

pip install cryptography




# Fernet module is imported from the 
# cryptography package
from cryptography.fernet import Fernet
  
# key is generated
key = Fernet.generate_key()
  
# value of key is assigned to a variable
f = Fernet(key)
  
# the plaintext is converted to ciphertext
token = f.encrypt(b"welcome to geeksforgeeks")
  
# display the ciphertext
print(token)
  
# decrypting the ciphertext
d = f.decrypt(token)
  
# display the plaintext
print(d)

Output:

b’gAAAAABfYMSL3Cjz8I8Sg7NwatdtTvOtqHtPrNDGXTGx4w1gW-9yvrMBUFz3bAWnwVk2WjcOrhjfAzyX7Z6M1IDbcRDhxPvd2dKPjypVv9hLQ1lARWdf-RE=’
b’welcome to geeksforgeeks’

The decrypted output has a ‘b’ in front of the original message which indicates the byte format. However, this can be removed using the decode() method while printing the original message. The program below implements the decode() method. 




# Fernet module is imported from the 
# cryptography package
from cryptography.fernet import Fernet
  
  
# key is generated
key = Fernet.generate_key()
  
# value of key is assigned to a variable
f = Fernet(key)
  
# the plaintext is converted to ciphertext
token = f.encrypt(b"welcome to geeksforgeeks")
  
# display the ciphertext
print(token)
  
# decrypting the ciphertext
d = f.decrypt(token)
  
# display the plaintext and the decode() method 
# converts it from byte to string
print(d.decode())

Output:

b’gAAAAABfYMTfbEYTSsU6BCyXr9ArUIbpELTu5axUtWRfIxc4zzv3AktmOwdNSd1rH_zrL4Qz7tDi1K067kLx0Ma3S828nSTJlP9Y7L0_ZfVyCelZlayGK3k=’
welcome to geeksforgeeks


Article Tags :