|
-
June 1st, 2012, 11:08 AM
#6
Re: Using this XOR encryption in C++
 Originally Posted by qwerz
Sorry I did recognize it of course, but I just wanted to say that the second example that you coded was not an option for me.
Well, it is the same as the first example, except it's wrtten in a much more flexible way.
Well if it is working for you, then I will be making a mistake?
This is the full code that I am talking about.
Even if you didn't use ByteArray, there are problems with your code.
First, All of those Windows API calls are off-topic in this forum. Assume I know nothing about them or what they do.
Code:
LPVOID pBuffer = NULL;
//...
pBuffer = lpData;
So is pBuffer NULL terminated array of char? If not, then your original XOR function using char* would not have worked properly, since calling strlen() on data you aren't sure is NULL terminated leads to strlen() going off the deep-end trying to find the NULL.
If it is NULL terminated, then you need to create ByteArray's from this data. You can't just send it to the function, as raw char data is not a ByteArray. Remember that ByteArray is a vector -- a full-blown class that wraps an array, and isn't just an array. You have to get the raw data into the ByteArray object somehow. That's why I mentioned to you to study the interface of vector, so that you know how to convert from a raw type to a vector:
Code:
pBuffer = lpData;
ByteArray b1(pBuffer, pBuffer + strlen(pBuffer));
ByteArray b2(b, b + sizeof(b));
XOR(b1, b2);
Then you use b1 and b2 from there, as pBuffer is no longer needed at that point.
There is a constructor for vector that takes two arguments, where both arguments are iterators to starting and beginning of the sequence of values. In the example, you want to create a vector that consists of characters from pBuffer and from b.
The b1 vector is created by supplying the start of pBuffer and the end of pBuffer. The same with b2 -- the start of b and the end of b are provided. Then presto, you have the vector of characters.
Again, I'm assuming you know that pBuffer is NULL terminated. If it isn't, then you need to know the number of bytes so as to set up the b1 vector correctly (you can't use strlen()).
Code:
ByteArray b1(pBuffer, pBuffer + whatever_number_of_bytes);
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 1st, 2012 at 11:11 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|