C++ Simple XOR crypt

Hello! I am intercepting packets and want to encrypt them using a simple XOR. The packets are not being encrypted, where am I going wrong?

void process_send(SOCKET s, const char* buf, int *len, int flags) {

			char* q;
			char j = 0;
			int l = sizeof(buf);
			char k;
			int i;
			q = (char *)buf;
			k = q[4];			

			q[4] ^= 0x76;
			for(i = 5; i < l; i++)
			j = q[i];
			q[i] ^= k;
			k = j;

Open in new window

Help-me :/
magdiel linharesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi magdiel linhares,

it's hard to help you without knowing what's going wrong. Could you describe the problem(s) you have with the code? Does it compile? Does it crash? Is the result unexpected?

Anyway, in at the code I think there's one thing which is probably an error:
int l = sizeof(buf);

Open in new window

I'm not sure what you expect here:  I guess you want to retrieve the length of the passed string - but, because in your case buf is a pointer to char, sizeof(buf) is the size of the pointer (i.e. 4 byte in 32-bit), not the length of the string. The length of the string (or of the reserved buffer for a string) seems to be passed by the calling function as len.

If you're in a 32-bit compiled program this means the for-loop will never be entered, because you start it with i=5, but l is 4, so i is never smaller than l.

Best regards,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
magdiel linharesAuthor Commented:
Nice! You really made me understand my mistake!
Fine. I'm glad I could help ...

Have a nice weekend,

evilrixSenior Software Engineer (Avast)Commented:
>> encrypt them using a simple XOR

Just an observation, but XOR is NOT encryption. It is obfuscation and very easy to reverse engineer. If you are looking to properly encrypt data this is NOT the way to do it!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.