//source://https://everythingesp.com/esp32-arduino-tutorial-encryption-aes128-in-ecb-mode/
//check:
//https://the-x.cn/en-US/cryptography/Aes.aspx
//http://aes.online-domain-tools.com/
//ascii to hex//https://onlinetools.com/hex/convert-ascii-to-hex
//#include "Seeed_mbedtls.h"
#include "mbedtls/aes.h"
void encrypt(char * plainText, char * key, unsigned char * outputBuffer){
mbedtls_aes_context aes;
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*)plainText, outputBuffer);
mbedtls_aes_free( &aes );
}
void decrypt(unsigned char * chipherText, char * key, unsigned char * outputBuffer){
mbedtls_aes_context aes;
mbedtls_aes_init( &aes );
mbedtls_aes_setkey_dec( &aes, (const unsigned char*) key, strlen(key) * 8 );
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, (const unsigned char*)chipherText, outputBuffer);
mbedtls_aes_free( &aes );
}
void setup() {
Serial.begin(115200);
char * key = "abcdefghijklmnop";
//unsigned char* key [] ={0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70};
char *plainText = "Tech tutorials x";
unsigned char cipherTextOutput[16];
unsigned char decipheredTextOutput[16];
encrypt(plainText, key, cipherTextOutput);
decrypt(cipherTextOutput, key, decipheredTextOutput);
Serial.println("\nOriginal plain text:");
Serial.println(plainText);
Serial.println("\nCiphered text:");
for (int i = 0; i < 16; i++) {
Serial.print((char)cipherTextOutput[i]);
}
/* for (int i = 0; i < 16; i++) {
char str[3];
sprintf(str, "%02x", (int)cipherTextOutput[i]);
Serial.print(str);
}*/
Serial.println("\n\nDeciphered text:");
//Serial.print(decipheredTextOutput);
for (int i = 0; i < 16; i++) {
Serial.print((char)decipheredTextOutput[i]);
}
}
void loop() {}