Implementation of AES Encryption and Decryption of Python

  • 2021-07-13 05:32:53
  • OfStack

Advanced Encryption Standard (English: Advanced Encryption Standard, abbreviation: AES), also known as Rijndael encryption in cryptography, is a block encryption standard adopted by the US federal government. This standard is used to replace the original DES, which has been analyzed by many parties and widely used all over the world. After a five-year selection process, the Advanced Encryption Standard was published in FIPS PUB 197 by the National Institute of Standards and Technology (NIST) on November 26, 2001, and became an effective standard on May 26, 2002. In 2006, Advanced Encryption Standard has become one of the most popular algorithms in symmetric key encryption. -Baidu Encyclopedia

There are five encryption methods for AES: ECB, CBC, CTR, CFB and OFB

1. ECB mode (electronic codebook mode: Electronic codebook)

ECB is the simplest block cipher encryption mode. Before encryption, it is divided into several blocks according to the size of encrypted blocks (for example, AES is 128 bits), and then each block is encrypted separately with the same key. The same is true for decryption.

2. CBC Mode (Cipher Block Link: Cipher-block chaining)

In CBC mode, each cipher block to be encrypted will be exclusive OR with the cipher text of the previous cipher block before encryption, and then encrypted with an encryptor. The first plaintext block is exclusive OR with a data block called initialization vector.

3. CFB mode (ciphertext feedback: Cipher feedback)

Unlike ECB and CBC modes, which can only encrypt block data, CFB can convert block ciphertext (Block Cipher) into stream ciphertext (Stream Cipher).

4. OFB Mode (Output Feedback: Output feedback)

In OFB, a block encryptor is used to generate a key stream (Keystream), and then the key stream and plaintext stream are XOR to obtain a ciphertext stream. Decryption is to generate a key stream with a block encryptor, and then XOR the key stream and ciphertext stream to obtain plaintext. Because of the symmetry of XOR operation, the encryption and decryption processes are completely identical.

CBC encryption method is recommended from the security point of view. This paper introduces the python implementation of CBC and ECB encryption methods

When python uses AES under Windows, the pycryptodome module pip install pycryptodome is installed

python When using AES under Linux, the pycrypto module pip install pycrypto is installed

CBC encryption requires a 106-bit key (key) and a 106-bit iv (offset)

ECB encryption does not require iv

python Implementation of AES CBC Encryption


from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex


#  If text Insufficient 16 The multiple of bits is made up with spaces as 16 Bit 
def add_to_16(text):
 if len(text.encode('utf-8')) % 16:
  add = 16 - (len(text.encode('utf-8')) % 16)
 else:
  add = 0
 text = text + ('\0' * add)
 return text.encode('utf-8')


#  Encryption function 
def encrypt(text):
 key = '9999999999999999'.encode('utf-8')
 mode = AES.MODE_CBC
 iv = b'qqqqqqqqqqqqqqqq'
 text = add_to_16(text)
 cryptos = AES.new(key, mode, iv)
 cipher_text = cryptos.encrypt(text)
 #  Because AES The encrypted string does not 1 It must be ascii Character set, output saving may have problems, so here it is changed to 16 Binary string 
 return b2a_hex(cipher_text)


#  After decryption, remove the complemented spaces and use strip()  Remove 
def decrypt(text):
 key = '9999999999999999'.encode('utf-8')
 iv = b'qqqqqqqqqqqqqqqq'
 mode = AES.MODE_CBC
 cryptos = AES.new(key, mode, iv)
 plain_text = cryptos.decrypt(a2b_hex(text))
 return bytes.decode(plain_text).rstrip('\0')


if __name__ == '__main__':
 e = encrypt("hello world") #  Encryption 
 d = decrypt(e) #  Decryption 
 print(" Encryption :", e)
 print(" Decryption :", d)

python Implementation of AES ECB Encryption


"""
ECB No Offset 
"""
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex


def add_to_16(text):
 if len(text.encode('utf-8')) % 16:
  add = 16 - (len(text.encode('utf-8')) % 16)
 else:
  add = 0
 text = text + ('\0' * add)
 return text.encode('utf-8')


#  Encryption function 
def encrypt(text):
 key = '9999999999999999'.encode('utf-8')
 mode = AES.MODE_ECB
 text = add_to_16(text)
 cryptos = AES.new(key, mode)

 cipher_text = cryptos.encrypt(text)
 return b2a_hex(cipher_text)


#  After decryption, remove the complemented spaces and use strip()  Remove 
def decrypt(text):
 key = '9999999999999999'.encode('utf-8')
 mode = AES.MODE_ECB
 cryptor = AES.new(key, mode)
 plain_text = cryptor.decrypt(a2b_hex(text))
 return bytes.decode(plain_text).rstrip('\0')


if __name__ == '__main__':
 e = encrypt("hello world") #  Encryption 
 d = decrypt(e) #  Decryption 
 print(" Encryption :", e)
 print(" Decryption :", d)

Related articles: