写一个C语言额AES加密函数
时间: 2024-05-16 18:07:31 浏览: 142
好的,我可以帮您写一个C语言的AES加密函数,以下是代码:
```
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define AES_BLOCK_SIZE 16
typedef struct {
uint32_t rk[60];
int nr;
} aes_key_t;
static const uint8_t sbox[256] = {
99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118,
202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192,
183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21,
4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117,
9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132,
83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207,
208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168,
81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210,
205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115,
96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219,
224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121,
231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8,
186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138,
112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158,
225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223,
140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22,
};
static const uint8_t rcon[11] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36, 0x6C };
static void aes_sub_bytes(uint8_t* state) {
uint32_t i;
for (i = 0; i < AES_BLOCK_SIZE; ++i) {
state[i] = sbox[state[i]];
}
}
static void aes_shift_rows(uint8_t* state) {
uint32_t tmp;
// Row 0 does not change
// Shift row 1
tmp = state[1];
state[1] = state[5];
state[5] = state[9];
state[9] = state[13];
state[13] = tmp;
// Shift row 2
tmp = state[2];
state[2] = state[10];
state[10] = tmp;
tmp = state[6];
state[6] = state[14];
state[14] = tmp;
// Shift row 3
tmp = state[15];
state[15] = state[11];
state[11] = state[7];
state[7] = state[3];
state[3] = tmp;
}
static uint8_t gfmul(uint8_t a, uint8_t b) {
uint32_t sum = 0;
while (b) {
if (b & 1) {
sum ^= a;
}
a <<= 1;
b >>= 1;
if (a & 0x100) {
a ^= 0x11B;
}
}
return (uint8_t)sum;
}
static void mix_column(uint8_t* col) {
uint8_t tmp[4];
uint32_t i;
for (i = 0; i < 4; ++i) {
tmp[i] = col[i];
}
col[0] = gfmul(tmp[0], 2) ^ gfmul(tmp[3], 1) ^ gfmul(tmp[2], 1) ^ gfmul(tmp[1], 3);
col[1] = gfmul(tmp[1], 2) ^ gfmul(tmp[0], 1) ^ gfmul(tmp[3], 1) ^ gfmul(tmp[2], 3);
col[2] = gfmul(tmp[2], 2) ^ gfmul(tmp[1], 1) ^ gfmul(tmp[0], 1) ^ gfmul(tmp[3], 3);
col[3] = gfmul(tmp[3], 2) ^ gfmul(tmp[2], 1) ^ gfmul(tmp[1], 1) ^ gfmul(tmp[0], 3);
}
static void aes_mix_columns(uint8_t* state) {
uint32_t i;
for (i = 0; i < 4; ++i) {
mix_column(&state[i * 4]);
}
}
static void aes_add_round_key(uint8_t* state, const uint32_t* key) {
uint32_t i, j;
for (i = 0; i < 4; ++i) {
j = i * 4;
state[j] ^= (uint8_t)(key[i] >> 24);
state[j + 1] ^= (uint8_t)(key[i] >> 16);
state[j + 2] ^= (uint8_t)(key[i] >> 8);
state[j + 3] ^= (uint8_t)key[i];
}
}
static void aes_generate_key(const uint8_t* key, aes_key_t* aes_key) {
uint32_t i, j, k;
uint32_t temp;
uint32_t* rk;
const uint32_t* rc;
const uint32_t* rcp;
rk = aes_key->rk;
aes_key->nr = 10;
for (i = 0; i < 4; ++i) {
rk[i] =
(uint32_t)((key[4 * i] << 24) | (key[4 * i + 1] << 16) | (key[4 * i + 2] << 8) | key[4 * i + 3]);
}
rc = rcon;
rcp = rc++;
// Generate the next 44 round keys
for (i = 4; i < 44; ++i) {
temp = rk[i - 1];
if (i % 4 == 0) {
temp = ((uint32_t)sbox[temp & 0xFF]) |
((uint32_t)sbox[(temp >> 8) & 0xFF] << 8) |
((uint32_t)sbox[(temp >> 16) & 0xFF] << 16) |
((uint32_t)sbox[(temp >> 24) & 0xFF] << 24);
temp ^= (*rcp << 24);
rcp = (++rcp == rcon + 10) ? rcon : rcp;
}
if (i % 4 == 1) {
temp = ((uint32_t)sbox[temp & 0xFF] << 8) |
((uint32_t)sbox[(temp >> 8) & 0xFF] << 16) |
((uint32_t)sbox[(temp >> 16) & 0xFF] << 24) |
((uint32_t)sbox[(temp >> 24) & 0xFF]);
}
rk[i] = rk[i - 4] ^ temp;
}
}
static void aes_encrypt(const uint8_t* in, uint8_t* out, const aes_key_t* aes_key) {
uint32_t i, j;
uint8_t state[AES_BLOCK_SIZE];
const uint32_t* rk;
memcpy(state, in, AES_BLOCK_SIZE);
rk = aes_key->rk;
aes_add_round_key(state, rk);
for (i = 1; i < aes_key->nr; ++i) {
aes_sub_bytes(state);
aes_shift_rows(state);
aes_mix_columns(state);
rk += 4;
aes_add_round_key(state, rk);
}
aes_sub_bytes(state);
aes_shift_rows(state);
rk += 4;
aes_add_round_key(state, rk);
memcpy(out, state, AES_BLOCK_SIZE);
}
void aes_cbc_encrypt(const uint8_t* iv, const uint8_t* in, uint8_t* out, uint32_t len, const uint8_t* key) {
aes_key_t aes_key;
uint8_t buf[AES_BLOCK_SIZE], prev[AES_BLOCK_SIZE];
uint32_t i, j;
const uint8_t* p;
aes_generate_key(key, &aes_key);
// Encrypt the first block with the IV
for (i = 0; i < AES_BLOCK_SIZE; ++i) {
buf[i] = prev[i] = in[i] ^ iv[i];
}
aes_encrypt(buf, buf, &aes_key);
for (i = 0; i < AES_BLOCK_SIZE; ++i) {
out[i] = buf[i];
}
// Encrypt the remaining blocks with CBC
for (p = in + AES_BLOCK_SIZE, i = AES_BLOCK_SIZE; i < len; p += AES_BLOCK_SIZE, i += AES_BLOCK_SIZE) {
for (j = 0; j < AES_BLOCK_SIZE; ++j) {
buf[j] = p[j] ^ prev[j];
}
aes_encrypt(buf, buf, &aes_key);
for (j = 0; j < AES_BLOCK_SIZE; ++j) {
prev[j] = buf[j];
out[i + j] = buf[j];
}
}
}
int main() {
uint8_t key[AES_BLOCK_SIZE] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
uint8_t in[AES_BLOCK_SIZE] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
uint8_t iv[AES_BLOCK_SIZE] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
uint8_t out[AES_BLOCK_SIZE];
aes_cbc_encrypt(iv, in, out, AES_BLOCK_SIZE, key);
for (int i = 0; i < AES_BLOCK_SIZE; i++) {
printf("%02x ", out[i]);
}
return 0;
}
```
阅读全文