Implementing Encryption and Decryption of Data in Python – Analytics India Magazine

Cryptography is a process which is mainly used for safe and secure communication. It works on different mathematical concepts and algorithms to transfer the encoded data into a secret code which is difficult to decode. It involves the process of encrypting and decrypting the data, for eg. If I need to send my personal details to someone over mail, I can convert the information using Encryption techniques and send it, on the other hand, the receiver will decrypt the information received using Decryption Techniques and there will be no data tampering in between.

The ciphertext is a data or text which is encrypted into a secret code using a mathematical algorithm, it can be deciphered using different mathematical Algorithms. Encryption is converting the text into a secret message, technically known as converting the plaintext to ciphertext and Decryption is converting the ciphertext back to plaintext so that only the authorized users can decipher and use the data. Generally, it uses a key that is known to both the sender and the receiver so that they can cipher and decipher the text.

Python has the following modules/libraries which are used for cryptography namely:

In this article, we will be exploring:

Cryptography is a python package that is helpful in Encrypting and Decrypting the data in python. It provides cryptographic recipes to python developers.

Let us explore Cryptography and see how to encrypt and decrypt data using it.

Implementation:

We first need to install the library using pip install cryptography.

a. Importing the library

Fernet function is used for encryption and decryption in Cryptography. Let us import the Fernet function from the library.

b. Generating the Key

Cryptography works on authentication for which we will need to generate a key. Lets define a function to generate a key and write it to a file. This function will create a key file where our generated key will be stored.

This function will create a pass.key file in your directory as shown in the image below.

c. Loading the Key

The key generated above is a unique key and it will be used further for all encryption and decryption processes so In order to call this key, again and again, let us define a function to load the key whenever required.

d. Encrypting the Data

The next step would be passing the message you want to encrypt in the encode function, initializing the Fernet class, and encrypt the data using encrypt function.

As you can see we have successfully encrypted the data.

e. Decryption of Data

The message will be decrypted with the same key that we used to encrypt it and by using the function decrypt. Let us decode the encrypted message.

As you can see here we have successfully decoded the message. While using cryptography it is necessary to keep the Key file safe and secure to decode the message because if the Key is misplaced the message/data will not be decoded.

Similarly, Cryptography module can be used to convert data/text files, we just need to pass the file to the argument and encode and decode it.

It is a python module which is fast and converts the plaintext to ciphertext and ciphertext to plain text in seconds and with just a single line of code.

Implementation:

We first need to install the library using, pip install simple-crypt

a. Loading the Library

b. Encrypting and Decrypting

Simple-crypt has two pre-defined functions encrypt and decrypt which controls the process of encryption and decryption. For encryption, we need to call the encrypt function and pass the key and message to be encrypted.

Similarly, we can call the decrypt function and decode the original message from this ciphertext.

Here you can see that we have used AIM as the password and it is the same for encryption and decryption.

In simple-crypt, we should keep in mind that the same key should be provided for encryption and decryption otherwise messages will not be decoded back to original.

Hashlib is an open-source python library used for encoding and it contains most of the popular hashing algorithms used by big tech firms for security purposes.Hash is a function that takes variable length as an input and gives the fixed-length output sequence.Unlike the modules discussed earlier in Hashlib decoding is a very difficult and time-consuming job this is why Hashing is considered as the most secure and safe encoding.

Home Implementing Encryption and Decryption of Data in Python

The Hashlib functions that we will be exploring are MD5 and SHA1

MD5 Algorithm/Function produces a hash value which is 128 bit. It converts the strings to bytes so that it is accepted by hash. MD5 is mainly used for checking Data Integrity. It is predefined in hashlib.

Implementation:

We need to install the hashlib library to use MD5 using, pip install hashlib

a. Importing the library

b. Encrypting the data

In order to encrypt the data, we need to pass the message/data to the MD5 function to convert it into bytes. Here you will see that we will type b before typing the message because it converts the string to bytes so that it will be accepted by hash. The hexdigest function will encode the message and return the encoded message as a HEX string.

If we do not want the message to be encoded in HEX string and show it in a sequence of bytes then we will use the digest function.

Secure Hash Algorithms are more secured than MD5. It s a set of the algorithm like SHA1, SHA256, etc. It is widely used for cryptographic applications.

We have already imported the hashlib library so we will directly Encode the message/data using SHA1.

Encryption of Data

In order to encrypt the data, we need to pass the message/data to the SHA1 function to convert it into bytes. Similar to MD5 here also you will see that we will type b before typing the message because it converts the string to bytes so that it will be accepted by hash. The hexdigest function will encode the message and return the encoded message as a HEX string.

Similar to MD5 if we do not want the message to be encoded in HEX string and show it in a sequence of bytes then we will use the digest function.

Similarly, we can try different hashing algorithms for Encoding/Encryption.

In this article, we went through:

comments

Read the original here:
Implementing Encryption and Decryption of Data in Python - Analytics India Magazine

Related Posts
This entry was posted in $1$s. Bookmark the permalink.