Sample code to encrypt string using AES TLS library in esp32
Posted: Wed Dec 29, 2021 4:51 pm
Hi all,
anyone have sample code how to encrypt string in esp32?
i have this currently working but is not recommended approach and is only able to encrypt 16 characters
mbedtls_aes_context aes;
char * key = "abcdefghijklmnop";
char *input = "abcdefghijklmnop";
unsigned char output[16];
mbedtls_aes_init(&aes);
mbedtls_aes_setkey_enc(&aes, (const unsigned char*) key, strlen(key) * 8);
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, (const unsigned char*)input, output);
mbedtls_aes_free(&aes);
char result[50];
result[0] = '\0';
for (int i = 0; i < 32; i++) {
char str[3];
sprintf(str, "%02x", (int)output);
strlcat(result, str, sizeof(result));
Serial.print(str);
}
Serial.print(result);
thank you
anyone have sample code how to encrypt string in esp32?
i have this currently working but is not recommended approach and is only able to encrypt 16 characters
mbedtls_aes_context aes;
char * key = "abcdefghijklmnop";
char *input = "abcdefghijklmnop";
unsigned char output[16];
mbedtls_aes_init(&aes);
mbedtls_aes_setkey_enc(&aes, (const unsigned char*) key, strlen(key) * 8);
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, (const unsigned char*)input, output);
mbedtls_aes_free(&aes);
char result[50];
result[0] = '\0';
for (int i = 0; i < 32; i++) {
char str[3];
sprintf(str, "%02x", (int)output);
strlcat(result, str, sizeof(result));
Serial.print(str);
}
Serial.print(result);
thank you