用C语言写一个AES-CFB加密程序
时间: 2023-03-25 10:04:44 浏览: 335
用c语言编写的AES代码
4星 · 用户满意度95%
好的,我可以回答这个问题。以下是一个用C语言编写的AES-CFB加密程序的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#define AES_BLOCK_SIZE 16
void aes_cfb_encrypt(unsigned char *in, unsigned char *out, size_t length, unsigned char *key, unsigned char *iv)
{
AES_KEY aes_key;
AES_set_encrypt_key(key, 128, &aes_key);
unsigned char eiv[AES_BLOCK_SIZE];
memcpy(eiv, iv, AES_BLOCK_SIZE);
int num = ;
while (num < length) {
unsigned char block[AES_BLOCK_SIZE];
AES_encrypt(eiv, block, &aes_key);
int i;
for (i = ; i < AES_BLOCK_SIZE && num < length; i++) {
out[num] = block[i] ^ in[num];
eiv[i] = out[num];
num++;
}
}
}
int main()
{
unsigned char key[] = "0123456789abcdef";
unsigned char iv[] = "fedcba987654321";
unsigned char plaintext[] = "Hello, world!";
size_t plaintext_len = strlen((char *)plaintext);
unsigned char *ciphertext = malloc(plaintext_len);
aes_cfb_encrypt(plaintext, ciphertext, plaintext_len, key, iv);
printf("Plaintext: %s\n", plaintext);
printf("Ciphertext: ");
int i;
for (i = ; i < plaintext_len; i++) {
printf("%02x", ciphertext[i]);
}
printf("\n");
free(ciphertext);
return ;
}
```
这个程序使用了OpenSSL库中的AES加密函数,实现了AES-CFB模式的加密。它接受一个输入字符串、一个密钥和一个初始化向量,并输出加密后的结果。注意,这个程序只是一个示例,实际使用中需要根据具体需求进行修改和优化。
阅读全文