Xor and xnor confusion

Started by
2 comments, last by Headkaze 12 years, 2 months ago
I have a struct that I want to xor with a key before saving and then xnor back after loading.

For some reason the first xor and xnor does not produce the original results. But an additional call will. The problem appears to be related to the sign bit.

Here are the two functions

void XOR(unsigned char *data, unsigned int length, const string& key)
{
for (int k = 0, v = 0; v < length; v++)
{
data[v] = data[v] ^ key[k];
k = (++k < key.length() ? k : 0);
}
}

void XNOR(unsigned char *data, unsigned int length, const string& key)
{
for (int k = 0, v = 0; v < length; v++)
{
data[v] = ~(data[v] ^ key[k]);
k = (++k < key.length() ? k : 0);
}
}


And here are some tests

DEBUGLOG("1. %d", g_settings.Size);

XOR((unsigned char *)&g_settings, sizeof(Settings), KEY);

DEBUGLOG("2. %d", g_settings.Size);

XNOR((unsigned char *)&g_settings, sizeof(Settings), KEY);

DEBUGLOG("3. %d", g_settings.Size);

XOR((unsigned char *)&g_settings, sizeof(Settings), KEY);

DEBUGLOG("4. %d", g_settings.Size);

XNOR((unsigned char *)&g_settings, sizeof(Settings), KEY);

DEBUGLOG("5. %d", g_settings.Size);


The output I'm getting is

1. 1224
2. 977354226
3. -1225
4. -977354227
5. 1224
Advertisement
You'll have to think about how the logic works with XOR. Forget XNOR. original == (original ^ key) ^ key == original ^ (key ^ key) == original ^ (0) == original.

To load and save you should just use XOR.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
The opposite of XOR is XOR.
That might explain it. Thanks guys.

This topic is closed to new replies.

Advertisement