Arduino IDE and esp8266 arduino-crypto library

Stealthrt used Ask the Experts™
I am using arduino-crypto for my ESP8266. I'm not running into any errors but I do have an issue that I hope you can solve for me.

This code:
#define BLOCK_SIZE 16

uint8_t key[BLOCK_SIZE] = { 0x1C,0x3E,0x4B,0xAF,0x13,0x4A,0x89,0xC3,0xF3,0x87,0x4F,0xBC,0xD7,0xF3, 0x31, 0x31 };
uint8_t iv[BLOCK_SIZE] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

char plain_text[] = "1234567890ABCDEF1234567890ABCDEF";

  // encrypt
  int length = 0;
  bufferSize(plain_text, length);
  char encrypted[length];
  encrypt(plain_text, encrypted, length);

  Serial.print("Encrypted: ");


Open in new window

Works just fine and gives me an encryption value of:

Encrypted: y3QzsGi6cYiy3GGs31jXvTY8VI2OrlxsohOasiw4pgP+54gWXtUZPrjuRvOfyRTz

Open in new window

Now if i do both encrypt and decrypt then it decodes it just fine.

However, when i just run the decryption and copy the encrypted value and then run this:

// decrypt
  char* encrypted = "y3QzsGi6cYiy3GGs31jXvTY8VI2OrlxsohOasiw4pgP+54gWXtUZPrjuRvOfyRTz";
  int length = strlen(encrypted);
  char decrypted[length];
  decrypt(encrypted, decrypted, length);
  Serial.print("Decrypted: ");

Open in new window

It outputs this in the console:

Decrypted: ⸮⸮⸮⸮⸮j⸮Ҏ⸮R|;⸮⸮1234567890ABCDEF

Open in new window

It has a few original message letters/numbers in there but not sure why its not decrpting it like it does when you run both back-to-back.

What can be done in order to fix this?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun VermaakSenior Consultant
Awarded 2017
Distinguished Expert 2018

Did you try at a different bps?

For example

Open in new window

btanExec Consultant
Distinguished Expert 2018

Make sure that all the uninitialised char string are set to a known value i.e. 0. You can consider memset encrypted and decrypted string.

Especially for decrypted, its length must be sufficient to hold the plain text. I noted that you actually used the decrypted's length as the length of the ciphertext string. I know it is larger than the chosen plaintext  hence it should be alright but the proper way is that the return function of decrypt should have a value and then you can allocate memory space to store the plaintext.

After memset the decrypted string, you will have a null char or a terminating char for a string so that it can be printed out in proper. You should also print out the actual length of the decrypted again - it should not be the original decrypted length i.e. ciphertext length.

Anyway, not sure what is going on in the encrypt and decrypt function but I assumed they are handling the base64 accordingly as well as include the IV and padding as part of the encryption and decryption. The proper example share the run through from encrypt to decrypt using the actual AES functions and more.
nociSoftware Engineer
Distinguished Expert 2018

Was the key & IV setup  correctly in the second case?
And the example shows length = 0...? is that correct?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial