RAK3172 AES128 HEX - ASCII

Hello,

I was communicating with two RAK3172 modules in P2P mode without encryption. Then I wanted to do encryption and I encrypted the payload with commands. I wrote the same codes in the receiver and transmitter part. But after uploading the new codes, I decode the received data and verify that I solved it. The data I decode comes in HEX format correctly. After applying the HEX to ASCII process on an online site, I can read the data correctly. But the code below fails to translate correctly when converting to ASCII for me. Can you help?

Note: This issue arose when I started using AES128 encryption. Oddly enough, the decrypting is successful so I can read the decrypted HEX data properly.

Code:

void recv_cb(rui_lora_p2p_recv_t data) {
  rx_done = true;
  if (data.BufferSize == 0) {
    Serial.println("Empty buffer.");
    return;
  }
  char donusturulmus[data.BufferSize + 1];
  for (uint16_t i = 0; i < data.BufferSize; i++) {
    donusturulmus[i] = (char)data.Buffer[i];
  }
  donusturulmus[data.BufferSize] = '\0'; 
  Serial.print("Incoming message: ");
  Serial.println(donusturulmus);

Output Sample:

+EVT:RXP2P:-18:7:312C31312C37
Incoming message: ǎxµ)?

The data sample should look like:

+EVT:RXP2P:-18:7:312C31312C37
Incoming message: 1,11,7

It also seems same in P2P Example ASCII Table:

image

Can you help with this?
@Kongduino @carlrowan I believe that you can handle this :))

donusturulmus[i] = (char)data.Buffer[i];

You are not converting, just copying. What you need to do is:

  1. Prepare a buffer twice as long as the payload, + 1 byte for \0.
  2. Convert each byte to a 2-byte ASCII hex display.
void array2hex(uint8_t *inBuf, size_t sLen, char *outBuf) {
  size_t i, len, n = 0;
  const char *hex = "0123456789ABCDEF";
  for (i = 0; i < sLen; ++i) {
    outBuf[n++] = hex[(inBuf[i] >> 4) & 0xF];
    outBuf[n++] = hex[inBuf[i] & 0xF];
  }
  outBuf[n++] = 0;
}

uint8_t ln = data.BufferSize;
char donusturulmus[ln*2+1];
array2hex(data.Buffer, ln, donusturulmus);

This does the trick. donusturulmus will have the data, properly formatted.

1 Like

Isn’t this code for converting to HEX? I need to convert HEX to ASCII.
Also, after applying the code you gave, my output is:

+EVT:RXP2P:-28:7:312C31312C35
Incoming message: 15EAA6

@Kongduino @carlrowan Guys please help. As you can see below im getting a weird problem. As shown in comments, i explained the process.

+EVT:RXP2P:-22:7:9A1FCEF3CB64A39E20BC58BDD096AFE7 // IF YOU CONVERT HEX TO ASCII YOU'LL SEE = šÎóËd£ž ¼X½Ð–¯ç
11:54
Incoming message: 1,11,23 // BUT THIS LINE SHOWS CORRECT DATA
11:54
Sensor ID: 1
11:54
Match ID: 11
11:54
Data: 23
11:54

//AT HERE, WITH THE "AT+ENCRY=1" COMMAND ENCRYPTION ENABLED. AND THE ENCRYPTION KEY SET SAME IN DEVICES BEFORE.

11:55
+EVT:RXP2P:-23:6:312C31312C3234 // IF YOU CONVERT HEX TO ASCII YOU'LL SEE = 1,11,24
11:55
Incoming message: `�m�0f // BUT THIS LINE SHOWS FALSE DATA
11:55
Sensor ID: `�m�0f
11:55
Match ID:
11:55
Data:
11:55

I have latest RAK3172 firmware.

Ah yes, that’d be the reverse you need.

void hex2array(uint8_t *src, uint8_t *dst, size_t sLen) {
  size_t i, n = 0;
  for (i = 0; i < sLen; i += 2) {
    uint8_t x, c;
    c = src[i];
    if (c != '-') {
      if (c > 0x39) c -= 55;
      else c -= 0x30;
      x = c << 4;
      c = src[i + 1];
      if (c > 0x39) c -= 55;
      else c -= 0x30;
      dst[n++] = (x + c);
    }
  }
}

Also, note that the encryption scheme in RUI3 is broken – has been for a while – and might get fixed in a release down the line. Use software AES code from my repositories.

1 Like

Thanks for your interest.

Do you have any idea about when R&D team will fix RUI3 encryption problem?
@Kongduino @carlrowan

Hi @whydont ,

As of today, the team is already looking at issues related to the encryption. Surely we are looking in to this :100:

1 Like

I hope we have the encryption fixed with V4.0.2.
Right now the team is fixing a bug we found in V4.0.0 which is critical. V4.0.1 should be released within the next days, then they work on the encryption.

2 Likes

Thank you.When it’ll be released?

I don’t have release date yet.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.