Tech Blog

Coding

Encrypt and Decrypt a message using DES Algorithm in Python

By 42Gears Team3 min read
← Back to blog

What is Cryptography?

Cryptography is that the art of communication between a sender and a receiver using codes in order that only those for whom the knowledge is meant can read and process it.

Terminologies of Cryptography

Plain Text:  it is the text which is readable and can be understood by all users.

Cipher Text: the message obtained after applying cryptography on plain text is Cipher text.

Encryption: the method of converting plain text to cipher text is named encryption. It is also called encoding.

Decryption: the method of converting cipher text to plain text is named decryption. It is also termed decoding.

Why Encryption is important?

  1. For maintaining the security of financial and other Research & Development data with various internal data which needs to be safe from intruders.
  2. The employees or organizations don’t want their financial or private items to be made public.
  3. To make quick transformation so that private items are not made public.

Encryption scrambles text to make it unreadable by others and the security strategy should have the ability to slow down hackers from stealing sensitive information.

Installation Process

Step1: We need to install two libraries i.e., pip install pycrypto for decryption and pip install base32hex for base32 decoding (Note: pycrypto installation works for python 2.7.9 versions.) The PyCrypto package is the most well-known third-party cryptography package for Python. Sadly PyCrypto’s development stopping in 2012. Others have continued to release the newest version of PyCryto so you’ll still catch on for Python 3.5. Alternatively, there’s a fork of the project called PyCrytodome that’s a replacement for PyCrypto.

Do not Install PyCrypto and PyCryptodome at an equivalent time, as they’re going to interfere with one another.

From the command prompt, we need to install the required libraries using the following commands, Mac users can use pip3 instead of pip

pip install pycrypto
pip install base32hex  

Step2: Import the following libraries.

  1. We are using base32hex to encode and decode the string.
  2. Hashlib module is an interface for hashing message. The idea behind this module is to use a hash function on a string, and encrypt in order that it’s very difficult to decrypt it.

The code for Encryption is mentioned below:

import base32hex
import hashlib
from Crypto.Cipher import DES
password = "Password"
salt = '\x28\xAB\xBC\xCD\xDE\xEF\x00\x33'
key = password + salt
m = hashlib.md5(key)
key = m.digest()
(dk, iv) =(key[:8], key[8:])
crypter = DES.new(dk, DES.MODE_CBC, iv)

plain_text= "I see you"

print("The plain text is : ",plain_text)
plain_text += '\x00' * (8 - len(plain_text) % 8)
ciphertext = crypter.encrypt(plain_text)
encode_string= base32hex.b32encode(ciphertext)
print("The encoded string is : ",encode_string)

Output after Encryption

The code for Decryption is mentioned below:

import base32hex
import hashlib
from Crypto.Cipher import DES
password = "Password"
salt = '\x28\xAB\xBC\xCD\xDE\xEF\x00\x33'
key = password + salt
m = hashlib.md5(key)
key = m.digest()
(dk, iv) =(key[:8], key[8:])
crypter = DES.new(dk, DES.MODE_CBC, iv)

encrypted_string='UH562EGM8RCHHTOUC5CTRS59OG======'

print("The ecrypted string is : ",encrypted_string)
encrypted_string=base32hex.b32decode(encrypted_string)
decrypted_string = crypter.decrypt(encrypted_string)
print("The decrypted string is : ",decrypted_string)

Output after Decryption

This is the procedure to decrypt using pycrypto Python Library using salt, password and key.

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →