python implementation of AES bidirectional symmetric encryption and decryption and usage analysis

  • 2020-05-30 20:30:32
  • OfStack

This article illustrates the implementation and usage of AES bidirectional symmetric encryption and decryption by python. I will share it with you for your reference as follows:

Advanced encryption standard (Advanced Encryption Standard, AES), also known as Rijndael encryption in cryptography, is a block encryption standard adopted by the United States federal government. This standard, which replaces the original DES, has been analyzed by multiple parties and is widely used around the world. After a 5-year selection process, the advanced encryption standard was issued by the U.S. national institute of standards and technology (NIST) on FIPS PUB 197 on November 26, 2001, and became an effective standard on May 26, 2002. In 2006, advanced encryption standards became one of the most popular algorithms for symmetric key encryption.

AES is just a basic algorithm. There are several patterns to implement AES. The CBC mode is used by TLS (the encryption standard of https) and IPSec (the encryption standard of win) for its security. Simply put, CBC USES a password and salt (which is disruptive) to produce key and iv using a fixed algorithm (md5). It then encrypts (plaintext) and decrypts (ciphertext) with key and iv (initial vector, encryption block 1 plaintext).

The following is an example of encryption and decryption of AES implemented by python. Here, CBC mode is adopted and pycrypto‎ is used. The module


pip install Crypto
pip install binascii


#!/usr/bin/env python
# -*- coding:utf-8 -*-
#@author: rui.xu
# Used here pycrypto‎ library 
# According to the method :easy_install pycrypto‎
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 insufficient 16 The bit is complemented by a space 16 position 
  # If more than 16 At that time is not 16 A multiple of theta, so let's make it up to theta 16 Multiples. 
  def encrypt(self,text):
    cryptor =,self.mode,b'0000000000000000')
    # Here is the key key  The length must be 16 ( AES-128 ) ,
    #24 ( AES-192 ) , or 32  ( AES-256 ) Bytes  The length of the 
    # At present AES-128  Sufficient for current use 
    length = 16
    count = len(text)
    if count < length:
      add = (length-count)
      #\0 backspace
      text = text + ('\0' * add)
    elif count > length:
      add = (length-(count % length))
      text = text + ('\0' * add)
    self.ciphertext = cryptor.encrypt(text)
    # because AES The string you get when you encrypt it is not 1 It is ascii Character set, output to terminal or save may have problems 
    # So here 1 Converts the encrypted string to 16 Base string 
    return b2a_hex(self.ciphertext)
  # After decryption, remove the blank complement strip()  To get rid of 
  def decrypt(self,text):
    cryptor =,self.mode,b'0000000000000000')
    plain_text = cryptor.decrypt(a2b_hex(text))
    return plain_text.rstrip('\0')
if __name__ == '__main__':
  pc = prpcrypt('keyskeyskeyskeys') # Initialization key 
  import sys
  e = pc.encrypt(sys.argv[1]) # encryption 
  d = pc.decrypt(e) # decryption 
  print " encryption :",e
  print " decryption :",d

ValueError: IV must be 16 bytes long windows will report this error by default,

cryptor =,self.mode,b'0000000000000000')

I'm going to instantiate it and then I'm going to add Ok to it

PS: about encryption and decryption interested friends can also refer to the website online tools:

MD5 online encryption tool:

Thunderbolt, express and whirlwind encryption/decryption tools:

Online hash/hash algorithm encryption tool:

MD5/hash/ SHA-1 / SHA-2 / SHA-256 / SHA-512 / SHA-3 / RIPEMD-160 encryption tools:

Online sha1 sha224 / sha256 sha384 / sha512 encryption tools:

More about Python related topics: interested readers to view this site "Python coding skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article is helpful to you in Python programming.

Related articles: