Use python to do AES encryption

The DES encryption algorithm can be developed into a 3DES encryption algorithm, and later upgraded to an AES encryption algorithm, which increases the length of the key and increases the difficulty of brute force cracking.
This time, Python is used to encrypt and decrypt AES, and it is performed under ubuntu:

First´╝î we need to install python and pip:

#sudo apt-get install python
#sudo apt-get install python-pip

Then install

#pip install Ctypto
#pip install binascii

AES has many modes, and this time the CBC mode is adopted: the key and the salt (for scramble) are used to generate the key and iv according to a fixed algorithm (md5). then use key and
iv (initial vector, encrypted first block of plaintext) Encrypt (plaintext) and decrypt (ciphertext).
The idea of the following code implementation: encrypt the encrypted text in units of 8*16 bits, and encrypt each 16-byte data into 16-byte ciphertext.
In the following code, in order to simplify the code, the key and iv generated by the key are replaced with 16-bit keys. In fact, they can be different, but whether the number of digits can be different, I did not try.

#coding: utf8
import sys
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex

class prpcrypt():
def __init__(self, key):
   self.key = key
   self.mode = AES.MODE_CBC

#Encryption function, if text is not a multiple of 16 [encrypted text text must be a multiple of 16! ], then make up to #a multiple of 16

def encrypt(self, text):
   cryptor =, self.mode, self.key)

#The length of the key here must be 16 (AES-128), 24 (AES-192), or 32 (AES-256) Bytes length. Currently AES-128 is #sufficient

length = 16
count = len(text)
if(count % length != 0) :
    add = length - (count % length)
    add = 0
    text = text + ('\0' * add)
    self.ciphertext = cryptor.encrypt(text)

#Because the string obtained during AES encryption is not necessarily in the ascii character set, there may be problems #when outputting to the terminal or saving
#So here we uniformly convert the encrypted string into a hexadecimal string

    return b2a_hex(self.ciphertext)

#After decryption, remove the supplementary spaces and remove them with strip()

def decrypt(self, text):
    cryptor =, self.mode, self.key)
    plain_text = cryptor.decrypt(a2b_hex(text))
    return plain_text.rstrip('\0')

if __name__ == '__main__':
    pc = prpcrypt('keyskeyskeyskeys') #initialize key
    e = pc.encrypt("0123456789ABCDEF")
    d = pc.decrypt(e)
    print e, d
    e = pc.encrypt("00000000000000000000000000")
    d = pc.decrypt(e)
    print e, d


367b61b333c242a4253cfacfe6ea709f 0123456789ABCDEF

In this code, the initial key is keyskeyskeyskeys, which can be understood as an encrypted key. Only through this key can the encrypted data be decrypted.
As can be seen from the results, through AES encryption, we encrypt the string of ‘0123456789ABCDEF’ as 0x367b61b333c242a4253cfacfe6ea709f. The string of 00000000000000000000000000 is encrypted as
0x2c1969f213c703ebedc36f9e7e5a2b88922ac938c983201c200da51073d00b2c, after careful observation, it is found that it is twice the above,
Because the length of the original string is more than 16, but less than 32, in the above algorithm, it is filled into a string of 32 lengths and then encrypted.
From this, it can be extended to the encryption idea of large files: gradually read data of 16 lengths, encrypt every 16 lengths of data, and write the encrypted file until all plaintexts are written.
Data is encrypted.

view code in Github :

Leave a Comment

Your email address will not be published.

Scroll to Top