This article only describes the use of the AES algorithm without going into too much detail about its principles. Readers who want to understand its principles can search online.
1. Introduction to AES
Symmetric Encryption Algorithm:
-
The keys used for encryption and decryption are the same. This encryption method is very fast and suitable for scenarios where data is frequently sent. The drawback is that key transmission can be cumbersome.
Asymmetric Encryption Algorithm:
-
The keys used for encryption and decryption are different. This encryption method is based on mathematically difficult problems, and the speed of encryption and decryption is usually slower, making it suitable for occasional data transmission. The advantage is that key transmission is convenient. Common asymmetric encryption algorithms include RSA, ECC, and EIGamal.
Flowchart of AES Encryption Algorithm:
Plaintext P:
The original data that has not been encrypted.
Key K:
A password used to encrypt the original data. In symmetric algorithms, the keys required for both encryption and decryption are the same.
This key cannot be transmitted directly, otherwise it would lead to key leakage; the conventional approach is to encrypt the key using an asymmetric algorithm before transmission.
AES Encryption Function:
The encryption function processes the input key K and plaintext P to generate an encrypted ciphertext C.
Ciphertext C:
Data that has been encrypted with the key.
AES Decryption Function:
2. Implementation of AES Encryption Algorithm in C Language
All related interfaces for the AES algorithm are in aes.h, mainly including the following interfaces:
uint8_t *aes_init(size_t key_size);
void aes_key_expansion(uint8_t *key, uint8_t *w);
void aes_inv_cipher(uint8_t *in, uint8_t *out, uint8_t *w);
void aes_cipher(uint8_t *in, uint8_t *out, uint8_t *w);
-
aes_init() Function
Function: Initializes AES variables and creates memory needed for key expansion. key_size: Memory size required for the expanded key.
-
aes_key_expansion()
Function: Creates the expanded key. key: The expanded key. w: The memory address of the expanded key.
-
aes_inv_cipher()
Function: Data decryption. in: Input encrypted information. out: Output decrypted information. w: Memory address of the expanded key.
-
aes_cipher()
Function: Data encryption. in: Input unencrypted information. out: Output encrypted information. w: Memory address of the expanded key.
Example:
int main() {
uint8_t i = 0;
/* 256 bit key */
uint8_t key[] = {
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b,
0x1c, 0x1d, 0x1e, 0x1f};
uint8_t in[] = {
0x00, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb,
0xcc, 0xdd, 0xee, 0xff};
uint8_t out[16];
uint8_t *w = NULL;
w = aes_init(sizeof(key));
aes_key_expansion(key, w);
printf("Plaintext message:\n");
for (i = 0; i < 4; i++) {
printf("%02x %02x %02x %02x ", in[4*i+0], in[4*i+1], in[4*i+2], in[4*i+3]);
}
printf("\n");
aes_cipher(in, out, w);
printf("Ciphered message:\n");
for (i = 0; i < 4; i++) {
printf("%02x %02x %02x %02x ", out[4*i+0], out[4*i+1], out[4*i+2], out[4*i+3]);
}
printf("\n");
aes_inv_cipher(out, in, w);
printf("Original message (after inv cipher):\n");
for (i = 0; i < 4; i++) {
printf("%02x %02x %02x %02x ", in[4*i+0], in[4*i+1], in[4*i+2], in[4*i+3]);
}
printf("\n");
free(w);
return 0;
}

END