C++ 实现 AES 加密算法 - 基于密钥的加密
C++ 实现 AES 加密算法 - 基于密钥的加密
本文将使用 C++ 语言实现 AES 加密算法,并提供示例代码。AES 是一种对称分组密码算法,广泛应用于各种加密场景。
AES 加密算法原理
AES 加密算法主要包括以下步骤:
- 密钥扩展 (Key Expansion):将 128 位密钥扩展为 176 位的扩展密钥,用于不同的加密轮次。
- 初始轮 (Initial Round):将明文数据进行初始轮密钥加运算。
- 循环轮次 (Rounds):进行 10 轮加密,每轮包括以下操作:
- 字节替换 (Substitute Bytes):使用 S-box 对每个字节进行替换。
- 行移位 (Shift Rows):对状态矩阵的行进行循环移位。
- 列混淆 (Mix Columns):对状态矩阵的列进行线性变换。
- 轮密钥加 (Add Round Key):将扩展密钥与状态矩阵进行异或运算。
- 最终轮 (Final Round):进行最终轮密钥加运算。
C++ 代码实现
以下是使用 C++ 语言实现 AES 加密算法的示例代码:
#include <iostream>
#include <cstdint>
// AES S-box
uint8_t sbox[256] = {
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
};
// AES round constant
uint8_t rcon[11] = {
0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
};
// AES key expansion
void expandKey(uint8_t* key, uint32_t* expandedKey) {
for (int i = 0; i < 4; i++) {
expandedKey[i] = (key[4 * i] << 24) | (key[4 * i + 1] << 16) | (key[4 * i + 2] << 8) | (key[4 * i + 3]);
}
for (int i = 4; i < 44; i++) {
uint32_t temp = expandedKey[i - 1];
if (i % 4 == 0) {
temp = (sbox[(temp >> 8) & 0xFF] << 24) | (sbox[(temp >> 16) & 0xFF] << 16) | (sbox[(temp >> 24) & 0xFF] << 8) | (sbox[temp & 0xFF]);
temp ^= rcon[i / 4];
}
expandedKey[i] = expandedKey[i - 4] ^ temp;
}
}
// AES substitution bytes
void substituteBytes(uint8_t* state) {
for (int i = 0; i < 16; i++) {
state[i] = sbox[state[i]];
}
}
// AES inverse substitution bytes
void inverseSubstituteBytes(uint8_t* state) {
for (int i = 0; i < 16; i++) {
state[i] = sbox[state[i]];
}
}
// AES shift rows
void shiftRows(uint8_t* state) {
uint8_t temp[16];
for (int i = 0; i < 16; i++) {
temp[i] = state[i];
}
state[0] = temp[0];
state[1] = temp[5];
state[2] = temp[10];
state[3] = temp[15];
state[4] = temp[4];
state[5] = temp[9];
state[6] = temp[14];
state[7] = temp[3];
state[8] = temp[8];
state[9] = temp[13];
state[10] = temp[2];
state[11] = temp[7];
state[12] = temp[12];
state[13] = temp[1];
state[14] = temp[6];
state[15] = temp[11];
}
// AES inverse shift rows
void inverseShiftRows(uint8_t* state) {
uint8_t temp[16];
for (int i = 0; i < 16; i++) {
temp[i] = state[i];
}
state[0] = temp[0];
state[5] = temp[1];
state[10] = temp[2];
state[15] = temp[3];
state[4] = temp[4];
state[9] = temp[5];
state[14] = temp[6];
state[3] = temp[7];
state[8] = temp[8];
state[13] = temp[9];
state[2] = temp[10];
state[7] = temp[11];
state[12] = temp[12];
state[1] = temp[13];
state[6] = temp[14];
state[11] = temp[15];
}
// AES mix columns
void mixColumns(uint8_t* state) {
uint8_t temp[16];
for (int i = 0; i < 16; i++) {
temp[i] = state[i];
}
state[0] = (uint8_t)(mul2[temp[0]] ^ mul3[temp[1]] ^ temp[2] ^ temp[3]);
state[1] = (uint8_t)(temp[0] ^ mul2[temp[1]] ^ mul3[temp[2]] ^ temp[3]);
state[2] = (uint8_t)(temp[0] ^ temp[1] ^ mul2[temp[2]] ^ mul3[temp[3]]);
state[3] = (uint8_t)(mul3[temp[0]] ^ temp[1] ^ temp[2] ^ mul2[temp[3]]);
state[4] = (uint8_t)(mul2[temp[4]] ^ mul3[temp[5]] ^ temp[6] ^ temp[7]);
state[5] = (uint8_t)(temp[4] ^ mul2[temp[5]] ^ mul3[temp[6]] ^ temp[7]);
state[6] = (uint8_t)(temp[4] ^ temp[5] ^ mul2[temp[6]] ^ mul3[temp[7]]);
state[7] = (uint8_t)(mul3[temp[4]] ^ temp[5] ^ temp[6] ^ mul2[temp[7]]);
state[8] = (uint8_t)(mul2[temp[8]] ^ mul3[temp[9]] ^ temp[10] ^ temp[11]);
state[9] = (uint8_t)(temp[8] ^ mul2[temp[9]] ^ mul3[temp[10]] ^ temp[11]);
state[10] = (uint8_t)(temp[8] ^ temp[9] ^ mul2[temp[10]] ^ mul3[temp[11]]);
state[11] = (uint8_t)(mul3[temp[8]] ^ temp[9] ^ temp[10] ^ mul2[temp[11]]);
state[12] = (uint8_t)(mul2[temp[12]] ^ mul3[temp[13]] ^ temp[14] ^ temp[15]);
state[13] = (uint8_t)(temp[12] ^ mul2[temp[13]] ^ mul3[temp[14]] ^ temp[15]);
state[14] = (uint8_t)(temp[12] ^ temp[13] ^ mul2[temp[14]] ^ mul3[temp[15]]);
state[15] = (uint8_t)(mul3[temp[12]] ^ temp[13] ^ temp[14] ^ mul2[temp[15]]);
}
// AES inverse mix columns
void inverseMixColumns(uint8_t* state) {
uint8_t temp[16];
for (int i = 0; i < 16; i++) {
temp[i] = state[i];
}
state[0] = (uint8_t)(mul14[temp[0]] ^ mul11[temp[1]] ^ mul13[temp[2]] ^ mul9[temp[3]]);
state[1] = (uint8_t)(mul9[temp[0]] ^ mul14[temp[1]] ^ mul11[temp[2]] ^ mul13[temp[3]]);
state[2] = (uint8_t)(mul13[temp[0]] ^ mul9[temp[1]] ^ mul14[temp[2]] ^ mul11[temp[3]]);
state[3] = (uint8_t)(mul11[temp[0]] ^ mul13[temp[1]] ^ mul9[temp[2]] ^ mul14[temp[3]]);
state[4] = (uint8_t)(mul14[temp[4]] ^ mul11[temp[5]] ^ mul13[temp[6]] ^ mul9[temp[7]]);
state[5] = (uint8_t)(mul9[temp[4]] ^ mul14[temp[5]] ^ mul11[temp[6]] ^ mul13[temp[7]]);
state[6] = (uint8_t)(mul13[temp[4]] ^ mul9[temp[5]] ^ mul14[temp[6]] ^ mul11[temp[7]]);
state[7] = (uint8_t)(mul11[temp[4]] ^ mul13[temp[5]] ^ mul9[temp[6]] ^ mul14[temp[7]]);
state[8] = (uint8_t)(mul14[temp[8]] ^ mul11[temp[9]] ^ mul13[temp[10]] ^ mul9[temp[11]]);
state[9] = (uint8_t)(mul9[temp[8]] ^ mul14[temp[9]] ^ mul11[temp[10]] ^ mul13[temp[11]]);
state[10] = (uint8_t)(mul13[temp[8]] ^ mul9[temp[9]] ^ mul14[temp[10]] ^ mul11[temp[11]]);
state[11] = (uint8_t)(mul11[temp[8]] ^ mul13[temp[9]] ^ mul9[temp[10]] ^ mul14[temp[11]]);
state[12] = (uint8_t)(mul14[temp[12]] ^ mul11[temp[13]] ^ mul13[temp[14]] ^ mul9[temp[15]]);
state[13] = (uint8_t)(mul9[temp[12]] ^ mul14[temp[13]] ^ mul11[temp[14]] ^ mul13[temp[15]]);
state[14] = (uint8_t)(mul13[temp[12]] ^ mul9[temp[13]] ^ mul14[temp[14]] ^ mul11[temp[15]]);
state[15] = (uint8_t)(mul11[temp[12]] ^ mul13[temp[13]] ^ mul9[temp[14]] ^ mul14[temp[15]]);
}
// AES add round key
void addRoundKey(uint8_t* state, uint32_t* roundKey) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
state[i * 4 + j] ^= (uint8_t)((roundKey[i] >> (24 - j * 8)) & 0xFF);
}
}
}
// AES encrypt
void encrypt(uint8_t* input, uint8_t* key, uint8_t* output) {
uint32_t expandedKey[44];
expandKey(key, expandedKey);
uint8_t state[16];
for (int i = 0; i < 16; i++) {
state[i] = input[i];
}
addRoundKey(state, expandedKey);
for (int round = 1; round <= 10; round++) {
substituteBytes(state);
shiftRows(state);
if (round < 10) {
mixColumns(state);
}
addRoundKey(state, expandedKey + round * 4);
}
for (int i = 0; i < 16; i++) {
output[i] = state[i];
}
}
int main() {
uint8_t input[16] = { 0x32, 0x88, 0x31, 0xe0, 0x43, 0x5a, 0x31, 0x37, 0xf6, 0x30, 0x98, 0x07, 0xa8, 0x8d, 0xa2, 0x34 };
uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
uint8_t output[16];
encrypt(input, key, output);
std::cout << 'Encrypted Output: ';
for (int i = 0; i < 16; i++) {
std::cout << std::hex << std::setfill('0') << std::setw(2) << (int)output[i] << ' ';
}
std::cout << std::endl;
return 0;
}
代码说明
- S-box 和 Round Constant: 定义了 AES 算法中使用的 S-box 和 Round Constant。
- Key Expansion:
expandKey()函数实现了密钥扩展操作,将 128 位密钥扩展为 176 位的扩展密钥。 - Substitute Bytes:
substituteBytes()函数实现了字节替换操作,使用 S-box 对每个字节进行替换。 - Shift Rows:
shiftRows()函数实现了行移位操作,对状态矩阵的行进行循环移位。 - Mix Columns:
mixColumns()函数实现了列混淆操作,对状态矩阵的列进行线性变换。 - Add Round Key:
addRoundKey()函数实现了轮密钥加操作,将扩展密钥与状态矩阵进行异或运算。 - Encrypt:
encrypt()函数实现了 AES 加密操作,将明文数据进行加密并输出密文数据。
总结
本文详细介绍了 AES 加密算法的原理和 C++ 代码实现过程。该代码实现了 AES 加密算法的核心功能,可以用于简单的加密场景。在实际应用中,建议使用成熟的加密库,以确保安全性和效率。
原文地址: https://www.cveoy.top/t/topic/CC5 著作权归作者所有。请勿转载和采集!