Hey guys,
I am very new to Arduino project and trying my hands on building my first temperature humidity sensor. I am using a NodeMCU ESP8266 as the microcontroller board. I connected a HC-05 for bluetooth functionality. I wanted to do encryption on data being passed between the device and my mobile app (build using react native). I am using the arduino crypto library. I wanted to use the shared key I wanted to pass the WiFi credentials to the device encrypted using the key. I used the below code to generate the key and pass it to the mobile app.
void getConnected() {
// wait till someone try to connect to the user
if (btSerial.available() > 0) {
String message = btSerial.readString();
if(message == "Hello") {
Serial.println("user trying to connect");
byte key[32];
device.getKey(key);
Serial.println((char *)key);
btSerial.write((char *)key);
}
}
}
the device.getKey(key) is as follows:
void Device::getKey(byte* key) {
RNG.rand(key, sizeof(key));
}
The issue I am facing is that the RNG generates the key as a array of byte. I printed the bytes and it was something like this:
137 224 186 115 0 0 0 0 172 228 254 63 131 53 32 64 208 218 255 63 13 0 0 0 172 228 254 63 48 70 32 64 0
When the data is received in the mobile app, it is returning only the first 4 bytes because it is encountering a 0 after that and stopping the transmission. I am not really sure the best way to get this across to the mobile app. I tried converting every byte to String object and concatenating it but that didn't work either. Its returning something like
89e0ba730000ace4fe3f83352040d0daff3fd000ace4fe3f304620400
And I am not sure how to convert the same back into the byte array in React Native.
I apologize if this is a repeated question. I would really appreciate some help here. I am stuck with getting the cryptography to work between the React Native app and ESP8266 for the last 2 days.
Thanks in advance for any pointers.