2. AES加密算法C语言实现函数说明: aes算法相关接口都在aes.h内,主要有以下几个接口: 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变量,创建扩展密钥所需要的内存
key_size:扩展密钥所需内存大小
作用:创建扩展密钥
key:扩展密钥
w:扩展密钥内存首地址
作用:数据解密
in:输入加密的信息
out:输出解密后的信息
w:扩展密钥内存首地址
作用:数据加密
in:输入未加密的信息
out:输出加密后的信息
w:扩展密钥内存首地址 示例: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;
}
|