Link to home
Start Free TrialLog in
Avatar of Stealthrt
StealthrtFlag for United States of America

asked on

Arduino IDE and esp8266 arduino-crypto library

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.println("");
  Serial.print("Encrypted: ");
  Serial.println(encrypted);

  Serial.println(length);
  Serial.println(strlen(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: ");
  Serial.println(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?
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Did you try at a different bps?

For example
Serial.begin(9600);

Open in new window

Avatar of btan
btan

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.
Was the key & IV setup  correctly in the second case?
And the example shows length = 0...? is that correct?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.