Cryptographic Algorithms Interpretation and Implementation using Python

Praveen Sujanmulk
Analytics Vidhya
Published in
5 min readFeb 5, 2021

--

Algorithms: AES, RSA, MD5, SHA256

In this digital era there is a huge amount of data processed everyday. Here comes the data security. The security may be in terms of communication layer or storage layer or anywhere. The cryptography is the process of converting data from one form to another in which only one with access read or modify it.

Basically the data is transferred from one form to another and can be revert back to original is said to be Encryption and Decryption. If data which is transformed from one form to another and no way to revert back to original is said to be Hashing.

Encoding and Decoding

Encoding: It is the process of transferring the basic letters, alphabets, special characters, etc. into special format based on type of encoding technique.
Decoding: It is the process of converting encoded data to original data.

Encoding or Decoding does not need any Keys.

Encryption and Decryption

Encryption: It is the process of converting the plain data into cipher data using keys.
Decryption: it is the process of converting the cipher data to plain data using keys.

Here the KEYS represents the Public key and Private Key.

Public key: It is the key that can be shared with anyone.
Private Key: It is the key that cannot shared with others said to personal key.

There are two types of encryption algorithms: Symmetric and Asymmetric

Symmetric Algorithms

These algorithms perform both encryption using single key that is the Private Key.

AES Implementation with Python Code

Import Libraries

from Crypto.Cipher import AES
from base64 import b64encode, b64decode
from Crypto import Random

Message, Private key

plain_text=”hi this is the message”
private_key=”privatekey123456"

Encrypt the Data

iv = Random.new().read(AES.block_size)
cipher = AES.new(private_key.encode(“utf8”), AES.MODE_CFB, iv)
encrypted_text=cipher.encrypt(plain_text.encode(“utf8”))

Decrypt the Data

cipher = AES.new(private_key.encode(“utf8”), AES.MODE_CFB, iv)
decrypted_text=cipher.decrypt(encrypted_text).decode(“utf8”)

Here, AES algorithm is used to Encrypt and Decrypt the data. the plain text is encrypted using private key “privatekey123456”. The encrypted message is decrypted using the same private key.

Asymmetric Algorithms

These algorithms perform encryption and decryption using pair of keys that is public key and private key.

Scenario1 : Encryption with public key and Decryption with private key
Example: RSA

RSA Implementation with python

Import libraries

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto import Random

Public key, private key generation

random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
private_key, public_key = key, key.publickey()

Encrypt the data

plain_text=”this is the message”
cipher = PKCS1_OAEP.new(public_key)
encrypted_text=cipher.encrypt(plain_text.encode(“utf8”))
encrypted_text

Decrypt the data

cipher = PKCS1_OAEP.new(private_key)
decrypted_text =cipher.decrypt(encrypted_text).decode(“utf8”)
decrypted_text

In this scenario, RSA algorithm is used to perform encryption and decryption. The data is encrypted using the public key and decrypted using the private. If someone in the network stolen the data which is encrypted he/she can’t decrypt it as there is no private key. These type of algorithms mostly used to transfer information over a network.

Scenario2 : Encryption with private key and Decryption with public key

This scenario is quite opposite to scenario 1 where encryption is performed using the private key and decryption is performed using public key. you will wondering how secure it is!
This type of encryption is mostly used to generate digital signatures.

Digital signature generation

This architecture illustrates how the document is signed digitally. consider Alice wants to sign a document send by Bob. Alice will share his public key with Bob as it is sharable key. Alice will hash the Document using the hashing algorithm and encrypt the hashed document (digested document) with his private key. The encrypted message will be sent Bob. Now, Bob will decrypt message using Alice’s public key and verify the Alice’s hashed document with the Bob’s hashed document. If both hash (message digest) are same it is a valid and digitally signed document.

Hashing

It is a one-way process where the message is only encrypted and there is no way to decrypt it. Here the encrypted message is called as message digest.

Example: MD5, SHA

MD5 Python Code

import hashlib 
plain_text=”this is confidential message”
hashlib.md5(plain_text.encode(“utf8”)).digest()

SHA 256 Python code

Thus we can perform AES, RSA, MD5, SHA256, Digital signature Algorithms using Python.

Thanks and Regards
Praveen Sujanmulk

--

--