Trouble with XOR
So here's my attempt at xoring some data. The heap gets corrupted when XOR is called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
void XOR(char*& data, char*& key, unsigned int datalength, unsigned int keylength, char*& enc_data)
{
enc_data= new char[datalength];
unsigned int keyindex=0;
for (unsigned int a=0; a<datalength; a++)
{
*(enc_data+a)=(*(data+a))^(*(key+keyindex));
keyindex++;
if (keyindex==keylength){keyindex=0;}
}
delete[] data;
}
int main()
{
char* key=new char[3];
*key='a';*(key+1)='b';*(key+2)='c';
char* data=new char[5];
*data='h';*(data+1)='e';*(data+2)='l';*(data+4)='l';*(data+5)='o';
char* enc_data=NULL;
XOR(data, key, 5, 3, enc_data);
delete[] key;
delete[] enc_data;
system("pause");
}
|
As always your help is appreciated
Replaced all chars with unsigned chars. The problem seems to occur when delete[] data is called.
*data='h';*(data+1)='e';*(data+2)='l';*(data+4)='l';*(data+5)='o';
Take a close look at this line.
Thank you cire. How did I not notice that..
Topic archived. No new replies allowed.