aes128 加解密的C语言实现
时间: 2023-08-29 21:12:54 浏览: 93
以下是AES128加解密的C语言实现,使用了OpenSSL库:
```c
#include <openssl/aes.h>
int aes_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) {
AES_encrypt(in, out, key);
return 0;
}
int aes_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key) {
AES_decrypt(in, out, key);
return 0;
}
```
其中,`AES_KEY`是OpenSSL库中定义的结构体,用于存储AES算法密钥。`aes_encrypt`和`aes_decrypt`分别为加密和解密函数,参数中的`in`和`out`分别为输入和输出的数据缓冲区,`key`为AES算法密钥。
使用此代码需要先安装OpenSSL库,并在代码中引入头文件`<openssl/aes.h>`。
相关问题
aes加解密c语言实现
AES是一种高级加密标准,它是一种对称加密算法,也就是说AES加密和解密使用的是同一套算法。下面是使用C语言实现AES加解密的示例代码(使用openssl库):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#define AES_BLOCK_SIZE 16
int aes_encrypt(unsigned char *in, int in_len, unsigned char *key, unsigned char *out)
{
AES_KEY aes_key;
if (AES_set_encrypt_key(key, 128, &aes_key) < 0) {
return -1;
}
int padding_len = AES_BLOCK_SIZE - (in_len % AES_BLOCK_SIZE);
int total_len = in_len + padding_len;
unsigned char *padded_in = malloc(total_len);
if (!padded_in) {
return -1;
}
memcpy(padded_in, in, in_len);
memset(padded_in + in_len, padding_len, padding_len);
for (int i = 0; i < total_len / AES_BLOCK_SIZE; i++) {
AES_encrypt(padded_in + i * AES_BLOCK_SIZE, out + i * AES_BLOCK_SIZE, &aes_key);
}
free(padded_in);
return total_len;
}
int aes_decrypt(unsigned char *in, int in_len, unsigned char *key, unsigned char *out)
{
AES_KEY aes_key;
if (AES_set_decrypt_key(key, 128, &aes_key) < 0) {
return -1;
}
for (int i = 0; i < in_len / AES_BLOCK_SIZE; i++) {
AES_decrypt(in + i * AES_BLOCK_SIZE, out + i * AES_BLOCK_SIZE, &aes_key);
}
int padding_len = out[in_len - 1];
return in_len - padding_len;
}
int main()
{
unsigned char key[16] = "0123456789abcdef";
unsigned char in[] = "hello world";
unsigned char out[256];
int len = aes_encrypt(in, strlen(in), key, out);
printf("encrypt: %s\n", out);
int out_len = aes_decrypt(out, len, key, in);
in[out_len] = '\0';
printf("decrypt: %s\n", in);
return 0;
}
```
注意,在加密时需要对输入进行padding,以保证长度是16的倍数。在解密时需要去除padding。
AES加密解密C语言实现
以下是AES加密解密的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
// AES加密函数
int aes_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
{
AES_set_encrypt_key(key->key, 128, key);
AES_encrypt(in, out, key);
return 0;
}
// AES解密函数
int aes_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
{
AES_set_decrypt_key(key->key, 128, key);
AES_decrypt(in, out, key);
return 0;
}
int main()
{
AES_KEY key;
unsigned char *input_str = "Hello World!"; // 待加密的字符串
unsigned char *encrypt_str = (unsigned char *)malloc(strlen(input_str) + 1); // 存储加密后的字符串
unsigned char *decrypt_str = (unsigned char *)malloc(strlen(input_str) + 1); // 存储解密后的字符串
// 设置密钥
unsigned char *key_str = "1234567890123456";
AES_set_encrypt_key(key_str, 128, &key);
// 加密
aes_encrypt(input_str, encrypt_str, &key);
printf("加密后的字符串:%s\n", encrypt_str);
// 解密
aes_decrypt(encrypt_str, decrypt_str, &key);
printf("解密后的字符串:%s\n", decrypt_str);
free(encrypt_str);
free(decrypt_str);
return 0;
}
```
阅读全文