/  Technology   /  Understanding Data Encryption Using Python
Understanding Data Encryption Using Python 3 (i2tutorials)

Understanding Data Encryption Using Python

In this article, we will discuss data encryption using python. Before that let’s start by understanding what is a data encryption and why do we need it, by taking an example.


Science of Secrecy

Assume Tony and Pepper wants to communicate with each other secretly. Tony wants to send a letter to pepper and he suspects that someone in between will probably tamper the letter and see the secret message written in it.

He wants only pepper to read it, in case someone else gets hold of the letter they can read it but it should not make any sense to them. So to ensure this he needs to encrypt the letter.

One way of doing this is, he first writes the letter and then shifts every single alphabet by seven units, which means A will become I, B will become J, and so on. Then the letter will look something like this.


We will leave for Newyork tonight :-

Em eqtt tmidm nwz Vmegwzs bwvqopb


Now tony sends this letter to pepper and pepper gets this and knows what exactly tony has done. So, to get back to the original letter pepper simply shifts back by seven units. As you can see the secret key here is shifting by seven. This secret key may vary from person to person or letter to letter depending upon the communicating protocol.


Cryptography

Cryptography is the practice and study of technique for securing communication and data in the presence of adversaries. It allows you to securely protect data that you don’t want anyone else to have access to. This technique can be used to protect corporate secrets, secure classified information, and to protect personal information.


Terminologies of Cryptography

Plain Text

The plain text is nothing but the readable file or readable text which is understood by everyone.


Cipher Text

The ciphertext is an unreadable file or unreadable text which is obtained after applying cryptography on plain text.


Encryption

The task of converting plain text to cipher text is called encryption.


Decryption

The task of converting cipher text to plain text is called decryption.


In this article, we will learn how to use python to encrypt files using a cryptography library.

First, we need to install cryptography library, by using

pip install cryptography


Now we can start with our encrypting.

from cryptography.fernet import Fernet

def write_key():
    key = Fernet.generate_key()
    with open('key.key','wb') as file:
        file.write(key)

def load_key():
    return open('key.key','rb').read()


At first, we will need to get a key to encrypt and decrypt with, it is like a password but in a special format. But whenever we generate the key it generates the new key. This is because the generate_key function creates a random key every time when we call it.

Since the new key is generated every time we need to save the key as we can encrypt and decrypt with the same key. If you do not use the same key for encryption and decryption error will occur. That’s why we need to save the key.

For this purpose, we created two functions write_key and load_key which will generate, store, and load the key when called.

write_key()
key=load_key()

note = 'This is secret message'.encode()


The .encode() method will convert the string to byte code to be suitable for encryption.

f = Fernet(key)
encrypted=f.encrypt(note)
print(encrypted)
decrypted = f.decrypt(encrypted)
print(decrypted)


Output:


In this way we can encrypt data using python.

Leave a comment