Cryptography with Python using Fernet

Let us look at how to encrypt text and files using Python.
For this we are going to be using Fernet which is a part of python’s cryptography package

So let us get right into it

Ok firstly we need to downloaded the cryptography package using pip

On W…

Let us look at how to encrypt text and files using Python.
For this we are going to be using Fernet which is a part of python’s cryptography package

So let us get right into it

Ok firstly we need to downloaded the cryptography package using pip

On Windows:

pip install cryptography

On Linux/macOS:

pip3 install cryptography

After we have executed the command in the terminal, we are ready to start coding.

You might want to use your favourite code editor (vscode, sublime text, atom etc…) or any IDE.

Now let us first import the required library

from cryptography.fernet import Fernet

Then we are going to define a function that will write the key to a file

def write_key():
    key = Fernet.generate_key() # Generates the key
    with open("key.key", "wb") as key_file: # Opens the file the key is to be written to
        key_file.write(key) # Writes the key

We will also write down a function that will help with reading and loading the key into a variable

def load_key():
    return open("key.key", "rb").read() #Opens the file, reads and returns the key stored in the file

Now let us take the message we are going to encode as user input and encode it to bytes because that is how fernet works

message = input("Message: ").encode() # Takes the message as user input and encodes it

Now we need to write are top secret key to the key file. We are going to run this only once and then comment the line out because we don’t want it to write a new key down every time the code is run.

write_key() # Writes the key to the key file

Now we need to load the key i.e. read the key from the key file and store it in a variable called as key though you can name it whatever you want

key = load_key() # Loads the key and stores it in a variable

Now we need to initialize the fernet object by passing in the key we just loaded

f = Fernet(key)

Now let us get to the main part, encrypting the message
Also let us print it out

encrypted_message = f.encrypt(message)
print(encrypted_message)

Output:

image.png

You see that the encrypted message cannot be understood by anyone so it is safe now and will be the same unless decrypted with the same key

To decrypt, it is a very similar process

decrypted_message = f.decrypt(encrypred_message)
print(decrypted_message)

Output:

image.png
So here, first the message is encrypted and is printed out and then we decrypt it and print it in the next line. If the initial message and the decrypted message are the exact same, our code has worked!!!

So that was it for this tiny blog (and it was my first blog ever so leave down some feedback!!!)



Video Tutorial:

Link

%[https://www.youtube.com/watch?v=P2k4f1tu-ss]


Print Share Comment Cite Upload Translate
APA
Anish De | Sciencx (2024-03-29T14:21:12+00:00) » Cryptography with Python using Fernet. Retrieved from https://www.scien.cx/2021/04/10/cryptography-with-python-using-fernet/.
MLA
" » Cryptography with Python using Fernet." Anish De | Sciencx - Saturday April 10, 2021, https://www.scien.cx/2021/04/10/cryptography-with-python-using-fernet/
HARVARD
Anish De | Sciencx Saturday April 10, 2021 » Cryptography with Python using Fernet., viewed 2024-03-29T14:21:12+00:00,<https://www.scien.cx/2021/04/10/cryptography-with-python-using-fernet/>
VANCOUVER
Anish De | Sciencx - » Cryptography with Python using Fernet. [Internet]. [Accessed 2024-03-29T14:21:12+00:00]. Available from: https://www.scien.cx/2021/04/10/cryptography-with-python-using-fernet/
CHICAGO
" » Cryptography with Python using Fernet." Anish De | Sciencx - Accessed 2024-03-29T14:21:12+00:00. https://www.scien.cx/2021/04/10/cryptography-with-python-using-fernet/
IEEE
" » Cryptography with Python using Fernet." Anish De | Sciencx [Online]. Available: https://www.scien.cx/2021/04/10/cryptography-with-python-using-fernet/. [Accessed: 2024-03-29T14:21:12+00:00]
rf:citation
» Cryptography with Python using Fernet | Anish De | Sciencx | https://www.scien.cx/2021/04/10/cryptography-with-python-using-fernet/ | 2024-03-29T14:21:12+00:00
https://github.com/addpipe/simple-recorderjs-demo