# Basic Cryptographic Operations (Encryption and Decryption) in Python

Tags:

Categories:

Updated:

In this post we will take a look at the basic operations on cryptography (encryption and decryption) in python. We have an excellent module pycrypto that offers different built-in symmetric and asymmetric key cryptosystems.

Symmetric key cryptosystems use the same key to encrypt and decrypt a data or file. For example, DES and AES. Now-a-days, AES is the most common symmetric key cryptosystem.

Asymmetric key cryptosystems use two different mathematically related keys: one to encrypt and another to decrypt. For example, RSA and ECC.

Let’s first install the module:

pip install pycrypto


## Symmetric Cryptography

### DES

In the following examples, we will encrypt and decrypt our data using DES.

from Crypto.Cipher import DES

des = DES.new('eightchr', DES.MODE_ECB)
data = 'eightltr'
cipher_text = des.encrypt(data)
print(f"Encrypted Text: {cipher_text}")
print(f"Decrypted Text: {des.decrypt(cipher_text)}")


Here’s our output after running the program:

Encrypted Text: b'<e\x1d\xf2\xb2a\xa9a'
Decrypted Text: b'eightltr'


In the CFB mode, the object keeps changing each step. That’s why we will use seperate object to manage the key.

from Crypto.Cipher import DES
from Crypto import Random

iv = Random.get_random_bytes(8)
des_obj_1 = DES.new('eightchr', DES.MODE_CFB, iv)
des_obj_2 = DES.new('eightchr', DES.MODE_CFB, iv)
data = 'sixteencharacter'      # multiple of eight
cipher_text = des_obj_1.encrypt(data)
print(f"Encrypted Text: {cipher_text}")
print(f"Decrypted Text: {des_obj_2.decrypt(cipher_text)}")


Here is the program output:

Encrypted Text: b'\x92\x9ckk9{3\x0b3\xc8\xef\xb1_\xb0\xd0v'
Decrypted Text: b'sixteencharacter'


### AES

The following example offers encryption and decryption in python.

from os import urandom
from Crypto.Cipher import AES

def AES_encrypt(secret,iv,mode,data):
enc = AES.new(secret, mode, iv)
return data, enc.encrypt(data)

def AES_decrypt(secret,iv,mode,data):
dec = AES.new(secret, mode, iv)
return dec.decrypt(data).decode('utf-8')

if __name__=="__main__":
secret = urandom(16)
iv = urandom(16)
mode = AES.MODE_CBC

msg, encrypted_msg = AES_encrypt(secret, iv, mode, "this is a crypto")
print(encrypted_msg)
print(AES_decrypt(secret, iv, mode, encrypted_msg))


## Asymmetric Cryptography

Let’s create public-private key pair using RSA. Then we will encrypt the data using the public key. And then we will use the private key to decrypt the ciphertext (encrypted data).

import Crypto.Random
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
import binascii

private_key = RSA.generate(1024, random)
public_key = private_key.publickey()

print("Private and Public Keys")
print(binascii.hexlify(private_key.exportKey(format='DER')).decode("ascii"))
print(binascii.hexlify(public_key.exportKey(format='DER')).decode("ascii"))

print("\n")
data = "this is a test"
cipher_text = public_key.encrypt(data.encode(), 32)
print(f"Encrypted Text: {cipher_text}")
print(f"Decrypted Text: {private_key.decrypt(cipher_text)}")

Private and Public Keys