#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 19:38:36 浏览: 140
这段代码实现了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;
}
```
需要注意的是,此示例仅用于演示用途,实际应用中需要根据具体情况进行修改和优化。
openSSL实现AES解密
下面是一个使用OpenSSL库实现AES解密的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
int aes_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext) {
AES_KEY dec_key;
int len;
if (AES_set_decrypt_key(key, 128, &dec_key) < 0)
return -1;
AES_cbc_encrypt(ciphertext, plaintext, ciphertext_len, &dec_key, iv, AES_DECRYPT);
return 0;
}
int main() {
unsigned char key[AES_BLOCK_SIZE] = "0123456789abcdef"; // 16 bytes key
unsigned char iv[AES_BLOCK_SIZE] = "fedcba9876543210"; // 16 bytes iv
unsigned char ciphertext[] = {0x26, 0xc1, 0x6f, 0x1a, 0x3f, 0x82, 0x20, 0xdd, 0x4c, 0x9b, 0xc0, 0x95, 0x07, 0xec, 0x8a, 0x0e}; // 16 bytes ciphertext
int ciphertext_len = sizeof(ciphertext);
unsigned char plaintext[ciphertext_len];
// Decrypt the ciphertext
aes_decrypt(ciphertext, ciphertext_len, key, iv, plaintext);
// Print the decrypted plaintext
printf("Decrypted plaintext: %s\n", plaintext);
return 0;
}
```
在上述代码中,`aes_decrypt` 函数接受加密后的密文、密钥和初始化向量作为输入,并将解密后的明文输出到 `plaintext` 缓冲区。`main` 函数使用示例密文、密钥和初始化向量调用该函数,并打印出解密后的明文。
请注意,此示例代码中使用的是AES-128算法,因此密钥和初始化向量均为16个字节。如果您使用其他密钥长度,则需要相应地更改其长度。
阅读全文