Domanda

I'm trying to convert a ciphertext (from AES encryption) into a hexadecimal value.

I'm using the following code (state is the ciphertext) for the conversion:

foreach(char *v, state) {
    printf("%02X", *v);
}       

For this ciphertext:

    '4¥W[™úËj$Ó¶U6

I get this output:

    2734FFFFFFA5575BFFFFFF99FFFFFFFAFFFFFFCB6A240BFFFFFFD318FFFFFFB65536

Which is not the correct hex form for this ciphertext. I tried other conversion methods which all leads to the same result.

EDIT: foreach code:

#define foreach(item, array) \
for(int keep = 1, \
count = 0,\
size = sizeof (array) / sizeof *(array); \
keep && count != size; \
keep = !keep, count++) \
for(item = (array) + count; keep; keep = !keep)
È stato utile?

Soluzione

That is not C, there's no foreach in C. It might be C++, in which case the problem is that the argument to printf()'s %x conversion specifier is being sign-extended. Your platform likely has a signed char type.

Cast the value to unsigned char.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top