Encryption And Decryption In Python

Encryption And Decryption In Python

Published on October 28 2021

In this tutorial, you will learn how to encrypt and decrypt our data in Python.

So the first question comes to mind: what is an Encryption?

  • In simple terms, when you want to hide your data such that nobody can read except you or it can only be read after decryption.
  • Many websites store passwords in some sort of encrypted format so that nobody can access them except you.

Example

  • Suppose we have some data and are encrypted with some key.
    Data: "My Website"
    Key: "12345"
    Encrypted Data: "XDF015SEDD@PKI587RTF2152"
  • Encrypted data is not easily readable and understood.
  • We can get our data back in simple form by doing a description with the same key.
    Encrypted Data: "XDF015SEDD@PKI587RTF2152"
    Key: "12345"
    Data: "My Website"
  • This process of encryption and decryption is called cryptography.
  • In the above example, we have used symmetric encryption i.e. same key for both encryption and decryption.

Methods

There are many ways you can do the encryption.

  1. Pycrypto module
  2. Cryptography module
  3. Own logic with some random numbers and many more other libraries

In this tutorial we'll be using a cryptography module with symmetric encryption: the same key will be used to encrypt and decrypt the data.

Code Explanation

  1. The first step is to install the cryptography module using the Pip command.
    • Steps: Open the command prompt if your Python path variable is set and run the pip command mentioned below or if you are using any IDE like PyCharm, then you can open the terminal window.
      1. Pycharm → goto Menu → view → tool window--> and click on terminal.
      2. type "pip install cryptography" or "pip3 install cryptography"
      3. hit enter
      4. Cryptography module will get install
  2. Next step is to import the "Fernet" class from the cryptography module which is an implementation of symmetric authenticated cryptography and doesn’t allow you to read or modify the data without a "key".
    # importing the library
    from cryptography.fernet import Fernet
  3. We need to generate a key using the generate_key function which generates a unique key every time. If you lose this key, you will not be able to decrypt data that was encrypted with this key. To avoid such issues, you can store the key in some file or database and load the same key every time wherever needed in your project.
    # generating a key
    key = Fernet.generate_key()
  4. Declare a variable with some string message in it and convert it to bytes with the encode method.
    # encoding string message
    msg = "Welcome to Channel".encode()
  5. Create the object of Fernet class with the generated key.
    # creating Fernet class object with the same key
    f_obj = Fernet(key)
  6. Encrypt our message with an encrypt method and store out encrypted data in some variables. 
    # encrypting string message with the key
    encrpted_msg = f_obj.encrypt(msg)
  7. Now our data is encrypted and the final step is to decrypt the message by calling the decrypt method. Decrypt method will decrypt the data which was encrypted with the generated key. If the key is correct, then it will return us with the original plain text otherwise it will raise an exception.
    # decrypting string message with the key
    decrypted_msg = f_obj.decrypt(encrpted_msg)
    print(decrypted_msg)

Source Code

# importing the library
from cryptography.fernet import Fernet

# generating a key
key = Fernet.generate_key()
print(key)

# encoding string message
msg = "Welcome to Channel".encode()

# creating Fernet class object with the same key
f_obj = Fernet(key)

# encrypting string message with the key
encrpted_msg = f_obj.encrypt(msg)
print(encrpted_msg)

# decrypting string message with the key
decrypted_msg = f_obj.decrypt(encrpted_msg)
print(decrypted_msg)

Output

key: SDERDF1251XDF015SEDD@1212SAQWQ3223DSDSP
encrypted message: GRTYGFGTYUJH67564TFDRT45456YGHJHKJU87856453FDGFG5656GFDFDR3E4345454
decrypted message: Welcome to Channel



Next Tutorial