0

I am working with the Mbed TLS Library on an ESP32 board and a function i am using takes only unsigned char and also gives unsigned char as the output

  mbedtls_aes_setkey_enc( &aes, key, 256 );
  mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, 48, iv, input, output );

I now need to display output using this method

Heltec.display->drawString(0, 0, String(output));

but here it only takes strings type... how do i go about converting the unsigned char to string here? or is there a better way to display the output (I am using a Heltec LoRa32 board with a 0.96-inch display)

here is the full code

#include "mbedtls/aes.h"
#include "heltec.h"

mbedtls_aes_context aes;

unsigned char key[32];
unsigned char iv[16];

unsigned char input [128];
unsigned char output[128];

size_t input_len = 40;
size_t output_len = 0;

void setup() {
  Heltec.display->init();
  Heltec.display->flipScreenVertically();
  Heltec.display->setFont(ArialMT_Plain_10);
  delay(1500);
  Heltec.display->clear();

  Heltec.display->drawString(0, 0, "Heltec.LoRa Initial success!");
  Heltec.display->display();
  delay(1000);

  mbedtls_aes_setkey_enc( &aes, key, 256 );
  mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, 48, iv, input, output );
  }

void loop() {
  Heltec.display->clear();
  Heltec.display->setTextAlignment(TEXT_ALIGN_LEFT);
  Heltec.display->setFont(ArialMT_Plain_10);
  Heltec.display->drawString(0, 0, String(output);
  Heltec.display->display();
}
2
  • 1
    String((char*)output)? (assuming output is null terminated) Commented Apr 20, 2022 at 17:25
  • It looks like output is binary (encrypted) data, in which case displaying it as-is is not a good idea. You likely want to encode the raw bytes as a hex-encoded string first, then display that instead. Commented Apr 20, 2022 at 17:26

1 Answer 1

0

mbedtls_aes_crypt_cbc has 'strings' (unsigned char arrays) as input and output, not a single character:

mbedtls_aes_crypt_cbc (mbedtls_aes_context *ctx, int mode, size_t length, unsigned char iv[16], const unsigned char *input, unsigned char *output)

drawString takes 'string' (signed char array) as input:

void U8X8::drawString(uint8_t x, uint8_t y, const char *s)

and your output variable is char array, so all is correct (or some conversion to signed char pointer might be necessary).

You forgot closing parenthesis:

Heltec.display->drawString(0, 0, String(output));

of course if 'String' is a conversion :)

should be enough to:

Heltec.display->drawString(0, 0, (char*)(output));

Good comment is that from Remy Lebeau - output is a scrambled stream of bytes, there is no sense to print them (better display as HEX instead).

Sign up to request clarification or add additional context in comments.

1 Comment

A minor correction, char is neither defined to be signed nor unsigned. It depends on the platform. Char is actually a third distinct type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.