#include <stdio.h> #include <string.h> #include "aes.h"

int main(void) { // 输入加密的明文 unsigned char input[16] = "Hello, World!"; size_t input_length = strlen((const char*)input);

// 设置密钥
unsigned char key[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
unsigned char iv[16] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};

// 创建AES上下文
mbedtls_aes_context aes_ctx;
mbedtls_aes_context aes_ctx_decrypt;

// 初始化AES上下文
mbedtls_aes_init(&aes_ctx);
mbedtls_aes_init(&aes_ctx_decrypt);

// 设置AES密钥和加密模式
mbedtls_aes_setkey_enc(&aes_ctx, key, 128);
mbedtls_aes_setkey_dec(&aes_ctx_decrypt, key, 128);

// 加密数据
unsigned char output[16];
mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, input_length, iv, input, output);

// 打印加密结果
printf("加密结果:");
for (int i = 0; i < 16; i++)
{
    printf("%02x ", output[i]);
}
printf("\n");

// 解密数据
unsigned char decrypted[16];
mbedtls_aes_crypt_cbc(&aes_ctx_decrypt, MBEDTLS_AES_DECRYPT, input_length, iv, output, decrypted);

// 打印解密结果
printf("解密结果: ");
for (int i = 0; i < 16; i++)
{
    printf("%c", decrypted[i]);
}
printf("\n");

// 清理AES上下文
mbedtls_aes_free(&aes_ctx);
mbedtls_aes_free(&aes_ctx_decrypt);

return 0;
#include stdioh#include stringh#include aeshint mainvoid 输入加密的明文 unsigned char input16 = Hello World!; size_t input_length = strlenconst charinput; 设置密钥 unsigned char key16 = 0x01 0x2

原文地址: http://www.cveoy.top/t/topic/iYOe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录