plapointe6 / EspMQTTClient

Wifi and MQTT handling for ESP8266 and ESP32
GNU General Public License v3.0
441 stars 132 forks source link

Incomplete string when some encrypted buffer is received [Solution found, please update and release] #101

Open bangbaew opened 2 years ago

bangbaew commented 2 years ago

Description of the problem

I encrypt a JSON string from node.js using AES-128 ECB mode and send the encrypted buffer to my esp32 via Mqtt, and decrypt on my esp32 using esp32-Encrypt, however, the encryption process seems to work fine but when receiving the buffer, sometimes it only receives a partial buffer when sending a specific string.

for example: this is an example of a complete buffer, and it's always complete if the origin string is the one in the picture

image

this is from mqttx which is also subscribed to the same topic, the buffer is complete as same as the received one

image

but when the origin string data is changed from https://app.binance.com/qr/dplkff1d79e287474989a576f5b33064a7a7 to https://app.binance.com/qr/dplk3568fe48d0d24fe083c53efdd066c18b or some other different strings, the received buffer becomes incomplete, it only received a partial of the encrypted buffer, and is always reproducible

image

so I checked the mqttx, the encrypted buffer is complete but the highlighted part is the only part received by esp32

image

Versions

Hardware

C++ code

Mqtt code is the same as in the readme example

bangbaew commented 2 years ago

I've tried using PubSubClient, it receives the encrypted message correctly

image

but the code is different, it gets message length and loops the array buffer until it completes the length

image
bangbaew commented 2 years ago

I've found the solution for EspMQTTClient, looks like it's the problem with (char*)payload and payloadStr.c_str() which will unexpectedly terminate the encrypted string, so in EspMQTTClient.cpp EspMQTTClient::mqttMessageReceivedCallback, I had to change from String payloadStr((char*)payload); to the for loop below, and now it's working perfectly!

image

However, I'm not sure if this can be the permanent solution to this issue, please find a better solution and release the fix in the next updates, thanks.

EdJoPaTo commented 2 years ago

please find a better solution

This is an open source project. Others wont just do the work for you. Do tests and suggest one :)

Strings in C/C++ assume the 0 byte as end of string. Does the encrypted data contain the 0 byte? If so, its probably the reason why this happens. Its probably not a good idea to keep the data as data type String then.

bangbaew commented 2 years ago

please find a better solution

This is an open source project. Others wont just do the work for you. Do tests and suggest one :)

Strings in C/C++ assume the 0 byte as end of string. Does the encrypted data contain the 0 byte? If so, its probably the reason why this happens. Its probably not a good idea to keep the data as data type String then.

Sorry for that, I mean please suggest me if there's a better implementation to the code, because I don't know if using a for-loop like that will affect the efficiency/performance or not, or is there a way to prevent the 0 byte data to terminating the string. Thank you for providing the reason why the problem happens.