Click here to Skip to main content
15,884,054 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have made a simple XOR encryption to encrypt some packets, but decrypting does not work well... since i am a beginner in C++ please help me...

unsigned char cheimagice[] = { 0xCF, 0x10, 0x4E, 0x3A, 0xC2, 0xD8, 0x5F, 0xAD, 0xE4 };
    unsigned char test[] = { 0xC3, 0x18, 0xC7, 0xBE, 0x08, 0x1B, 0x25, 0xFF, 0x81, 0x55, 0xE0, 0xB5 };
    printf("[Criptare]: \n");
    printf("[INAINTE]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test[0], (unsigned char)test[1], (unsigned char)test[2], (unsigned char)test[3], (unsigned char)test[4], (unsigned char)test[5], (unsigned char)test[6], (unsigned char)test[7], (unsigned char)test[8], (unsigned char)test[9], (unsigned char)test[10], (unsigned char)test[11]);
    unsigned char *test1 = new unsigned char[sizeof(test)];
    unsigned char *test2 = new unsigned char[sizeof(test)];
    // criptare :)
    for (unsigned int i = 0; i < sizeof(test); ++i)
    {
        test1[i] ^= test[i - 1] ^ cheimagice[i % 9];
    }
    printf("[DUPA]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test1[0], (unsigned char)test1[1], (unsigned char)test1[2], (unsigned char)test1[3], (unsigned char)test1[4], (unsigned char)test1[5], (unsigned char)test1[6], (unsigned char)test1[7], (unsigned char)test1[8], (unsigned char)test1[9], (unsigned char)test1[10], (unsigned char)test1[11]);
    // decriptare :)
    printf("[Decriptare]: \n");
    printf("[INAINTE]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test1[0], (unsigned char)test1[1], (unsigned char)test1[2], (unsigned char)test1[3], (unsigned char)test1[4], (unsigned char)test1[5], (unsigned char)test1[6], (unsigned char)test1[7], (unsigned char)test1[8], (unsigned char)test1[9], (unsigned char)test1[10], (unsigned char)test1[11]);
    for (unsigned int i = 0; i < sizeof(test1); ++i)
    {
        test2[i] ^= test1[i - 1] ^ cheimagice[i % 9];
    }
    printf("[DUPA]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test2[0], (unsigned char)test2[1], (unsigned char)test2[2], (unsigned char)test2[3], (unsigned char)test2[4], (unsigned char)test2[5], (unsigned char)test2[6], (unsigned char)test2[7], (unsigned char)test2[8], (unsigned char)test2[9], (unsigned char)test2[10], (unsigned char)test2[11]);


The Output:

[Criptare]:
[INAINTE]: 0xC3 0x18 0xC7 0xBE 0x08 0x1B 0x25 0xFF 0x81 0x55 0xE0 0xB5
[DUPA]: 0xE3 0xD3 0x6C 0xFD 0x11 0xD0 0x29 0x88 0x21 0x4E 0x36 0xAE
[Decriptare]:
[INAINTE]: 0xE3 0xD3 0x6C 0xFD 0x11 0xD0 0x29 0x88 0x21 0x4E 0x36 0xAE
[DUPA]: 0x6C 0xF3 0x9D 0x56 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Posted
Comments
Jochen Arndt 20-May-15 8:48am    
You have access violations in test[i-1] and test1[i-1]. I don't know your algorithm but I assume that zero should be used instead when i is zero.

Decryption must be the reverse operation of encryption. I don't think that your code is correct because you are using identical operations and uses looping from zero at decryption. Because the previous value is used during encryption it might be necessary to start decryption at the last value. But again, I don't know your algorithm.
Frankie-C 20-May-15 9:02am    
It's a simple xoring of values with a salt, in this case of the same lenght of the sequence to encrypt.
So simply exoring the result wit the salt will give back the value:
10101 xor 01011 = 11110
11110 xor 01011 = 10101
Jochen Arndt 20-May-15 9:07am    
But not when x-oring also with a previous (already encrypted) value.
Frankie-C 20-May-15 10:01am    
You're right! I have not noticed it!
I made a fast test in debug mode, where the array test1 was filled with 0's...

1 solution

Why are you trying to address values outside the lower bound of the input array?
Here
C++
for (unsigned int i = 0; i < sizeof(test); ++i)
{
    test1[i] ^= test[i - 1] ^ cheimagice[i % 9];
}

and here
C++
for (unsigned int i = 0; i < sizeof(test1); ++i)
{
    test2[i] ^= test1[i - 1] ^ cheimagice[i % 9];
}

What should have been the scope? This should have also triggered a memory violation exception. Moreover Xoring contents of destination doesn't help to get the result.
[EDIT] I fixed the code:
C++
unsigned char cheimagice[] = { 0xCF, 0x10, 0x4E, 0x3A, 0xC2, 0xD8, 0x5F, 0xAD, 0xE4 };
		unsigned char test[] = { 0xC3, 0x18, 0xC7, 0xBE, 0x08, 0x1B, 0x25, 0xFF, 0x81, 0x55, 0xE0, 0xB5 };
		printf("[Criptare]: \n");
		printf("[INAINTE]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test[0], (unsigned char)test[1], (unsigned char)test[2], (unsigned char)test[3], (unsigned char)test[4], (unsigned char)test[5], (unsigned char)test[6], (unsigned char)test[7], (unsigned char)test[8], (unsigned char)test[9], (unsigned char)test[10], (unsigned char)test[11]);
		unsigned char *test1 = new unsigned char[sizeof(test)];
		unsigned char *test2 = new unsigned char[sizeof(test)];
	    // criptare :)
		for (unsigned int i = 0; i < sizeof(test); ++i)
		{
			test1[i] = test[i] ^ cheimagice[i % 9];
		}
		printf("[DUPA]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test1[0], (unsigned char)test1[1], (unsigned char)test1[2], (unsigned char)test1[3], (unsigned char)test1[4], (unsigned char)test1[5], (unsigned char)test1[6], (unsigned char)test1[7], (unsigned char)test1[8], (unsigned char)test1[9], (unsigned char)test1[10], (unsigned char)test1[11]);
	    // decriptare :)
		printf("[Decriptare]: \n");
		printf("[INAINTE]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test1[0], (unsigned char)test1[1], (unsigned char)test1[2], (unsigned char)test1[3], (unsigned char)test1[4], (unsigned char)test1[5], (unsigned char)test1[6], (unsigned char)test1[7], (unsigned char)test1[8], (unsigned char)test1[9], (unsigned char)test1[10], (unsigned char)test1[11]);
		for (unsigned int i = 0; i < sizeof(test1); ++i)
		{
			test2[i] = test1[i] ^ cheimagice[i % 9];
		}
		printf("[DUPA]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test2[0], (unsigned char)test2[1], (unsigned char)test2[2], (unsigned char)test2[3], (unsigned char)test2[4], (unsigned char)test2[5], (unsigned char)test2[6], (unsigned char)test2[7], (unsigned char)test2[8], (unsigned char)test2[9], (unsigned char)test2[10], (unsigned char)test2[11]);
 
Share this answer
 
v3
Comments
[no name] 20-May-15 8:53am    
Thank you... but does not work:

[Criptare]:
[INAINTE]: 0xC3 0x18 0xC7 0xBE 0x08 0x1B 0x25 0xFF 0x81 0x55 0xE0 0xB5
[DUPA]: 0x20 0x08 0xB3 0x84 0xA7 0xC3 0x17 0x52 0x5F 0x9A 0x83 0xFB
[Decriptare]:
[INAINTE]: 0x20 0x08 0xB3 0x84 0xA7 0xC3 0x17 0x52 0x5F 0x9A 0x83 0xFB
[DUPA]: 0xC0 0x18 0xFD 0xBE 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

I need this after encryption -> encrytion :

[INAINTE]: 0xC3 0x18 0xC7 0xBE 0x08 0x1B 0x25 0xFF 0x81 0x55 0xE0 0xB5
Frankie-C 20-May-15 10:06am    
You have made also another error XORing the data to encrypt with the destination value.
I fixed it now.
If it's ok accept the answer please.
[no name] 20-May-15 10:38am    
Thank you Frankie!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900