ESP8266&&32 AES加密、解密 (ECB方式)

#include 
#include "esp_system.h"
#include "mbedtls/aes.h"
#include 
#include 
#include "config.h"
#include 
#include 
#include "esp_system.h"

void app_main(void)
{
int ret = 0, i, j, u, mode;
unsigned int keybits;
unsigned char key[16]="123456789012345";
unsigned char inbuf[16]="123456789012345";
unsigned char outbuf[16]={0};
unsigned char buf[16]={0};
const unsigned char *aes_tests;
mbedtls_aes_context ctx;
//memset(key,0,16);

mbedtls_aes_init(&ctx);
for(int loop=0;loop<16;loop++)
     printf("%c",key[loop]);
keybits=128;
mode=1;
printf( "  AES-ECB-%3d (%s):\r\n ", keybits,
                            ( mode == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );
//memset( buf, 0, 16 );


//ret = mbedtls_aes_setkey_dec( &ctx, key, keybits );
ret = mbedtls_aes_setkey_enc( &ctx, key, keybits );
//aes_tests = aes_test_ecb_dec[u];
ret = mbedtls_aes_crypt_ecb( &ctx, mode, inbuf, outbuf );
//printf("%s\r\n",buf);
for(int loop=0;loop<16;loop++)
    printf("%x",outbuf[loop]);
//printf("enc%s",buf);

printf("\r\n");
mbedtls_aes_init(&ctx);    
mode=0;
ret = mbedtls_aes_setkey_dec( &ctx, key, keybits );
//aes_tests = aes_test_ecb_dec[u];
ret = mbedtls_aes_crypt_ecb( &ctx, mode, outbuf, buf );

printf("dec%s",buf);
printf("\r\n");
printf("balabala\r\n");

}

运行结果:
ESP8266&&32 AES加密、解密 (ECB方式)_第1张图片
这里可以借助AES在线加密解密工具对照
ESP8266&&32 AES加密、解密 (ECB方式)_第2张图片

你可能感兴趣的:(嵌入式)