AES Encryption using Python packages

By | 6th July 2019

AES stands for Advanced Encryption Standard. AES is the first and only publicly accessible cipher approved by the National Security Agency(NSA). There is multiple AES package available in python.

In this post, we will see the implementation with two different packages in python

  1. Method1 – “crypto” package
  2. Method2 – “cryptography” package

Using the “Crypto” AES package

In this method, the python version used is 2.6.6

Install the “crypto” package in your machine. Installation can be done by running “pip install crypto”

Once done, you will be able to run the below program to encrypt and decrypt the text

from Crypto.Cipher import AES

def encrypt(plain_text, iv, key):
    obj = AES.new(key, AES.MODE_CFB, iv)

    enc_text = obj.encrypt(plain_text)
    return enc_text

def decrypt(enc_text, iv, key):
    obj = AES.new(key, AES.MODE_CFB, iv)

    plain_text = obj.decrypt(enc_text)
    return plain_text

if __name__ == "__main__":
    plaintext = "Testing AES encryption/decryption in techieshouts.com"
    iv = "TestMeInitVector"
    key = "YourSampleEncKey"
    print("Plain text: ",plaintext)
    print("Calling encryption library")
    encryptedtext = encrypt(plaintext,iv,key)
    print("Encrypted text")
    print(encryptedtext)
    decryptedtext = decrypt(encryptedtext,iv,key)
    print("Decrypted text")

from Crypto.Cipher import AES

Import AES from the crypto cipher package so that we can use the encrypt and decrypt methods in the code.

obj = AES.new(key, AES.MODE_CFB, iv)

After importing the package, the AES object can be created using the above code. Key and iv are the secret characters that used to encrypt the content. Since these two are the secret elements of encryption, knowing these two will help the programmer to decrypt the encrypted contents.

enc_text = obj.encrypt(plain_text)

In the above line, we are calling the “encrypt” method of the AES package.

Using the “cryptography” package

There is another way to do encryption and decryption using the same AES algorithm. But, this time we will implement the same using the “cryptography ” package.

In this method, the python version used is 2.6.6

Before running the below program, we have to make sure that the “cryptography ” package is installed. If not, please run the pip command in administrator mode and get the package

After installing the “cryptography” package, just execute the below program to see the results.

from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import (Cipher, algorithms, modes)

def encrypt(p_text, iv, key):
    backend = default_backend()

    cipher = Cipher(algorithms.AES(key),modes.CFB(iv),backend=backend)
    padder = padding.PKCS7(128).padder() # 128 bit
    text = padder.update(p_text) + padder.finalize()
    encryptor = cipher.encryptor()
    c_text = encryptor.update(text) + encryptor.finalize()
    return c_text


def decrypt(p_text, iv, key):
    backend = default_backend()

    cipher = Cipher(algorithms.AES(key),modes.CFB(iv),backend=backend)
    padder = padding.PKCS7(128).padder() # 128 bit
    text = padder.update(p_text) + padder.finalize()
    decryptor = cipher.decryptor()
    c_text = decryptor.update(text) + decryptor.finalize()
    return c_text


if __name__ == "__main__":
    plaintext = "Testing AES encryption/decryption in techieshouts.com"
    iv = "TestMeInitVector"
    key = "YourSampleEncKey"
    print("Plain text: ",plaintext)
    print("Calling encryption library")
    encryptedtext = encrypt(plaintext,iv,key)
    print("Encrypted text")
    print(encryptedtext)
    decryptedtext = decrypt(encryptedtext,iv,key)
    print("Decrypted text")

cipher = Cipher(algorithms.AES(key),modes.CFB(iv),backend=backend)

We are creating the cipher object using the above line. While creating the object itself we are passing the iv and key.

Once the cipher object is created, we apply cipher padding and encrypt the given text using the below lines

padder = padding.PKCS7(128).padder() # 128 bit
text = padder.update(p_text) + padder.finalize()
encryptor = cipher.encryptor()
c_text = encryptor.update(text) + encryptor.finalize()

The same technique is used for decrypting the content. But, here, instead of the encryptor, we will use the decryptor method as in below line

cipher.decryptor()

Check the same “AES implementation using Java”

Reference – Advance Encryption Standard – wiki