Encryption In Python

Encryption is the art or science of securing digital data using some algorithm that uses mathematical calculations. It will convert the data to symbols that anyone cannot understand, making it useless for them.

This process will require the guy who will encrypt the message a password and for the receiver to have that password to decrypt it read your confidential message, unlike the hash which makes it impossible to restore the data.

1. Encryption With The Fernet Package

There are a lot of encryption libraries you can use, and it has different algorithms for each of them. Most of them are good such as AES-256 or Blowfish encryption, and we will learn about the cryptography library in this tutorial. Let’s first install this library:

pip3 install cryptography

Encrypting a message is easy using only a few commands using the Fernet package. Let’s see an example:

from cryptography.fernet import Fernet

# Generating The Key
key = Fernet.generate_key()

# Priniting The Key
print(key)

# Choosing The Message
message = "Thanks".encode()

# Creating The Key Object
key_obj = Fernet(key)

# Encrypting The Message
enc_message = key_obj.encrypt(message)

# Printing The Encrypted Message
print(enc_message)

Every time you run the code, you will notice that it will give you another encryption message, which makes it more potent in encryption. The previous code seems weird, but I will try to explain:

The first thing is importing the fernet package from the cryptography library used for encryption. Next, we use the function Fernet.generate_key() to generate a key for the encryption and the password used.

We’ve printed the key of encryption (password), and you can see it used a long password for encryption. It will generate a new one every time you run the code.

Let’s now choose the message we want to encrypt, “Thanks” and convert it to byte before starting the encryption using the .encode() function.

We will create a key object for encrypting the message using the Fernet(key) method and passing the encryption key.

Finally, encrypt the message “Thanks” with the key_obj.encrypt(message) function. Remember to pass the message you want to encrypt after converting it to byte.

2. Decryption With The Fernet Package

Since you can encrypt the message, you can also perform the decryption, which is restoring the original massage, but you need to use the same password you’ve used for decryption. Let’s see an example:

from cryptography.fernet import Fernet

# Generating The Key
key = Fernet.generate_key()

# Priniting The Key
print(key)

# Choosing The Message
message = "Thanks".encode()

# Creating The Key Object
key_obj = Fernet(key)

# Encrypting The Message
enc_message = key_obj.encrypt(message)

# Printing The Encrypted Message
print(enc_message)

# Decrypting The Message
dec_message = key_obj.decrypt(enc_message)

# Printing The Decrypted Message
print(dec_message)

When you execute the above code, you can see that it decrypted the original message, and it found it, which is the “Thanks” message.

We’ve used the object key_obj with the password already there when we created it using the Fernet(key) method, so we don’t need to type the key (password) again. 

You can see that the .decrypt() function is used for the decryption, and you need to pass the encrypted message, and it will perform the decryption process.

3. Choose a Password

You’ve noticed that we perform the encryption and decryption process without choosing our password. This package allows you to choose a password for encryption, and it should be the same for decryption. Let’s see an example:

from cryptography.fernet import Fernet
import base64

# Converting The Password To base64-encoded
key = base64.urlsafe_b64encode(b"1234567890AZSFGHJKFDDSFGUKLGDVBN")

# Priniting The Key
print(key)

# Choosing The Message
message = "Thanks".encode()

# Creating The Key Object
key_obj = Fernet(key)

# Encrypting The Message
enc_message = key_obj.encrypt(message)

# Printing The Encrypted Message
print(enc_message)

# Decrypting The Message
dec_message = key_obj.decrypt(enc_message)

# Printing The Decrypted Message
print(dec_message)

You can notice that we’ve chosen our password, which is that long numbers and characters inside the base64.urlsafe_b64encode() function will convert it to a base64-encoded to work with the fernet package. 

The password shouldn’t also be small to be converted to a base64-encoded. Otherwise, you will have an error if you’ve typed a small password. Because this encryption library accepts the 32 bytes, which means the password should be 32 characters long.

4. Storing The Key

You may notice that it is a daunting task to keep setting your password every time you want to encrypt a message or a confidential text to store it later so you can make it very simple which is using the fernet.generate_key() to generate the long key (password) and then store it directly in a .txt file:

from cryptography.fernet import Fernet

# Generating The Key
key = Fernet.generate_key()

# Printing The Key
print(key)

# Generating The New File
file = open("enc_key.key", "wb")

# Writing The Key Inside The File
file.write(key)

# Closing The File
file.close()

It will generate the key and store it inside the file enc_key.key, which is the extension file of this library. If you want to use this file again for decryption, then it is easy to perform this action:

# Opening The File
file = open("enc_key.key", "rb")

# Reading The File
key = file.read()

# Closing The File
file.close()

# Priniting The Key
print(key)

You will get the password generated before on your screen then you can perform the decryption or encryption if you want to use the same password for your sensitive data.

Conclusion

Thanks for reading! Encryption is used a lot in communication when sending emails, sending your credit card info when you purchase from online stores, when accessing the websites, and more, to name a few. So it will be better to learn it if you are going to be an ethical hacker or a web developer to use it in your web application for securing users’ data.

Scroll to Top