Error with AESlib library on Arduino

Hi, I am trying to implement the AES-128 encryption/decryption of a string in an ESP32 and for this I am using the AESLib library, but when I compile it I get this error:

'AES128_CBC_encrypt_buffer' was not declared in this scope

Someone can tell me how to correct this?

Here is my code:

#include <AESLib.h>

// key and IV should be 16 bytes long
const byte key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
const byte iv[16]  = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };

String plaintext = "My message to be encrypted";
String ciphertext;
String decryptedtext;

void setup() {
  Serial.begin(115200);
  //encrypt the plaintext
  Serial.println("Encrypting...");
  AES128_CBC_encrypt_buffer(key, iv, (byte*)&plaintext[0], plaintext.length());
  ciphertext = plaintext;
  Serial.println("Ciphertext is: " + ciphertext);

  //decrypt the ciphertext
  Serial.println("Decrypting...");
  AES128_CBC_decrypt_buffer(key, iv, (byte*)&ciphertext[0], ciphertext.length());
  decryptedtext = ciphertext;
  Serial.println("Decrypted text is: " + decryptedtext);
}

void loop() {
}

You are probably using the wrong AES library. Look for one that has that function, and post a link to where you found that code.

The best way to start with any library is to make sure that one or more of the examples included with the library compiles and runs as expected.

I found this example for AES encryption//decryption using the ESP32 native library.

#include "mbedtls/aes.h"

void encrypt(const char *plainText, const 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, const 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);

  const char *key = "abcdefghijklmnop";

  const 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++)
  {
    char str[3];

    sprintf(str, "%02x", (int)cipherTextOutput[i]);
    Serial.print(str);
  }

  Serial.println("\n\nDeciphered text:");
  for (int i = 0; i < 16; i++)
  {
    Serial.print((char)decipheredTextOutput[i]);
  }
}

void loop() {}

#include "mbedtls/aes.h"

where to set key & IV?

The key is passed as "key".

ECB doesn't use an IV.
For CBC, call the CBC versions which have an 'iv' argument:
https://os.mbed.com/teams/sandbox/code/mbedtls/docs/tip/aes_8h.html

Google had the answers.

from where can get library?

It is part of the ESP32 support. If you want to use AES on some other processor, look for a different library.

can you help me to find why output data not matched

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.