0

我是 Arduino 新手,正在开发一个在 Arduino (ESP8266) 和 React Native 应用程序之间传递加密密钥的项目。在 ESP8266 方面,我使用的是arduino 加密库,而对于 React Native,我使用的是react-native-crypto-js。加密似乎没有做正确的事情并返回垃圾。因此,作为第一步,我尝试将密钥从 Arduino 传递到移动应用程序(使用蓝牙 HC-05)。arduino 端使用了以下代码:

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);
    }
  }
}

密钥是使用生成的RNG.rand(key, sizeof(key)),我还单独打印了生成的字节,它类似于:

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,React Native 应用程序中的代码只获取前 4 个字节,其余的被忽略,因为它认为第 5 个字节是字符串的结尾。App中使用的代码如下:

  async setup(deviceId) {
    console.log('connecting');
    await BluetoothSerial.connect(deviceId);
    console.log('connected');
    await BluetoothSerial.write('Hello');
    setTimeout(async () => {
      let key = await BluetoothSerial.readFromDevice();
      console.log(key);
    }, 3000);
    // const input = await BluetoothSerial.readFromDevice();
    return true;
  }

我真的很感激一些指示。请帮忙。

谢谢

4

0 回答 0