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?
ZoppoConnect With a Mentor Commented:
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,

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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.