In this article, we will learn about encryption and decryption and their implementation in python. We will talk about why they are needed and a little bit about the basics.

What is cryptography? Cryptography is the process of the conversion of plain text into cipher text which is called encryption of data and recovering the plain text back from the cipher text which is called decryption of data.

Here, We are going to take the help of the fernet module that comes with the cryptography package to encrypt and decrypt data using Python. When we are going to implement encryption and decryption, we need to revise our basics that we need a unique key that is called the encryption key that is used to encrypt and decrypt data and without that, you cannot read or manipulate the encrypted data. Here, we will generate that key using the fernet module. We require a key for encryption. There are two main types of keys used for encryption and decryption. They are Symmetric-key and Asymmetric-key. Here, we will use the concept of Symmetric-key.

In the symmetric algorithm, we use the same key to encrypt and decrypt the data. The fernet module of the cryptography package has inbuilt functions for the generation of the key, encryption of plain text into cipher text, and decryption of cipher text into plain text 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.

Many times we have a requirement to store the data in the encrypted form so that normal users can’t read it and only the system will be allowed to read it for security purposes. So to achieve this we use the technique of encryption and decryption.

Installation:

Here, we are going to install the cryptography package using pip3. Run the below command in your terminal.

pip3 install cryptography

Here, in the below example, we will generate the encryption key and then will use the python function to encrypt and decrypt.

Step 1: Import the required python modules.

import cryptography
from cryptography.fernet import Fernet

Step 2: Generate the encryption key using fernet, You can also use the random key generator.

encryption_key = Fernet.generate_key()
cipher_suite = Fernet(key)

Step 3: Encode the string into a byte string before encryption and then after encoding into a byte string encrypt the string.

access_token = 'dummyaccesstoken'
encoded_accesstoken = access_token.encode()
encrypted_accesstoken = cipher_suite.encrypt(encoded_accesstoken)

Step 4: Decrypt the encrypted string into the plain text.

decrypted_accesstoken = cipher_suite.decrypt(encoded_accesstoken)
accesstoken = decrypted_accesstoken.decode()

Conclusion:

Now, we know how to implement symmetric-key encryption and decryption using the cryptography python package.

It would be good if you can save the key in the .env file and then retrieve it using load env variable module and then use that key to encrypt and store password or decrypt password from database to verify if it matches. There are various other cases where you can use this, be it a mini−project or a large scale project.

Let us know if you face any issues with the implementation of this by posting a comment.

Happy Coding!!