#include <stdio.h>#include <stdlib.h>#include <string.h>#include <openssl/aes.h>#define AES_KEY_SIZE 128 // AES算法密钥长度#define BLOCK_SIZE 16 // 分组大小// 加密函数void aes_encrypt(unsigned char *in, unsigned char *out, unsigned char *key, int len){ AES_KEY aes_key; // 设置加密密钥 AES_set_encrypt_key(key, AES_KEY_SIZE, &aes_key); // 加密数据 for (int i = 0; i < len; i += BLOCK_SIZE) { AES_encrypt(in + i, out + i, &aes_key); }}// 解密函数void aes_decrypt(unsigned char *in, unsigned char *out, unsigned char *key, int len){ AES_KEY aes_key; // 设置解密密钥 AES_set_decrypt_key(key, AES_KEY_SIZE, &aes_key); // 解密数据 for (int i = 0; i < len; i += BLOCK_SIZE) { AES_decrypt(in + i, out + i, &aes_key); }}int main(){ unsigned char key[AES_KEY_SIZE/8] = {0x00}; // 密钥 unsigned char in[] = "Hello World!"; // 明文 unsigned char out[sizeof(in)]; // 密文 unsigned char decrypted[sizeof(in)]; // 解密后的明文 // 获取明文长度 int len = strlen((char *)in); // 加密数据 aes_encrypt(in, out, key, len); // 输出加密结果 printf("Cipher text: "); for (int i = 0; i < len; i++) { printf("%02x", out[i]); } printf("\n"); // 解密数据 aes_decrypt(out, decrypted, key, len); // 输出解密结果 printf("Decrypted text: %s\n", decrypted); return 0;}这段代码的测试结果
时间: 2024-03-31 21:38:36 浏览: 23
这段代码实现了AES算法的加密和解密功能,使用了OpenSSL库中的AES函数。在main函数中,首先定义了一个128位的密钥和一个明文字符串,然后调用aes_encrypt函数进行加密,输出加密结果。接着调用aes_decrypt函数对加密结果进行解密,输出解密结果。
由于测试数据只有一个字符串,因此无法提供100MB明文字节流的加密/解密处理时间。不过,可以看出这段代码的加密和解密操作都是在for循环中进行的,每次处理一个16字节的分组,因此加密/解密速度较慢,而且代码也没有对加密和解密后的数据进行填充,因此只适用于长度为16的整数倍的数据。如果要处理大量数据,建议使用更快速的加密算法,如ChaCha20或者AES-GCM,并且要注意数据的填充问题。
相关问题
不采用#include <openssl/aes.h> 应该怎么写
如果不使用`<openssl/aes.h>`库,可以自己实现AES算法,下面给出一个基于AES的CMAC算法的C语言代码示例(不使用任何第三方库):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BLOCK_SIZE 16 //AES算法中数据块的长度为16字节
typedef struct {
unsigned char k[16];
unsigned char l[16];
unsigned char r[16];
unsigned char subkey1[16];
unsigned char subkey2[16];
} cmac_ctx;
void aes_encrypt(unsigned char *in, unsigned char *out, unsigned char *key)
{
//AES算法的实现
//...
}
void cmac_init(unsigned char *key, cmac_ctx *ctx)
{
//生成子密钥K1和K2
unsigned char zero[BLOCK_SIZE] = {0};
aes_encrypt(zero, ctx->subkey1, key);
if ((ctx->subkey1[0] & 0x80) == 0) {
for (size_t i = 0; i < BLOCK_SIZE; i++)
ctx->subkey1[i] <<= 1;
} else {
for (size_t i = 0; i < BLOCK_SIZE; i++) {
ctx->subkey1[i] = (ctx->subkey1[i] << 1) | (ctx->subkey1[i+1] >> 7);
}
ctx->subkey1[BLOCK_SIZE-1] ^= 0x87;
}
memcpy(ctx->subkey2, ctx->subkey1, BLOCK_SIZE);
//处理L和R
unsigned char zero2[BLOCK_SIZE] = {0};
aes_encrypt(zero2, ctx->l, key);
for (size_t i = 0; i < BLOCK_SIZE; i++) {
if ((ctx->l[i] & 0x80) == 0) {
ctx->l[i] <<= 1;
} else {
ctx->l[i] = (ctx->l[i] << 1) | (ctx->l[i+1] >> 7);
}
}
}
void cmac_update(unsigned char *msg, size_t msg_len, cmac_ctx *ctx)
{
//处理L和R
size_t n = (msg_len + BLOCK_SIZE - 1) / BLOCK_SIZE;
for (size_t i = 0; i < n; i++) {
size_t len = (i == n-1) ? msg_len % BLOCK_SIZE : BLOCK_SIZE;
unsigned char tmp[BLOCK_SIZE];
memcpy(tmp, &msg[i*BLOCK_SIZE], len);
if (len < BLOCK_SIZE) {
tmp[len] = 0x80;
for (size_t j = len+1; j < BLOCK_SIZE; j++) {
tmp[j] = 0x00;
}
}
for (size_t j = 0; j < BLOCK_SIZE; j++) {
ctx->r[j] ^= tmp[j];
}
aes_encrypt(ctx->r, ctx->r, ctx->k);
}
}
void cmac_final(unsigned char *msg, size_t msg_len, unsigned char *mac, cmac_ctx *ctx)
{
//处理最后一个数据块
unsigned char tmp[BLOCK_SIZE];
if (msg_len % BLOCK_SIZE == 0) {
memcpy(tmp, &msg[msg_len-BLOCK_SIZE], BLOCK_SIZE);
for (size_t i = 0; i < BLOCK_SIZE; i++) {
tmp[i] ^= ctx->subkey1[i];
}
} else {
size_t pad_len = BLOCK_SIZE - (msg_len % BLOCK_SIZE);
memcpy(tmp, &msg[msg_len-(BLOCK_SIZE-pad_len)], BLOCK_SIZE-pad_len);
tmp[BLOCK_SIZE-pad_len] = 0x80;
for (size_t i = BLOCK_SIZE-pad_len+1; i < BLOCK_SIZE; i++) {
tmp[i] = 0x00;
}
for (size_t i = 0; i < BLOCK_SIZE; i++) {
tmp[i] ^= ctx->subkey2[i];
}
}
for (size_t i = 0; i < BLOCK_SIZE; i++) {
ctx->r[i] ^= tmp[i];
}
aes_encrypt(ctx->r, mac, ctx->k);
}
void cmac(unsigned char *key, unsigned char *msg, size_t msg_len, unsigned char *mac)
{
cmac_ctx ctx;
memset(&ctx, 0, sizeof(cmac_ctx));
memcpy(ctx.k, key, BLOCK_SIZE);
cmac_init(key, &ctx);
cmac_update(msg, msg_len, &ctx);
cmac_final(msg, msg_len, mac, &ctx);
}
int main()
{
unsigned char key[] = "1234567890123456";
unsigned char msg[] = "Hello, world!";
unsigned char mac[BLOCK_SIZE];
size_t msg_len = strlen(msg);
cmac(key, msg, msg_len, mac);
printf("Message: %s\n", msg);
printf("MAC: ");
for (int i = 0; i < BLOCK_SIZE; i++) {
printf("%02x", mac[i]);
}
printf("\n");
return 0;
}
```
需要注意的是,此示例仅用于演示用途,实际应用中需要根据具体情况进行修改和优化。
C语言openssl实现aes加密
要在C语言中使用OpenSSL库实现AES加密,你需要按照以下步骤进行操作:
1. 包含必要的头文件:
```c
#include <openssl/aes.h>
#include <openssl/rand.h>
```
2. 生成随机密钥:
```c
unsigned char key[AES_BLOCK_SIZE];
if (!RAND_bytes(key, sizeof(key))) {
// 生成密钥失败
}
```
3. 初始化AES加密上下文:
```c
AES_KEY aes_key;
if (AES_set_encrypt_key(key, 128, &aes_key) < 0) {
// 初始化加密上下文失败
}
```
4. 加密数据:
```c
unsigned char plaintext[AES_BLOCK_SIZE] = "Hello, AES!";
unsigned char ciphertext[AES_BLOCK_SIZE];
AES_encrypt(plaintext, ciphertext, &aes_key);
```
5. 解密数据:
```c
unsigned char decrypted[AES_BLOCK_SIZE];
AES_decrypt(ciphertext, decrypted, &aes_key);
```
完整的示例代码如下所示:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
void print_hex(const unsigned char *buf, size_t len) {
for (size_t i = 0; i < len; i++) {
printf("%02X", buf[i]);
}
printf("\n");
}
int main() {
unsigned char plain_text[AES_BLOCK_SIZE] = "Hello, AES!";
unsigned char key[AES_BLOCK_SIZE];
// 生成密钥
if (!RAND_bytes(key, sizeof(key))) {
fprintf(stderr, "生成密钥失败!\n");
return 1;
}
printf("原始数据:");
print_hex(plain_text, sizeof(plain_text));
// 加密
AES_KEY aes_key;
if (AES_set_encrypt_key(key, 128, &aes_key) < 0) {
fprintf(stderr, "初始化加密上下文失败!\n");
return 1;
}
unsigned char cipher_text[AES_BLOCK_SIZE];
AES_encrypt(plain_text, cipher_text, &aes_key);
printf("加密后的数据:");
print_hex(cipher_text, sizeof(cipher_text));
// 解密
if (AES_set_decrypt_key(key, 128, &aes_key) < 0) {
fprintf(stderr, "初始化解密上下文失败!\n");
return 1;
}
unsigned char decrypted[AES_BLOCK_SIZE];
AES_decrypt(cipher_text, decrypted, &aes_key);
printf("解密后的数据:");
print_hex(decrypted, sizeof(decrypted));
return 0;
}
```
以上代码将生成一个随机的128位密钥,使用该密钥对明文进行AES加密,并将加密后的结果进行解密。输出结果将打印出原始数据、加密后的数据和解密后的数据的十六进制表示形式。
请确保已经正确安装了OpenSSL库,并通过编译器链接该库进行编译。