1. Basic Concepts
- Symmetric encryption:
Symmetric encryption is an encryption method that uses a single-key cryptosystem, in which the same key is used both to encrypt and to decrypt the information. Because it is fast, it is often used to encrypt the transmission of large amounts of data. - DES (Data Encryption Standard):
The key length of DES is 56 bits, and the theoretical security strength of the algorithm is \( 2^{56} \). As computer processing power has increased, DES can no longer provide sufficient security. - AES (Advanced Encryption Standard):
In January 1997, the U.S. National Institute of Standards and Technology began soliciting proposals for an Advanced Encryption Standard, and drew responses from many scholars. After rounds of screening and performance testing, the Rijndael algorithm was finally selected. The algorithm was designed by the Belgian cryptographers Joan Daemen and Vincent Rijmen.
2. Basic Principles
Below is an animated diagram of the encryption process.

The AES encryption algorithm involves 4 operations.
- SubBytes:
The main function of byte substitution is to complete a mapping from one byte to another
- SubBytes:
- ShiftRows:
ShiftRows is a permutation between bytes within a 4x4 matrix, used to provide diffusion for the algorithm.
- ShiftRows:
- MixColumns:
MixColumns is a substitution that exploits the arithmetic properties of the GF(\( 2^{8} \)) field, and is likewise used to provide diffusion for the algorithm.
- MixColumns:
- AddRoundKey:
Every byte in the matrix is XORed with the round key for that round; each subkey is produced by the key generation scheme.
- AddRoundKey:
Below is an illustration of the AES encryption algorithm:

3. Modes of Operation
Block ciphers perform encryption and decryption according to the block size — for example, the block size of the DES algorithm is 64 bits, while that of AES is 128 bits — but the actual length of the plaintext is generally far greater than the block size. How is such a case handled?
What a mode of operation specifies is exactly these questions: how large a block the plaintext data stream is split into, and how to handle data that does not align.
The main modes of operation:
- Electronic Code Book Mode (ECB):
ECB mode merely splits the plaintext by block size and then normally encrypts the resulting plaintext blocks with the same key. The ideal use case for ECB is the encryption of short data (such as an encryption key). The problem with this mode is that it cannot hide the patterns of the original plaintext data, because identical plaintext blocks yield identical ciphertext when encrypted. - Cipher Block Chaining Mode (CBC): introduces the concept of the IV (Initialization Vector). Compared with ECB, CBC mode achieves better pattern hiding, but because it brings the ciphertext into the computation, encryption and decryption operations cannot be performed in parallel. In addition, the IV it introduces must also be known to both the encrypting and the decrypting party.
- Cipher Feedback Mode (CFB): similar to CBC mode, but the difference is that CFB mode first generates a keystream dictionary and then XORs the cipher dictionary with the plaintext to finally produce the ciphertext. Generating the cipher dictionary for the next block requires the ciphertext of the previous block to take part in the computation.
- Output Feedback Mode (OFB): the difference between OFB mode and CFB mode is that the plaintext takes part in the computation when generating the dictionary, whereas CFB uses the ciphertext.
- Counter Mode (CTR): CTR mode likewise generates a stream-cipher dictionary, but at the same time it introduces a counter, to guarantee that no repeated output is produced no matter how long it runs.
4. Padding
The purpose of padding is to extend the length of plain text to the required length before encryption. ECB and CBC require padding, that is, the encrypted length may differ; CFB, OFB, and CTR do not require padding, and the ciphertext length is the same as the plaintext length. The main padding modes are PKCS7, ANSIX923, ISO10126, NoPadding, and ZeroPadding.
- PKCS7: the padding string consists of a sequence of bytes, and each byte is filled with the length of that byte sequence
- ANSIX923: the padding string contains a byte sequence padded with zeros
- ISO10126: the padding string contains random data of the length
- NoPadding: no padding is done
- ZeroPadding: the padding string consists of bytes set to zero
Example:
数据︰ FF FF FF FF FF FF FF FF FF PKCS7 填充︰ FF FF FF FF FF FF FF FF FF 07 07
07 07-07-07 07 ANSIX923填充︰ FF FF FF FF FF FF FF FF FF 00 00 00 00 00 00 07
ISO10126 填充︰ FF FF FF FF FF FF FF FF FF 7 D 2A 75 EF F8 EF 07 ZeroPadding
填充︰ FF FF FF FF FF FF FF FF FF 00 00 00 00 00 00 00
5. Frontend AES Encryption - JavaScript
| |
6. Backend AES Encryption - Python
- Install the PyCrypto library
To use AES encryption in Python, you only need to install the PyCrypto library.
| |
- Generate a Key
The optional length of an AES key must be 16 bytes, 24 bytes, or 32 bytes.
| |
- AES encryption function prototype
| |
- key: the initial key. According to the AES specification, it can be 16 bytes, 24 bytes, or 32 bytes long, corresponding to 128 bits, 192 bits, and 256 bits respectively
- mode: the encryption mode. You can look up the relevant documentation to learn about it. The examples that follow use CBC mode, so a brief explanation is given here: in CBC mode, the plaintext is first split into several small segments, then each small segment is XORed with the initial block or the ciphertext segment of the previous segment, and only then encrypted with the key
- iv: the initialization vector. It is needed in some encryption modes; for CBC mode, for instance, it must be chosen randomly and must be kept secret; moreover, its length is the same as the block size (the AES block length is fixed at 16 bytes)
| |
Output:
| |
