简体   繁体   English

将 unsigned char 转换为十六进制不正确。 最后一个值将始终增加 + 1

[英]Converting unsigned char to hex incorrect. Last value will always increase + 1

I have issue that i cant figure out why.我有问题,我不知道为什么。

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

result is: "31323334353637383930313233343537"结果是:“31323334353637383930313233343537”

when i use hex converter online to decode back, the string result is "1234567890123457" which is not the same as the the initial chars.当我在线使用十六进制转换器进行解码时,字符串结果是“1234567890123457”,这与初始字符不同。 using bytes {0x01}, {0x02}.. with the same result.使用字节 {0x01}、{0x02}.. 具有相同的结果。

this code was a part that pass as a iv to a cipher encryption.此代码是作为 iv 传递给密码加密的一部分。 hex converter online: https://string-functions.com/hex-string.aspx十六进制转换器在线: https://string-functions.com/hex-string.aspx

The last value will always increase + 1. I have tried other approach to loop with the same result.最后一个值将始终增加 + 1。我尝试了其他方法来循环获得相同的结果。 I also have use to convert the unsigned char to base64 and decode and have the same issue.我也可以将 unsigned char 转换为 base64 并解码并遇到同样的问题。 Anyone can help whats wrong with this codes?任何人都可以帮助这个代码有什么问题吗?

figure out that the library that was used modifies the unsigned char that was pass to it.找出使用的库修改了传递给它的无符号字符。 I have to use a copy of the var to have the correct HEX value other then the one i use to pass to the lib.我必须使用 var 的副本来获得正确的 HEX 值,而不是我用来传递给 lib 的值。

it was mentioned by the user chegewara in the git example here: here用户 chegewara 在 git 示例中提到了它: here

static void cbc()
{
#if defined(MBEDTLS_CIPHER_MODE_CBC)
    unsigned char iv[] = {0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
    unsigned char iv1[] = {0xff, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
    unsigned char encrypt_output[INPUT_LENGTH] = {0};
    unsigned char decrypt_output[INPUT_LENGTH] = {0};
    mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, INPUT_LENGTH, iv, input, encrypt_output);
    mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_DECRYPT, INPUT_LENGTH, iv1, encrypt_output, decrypt_output);
    ESP_LOG_BUFFER_HEX("cbc", encrypt_output, INPUT_LENGTH);
    ESP_LOG_BUFFER_HEX("cbc", decrypt_output, INPUT_LENGTH);
    ESP_LOGI("cbc", "%s", decrypt_output);
#endif
}

"Im guessing you made common mistake, which i did when i made that example too. Then IV is changed during encryption/decryption by code, which means you cant use the same char array to do both in the same code. Best way is to keep 1 copy as const char array, then before encrypt/decrypt copy it into arrays used in process." “我猜你犯了常见的错误,我在做那个例子时也犯了这个错误。然后在加密/解密期间通过代码更改 IV,这意味着你不能使用相同的 char 数组在相同的代码中执行这两个操作。最好的方法是保留 1 个副本作为 const char 数组,然后在加密/解密之前将其复制到进程中使用的 arrays 中。” - chegewara - 切格瓦拉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM