Converting to HEX and deHex increase end String value + 1

tsctrl
Posts: 30
Joined: Tue Dec 08, 2020 1:37 pm

Converting to HEX and deHex increase end String value + 1

Postby tsctrl » Thu Dec 30, 2021 3:02 pm

Hi all,

I have small issue that i cant figure out why.

uint8_t nonce[16] = "1234567890123456";
String cipherText = String();
for (int i = 0; i < 17; i++) {
char str[3];
sprintf(str, "%02X", nonce);
cipherText = cipherText+String(str);
}
System.println(cipherText);

result is: "31323334353637383930313233343537"
when i use hex converter online the de hex result is "1234567890123457"

hex converter online: https://string-functions.com/hex-string.aspx

The last value will always increase + 1
Anyone can help whats wrong with this codes?

tsctrl
Posts: 30
Joined: Tue Dec 08, 2020 1:37 pm

Re: Converting to HEX and deHex increase end String value + 1

Postby tsctrl » Thu Dec 30, 2021 5:13 pm

unsigned char nonce[16] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};

ESP_LOGE(TAG, "[LOGGER]: nonce. %x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x", nonce[0], nonce[1], nonce[2], nonce[3], nonce[4], nonce[5], nonce[6], nonce[7], nonce[8], nonce[9], nonce[10], nonce[11], nonce[12], nonce[13], nonce[14], nonce[15]);

[LOGGER]: nonce. 1111111111111112 :D :D :D :D :D :D :D

chegewara
Posts: 2362
Joined: Wed Jun 14, 2017 9:00 pm

Re: Converting to HEX and deHex increase end String value + 1

Postby chegewara » Thu Dec 30, 2021 6:34 pm

tsctrl wrote:
Thu Dec 30, 2021 3:02 pm
Hi all,

I have small issue that i cant figure out why.

uint8_t nonce[16] = "1234567890123456";
String cipherText = String();
for (int i = 0; i < 17; i++) {
char str[3];
sprintf(str, "%02X", nonce);
cipherText = cipherText+String(str);
}
System.println(cipherText);

result is: "31323334353637383930313233343537"
when i use hex converter online the de hex result is "1234567890123457"

hex converter online: https://string-functions.com/hex-string.aspx

The last value will always increase + 1
Anyone can help whats wrong with this codes?

But you know you are trying to read beyond nonce array? What i mean you are reading 17 elements from array size of 16.

tsctrl
Posts: 30
Joined: Tue Dec 08, 2020 1:37 pm

Re: Converting to HEX and deHex increase end String value + 1

Postby tsctrl » Thu Dec 30, 2021 6:42 pm

yup, tested with 16 with the same issue.

tsctrl
Posts: 30
Joined: Tue Dec 08, 2020 1:37 pm

Re: Converting to HEX and deHex increase end String value + 1

Postby tsctrl » Thu Dec 30, 2021 6:50 pm

i have use base64 also with the same issue. and also use other loop approach.

thanks!

tsctrl
Posts: 30
Joined: Tue Dec 08, 2020 1:37 pm

Re: Converting to HEX and deHex increase end String value + 1

Postby tsctrl » Fri Dec 31, 2021 2:20 am

hi @chegewara, yes you correct. the issue is the iv need to be copy as on your other reply on other post here
viewtopic.php?f=2&t=25269
thanks you so much and happy new year!

Who is online

Users browsing this forum: No registered users and 187 guests