What is the AES
Advanced Encryption Standard (AES), also known as Rijndael, is a block Encryption Standard adopted by the U.S. federal government. This standard to replace the original DES, has been widely analyzed and used around the world. After a five-year selection process, the Advanced Encryption Standard was published by the National Institute of Standards and Technology (NIST) on FIPS PUB 197 on 26 November 2001 and became a valid standard on 26 May 2002. Now advanced Encryption Standard has become one of the most popular algorithms in symmetric key encryption.
The algorithm was designed by Belgian cryptographers Joan Daemen and Vincent Rijmen, and combined with the names of the two authors to contribute the selection process of advanced encryption standard under the name Rijndael.
The above description is from Wikipedia.
AES features
In the AES standard specification, the packet length can only be 128 bits, that is, each packet is 16 bytes (8 bits per byte)
The length of the secret key
- 128-bit: generally, it is
AES-128
A byte,8 bits
Is the secret key of length16 bytes
, the packet length is16 bytes
Encryption,10 rounds
- 192 bits: generally recorded as
AES-192
A byte,8 bits
Is the secret key of length24 bytes
, the packet length is16 bytes
Encryption,12 rounds
- 256 bits: Generally recorded as
AES-128
A byte,8 bits
Is the secret key of length32 bytes
, the packet length is16 bytes
Encryption,14 rounds
Working mode
Reference: github.com/openssl/ope…
- ECB (Electronic Codebook Book) : In this mode, the entire plaintext is divided into several identical segments, each segment is encrypted, and finally spliced.
- Cipher Block Chaining (CBC) : In this mode, the plaintext is divided into several small segments, and each segment is xOR with the initial Block or the ciphertext segment of the previous segment, and then encrypted with the key.
- Calculator mode CTR (Counter) : ** Calculator mode is not common. In CTR mode, there is an increment operator, which uses the key to encrypt the output and the plaintext xor result to get ciphertext, equivalent to one cipher at a time. This method of encryption is simple, fast, secure, and can be encrypted in parallel, but the key can only be used once if the calculator does not last very long.
- .
Fill mode
- NoPadding: padding with 0 if the data length is not aligned, otherwise NoPadding
- PKCS7Padding: Assume that the data length needs to be filled with n(n>0) bytes to align, then fill n bytes, each byte is N; If the data itself is already aligned, a block of block-sized data is padded, each byte being a block size
- PKCS5Padding: a subset of PKCS7Padding with a fixed block size of 8 bytes.
- .
AES encryption process
The plaintext is first grouped in 16 bytes and then divided into nogan rents. But sometimes what happens when the plain text is not a multiple of 16?
At this point, you can use fill mode to fill the plaintext up to a multiple of 16
Then encrypt according to the selected working mode
The number of rounds of encryption is determined by the length of the key
The Go standard library
In Go, the crypto/ AES standard library is officially provided to give us encryption, the official explanation is like this:
The AES operations in this package are not implemented using constant-time algorithms. An exception is when running on systems with enabled hardware support for AES that makes these operations constant-time. Examples include amd64 systems using AES-NI extensions and s390x systems using Message-Security-Assist extensions. On such systems, when the result of NewCipher is passed to cipher.NewGCM, the GHASH operation used by GCM is also constant-time.
This library does not specify a mode, so we can use cipher to select the encryption mode
The article recommended
Go implements AES encryption in CBC mode