Loop Issue..

I'm trying to run a crypto with some data inside it and i cant seem to get it to work correctly.

2b 00 01 00 70 af 8d 6a 00 65 66 71 00 00 00 00 00 67 00 2d 15 0d 15 08 0c 37 14 1b 0f 0d 00 00 00 00 00 00 00 00 00 00 00 00 00

I run this Data up top thru this function and get this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void CCrypt::DeCryptPacket( CPacket* pak )
{
	// Removing the Header..
	pak->Buffer.erase( pak->Buffer.begin(),pak->Buffer.begin()+4 );
	// check data correctness
	size_t decyptbufSize = pak->Buffer.size();
    if ( decyptbufSize < 4 )
        return;
	
	const char unknown_key[] = "qmfaktnpgjs";
    unsigned char unknown_key2[sizeof(unknown_key)];
    std::copy(unknown_key, unknown_key + sizeof(unknown_key), unknown_key2);
	
	if ( decyptbufSize < 4 )
		return;
	
	for (unsigned i = 0; i < pak->Buffer.size(); i++)
	{
		if ( pak->Buffer[i] != 0)
		{
			pak->Buffer[ i ] = unknown_key2[i % 0xB] ^ pak->Buffer[i];
		}
	}
}


My answer i get is this..

01 c2 eb 0b 00 11 08 01 00 00 00 00 00 01 00 46 61 63 65 6f 66 44 65 76 69 6c 00 00 00 00 00 00 00 00 00 00 00 00 00

Now when i try to run this top data thru this function
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
26
void CCrypt::EnCryptPacket( CPacket* pak )
{
	size_t encryptbufSize = pak->Buffer.size();
    if ( encryptbufSize < 4 )
        return;
	
	const char unknown_key[] = "qmfaktnpgjs";
    unsigned char unknown_key2[sizeof(unknown_key)];
    std::copy(unknown_key, unknown_key + sizeof(unknown_key), unknown_key2);

	if ( encryptbufSize < 4 )
		return;
	
	// perform XOR encrypting
	pak->Buffer[ 0 ] = pak->Buffer.size() & 0xFF;
	pak->Buffer[ 1 ] = ( pak->Buffer.size() & 0xFF00 ) >> 8;
	pak->Buffer[ 2 ] = 0x01;
	pak->Buffer[ 3 ] = 0x00;
	for (unsigned i = 4; i < pak->Buffer.size(); i++)
	{
		if ( pak->Buffer[i] != 0)
		{
			pak->Buffer[ i ] = unknown_key2[i % 0x0B] ^ pak->Buffer[i];
		}
	}
}


I get this as a answer
2b 00 01 00 6a b6 85 7b 00 7b 7b 70 00 00 00 00 00 6f 00 21 0b 10 14 02 00 25 0e 02 07 1c 00 00 00 00 00 00 00 00 00 00 00 00 00

When really i should be getting this as a answer
2b 00 01 00 70 af 8d 6a 00 65 66 71 00 00 00 00 00 67 00 2d 15 0d 15 08 0c 37 14 1b 0f 0d 00 00 00 00 00 00 00 00 00 00 00 00 00

Does anyone know why in the encrypt function that its not working corretly (loop?)

edit: please note that in another function before hand im adding 4 bytes in front of that buffer.
Last edited on
Hi

what does pak->Buffer.size(); return in the following loop, is it a number small or equal to 4 ??

1
2
3
4
5
6
7
for (unsigned i = 4; i < pak->Buffer.size(); i++)
	{
		if ( pak->Buffer[i] != 0)
		{
			pak->Buffer[ i ] = unknown_key2[i % 0x0B] ^ pak->Buffer[i];
		}
	}


Edit:: could you post the structure of CPacket ?
Last edited on
It returns the correct size of 43

1
2
3
4
5
6
7
8
9
struct CPacket
{
    public:
		std::vector<unsigned char> Buffer;
		unsigned char		Command;

public:
	CPacket( ) {}
	CPacket( unsigned short size ) : Buffer(size){ }
get some help please?
Hi

Run your programm in debug mode and try to find your bugs and errors, if you cant debug, provide me all your code( including the appication files) as zip file I will try to do it for you.
http://www.mediafire.com/?a3dd8ulte0e963a

here you can try it i believe it has to do with the i=4 instead probly i=0

So there for my only option would to xor the data then resize the buffer then swap or instert in front of the buffer the header?

I dont know mabye you'd have a suggestion since im working with STD::vector and i guess its bad to add data in front of existsing data on a STD::vetor
Hi

I dont have MS VS Studio, I may try to import the project into Eclipse and try to find the problem with GCC, but I need some time for that, currently I am just busy.
I believe the problem is with the loop.

 
for (unsigned i = 4; i < pak->Buffer.size(); i++)


Where i = 4

Your then xoring the array on the 4th
 
unknown_key2[i % 0x0B]


4 % 0x0b = answer. Instead it being 0 % 0x0b = answer..

Only thing i can think of is xor the array before i add in the xor header. But im alittle confused into getting that to work correctly.

Should i just store the header in a differnt pointer then point that data to the other vector and just insert that into the vector. Only problem is that i have to insert it to the front of the vector and vectors dont like data to be reinserted to the front messing up the eliment
Last edited on
by the way. which algorithm do you use, the name of algorithm that you use for encrytion and decryption.
None its just a custom algorithm. You should have no problems copy and pasteing it to Eclipse.
No Luck?
Hi

I have just import your code into eclipse, there is no problem with the loop itself, however there is a problem with the algorithm itself.

Is it your own algorithm ? I think I need to analyse your algorithm for it's stability and correctness.
Ya its just a custom algorithm. The stability should be fine but i have no idea why correctness sucks.

If its able to decrypt one way it shouldnt have a problem encrypting another way.
After fighting with it i was able to solve it.

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 CCrypt::EnCryptPacket( CPacket* pak )
{
	size_t encryptbufSize = pak->Buffer.size();
    if ( encryptbufSize < 4 )
        return;
	
	const char unknown_key[] = "qmfaktnpgjs";
    unsigned char unknown_key2[sizeof(unknown_key)];
    std::copy(unknown_key, unknown_key + sizeof(unknown_key), unknown_key2);

	unsigned char arr[4] ={ pak->Buffer.size()+4 & 0xFF,( pak->Buffer.size() & 0xFF00 ) >> 8,0x01,0x00};

	if ( encryptbufSize < 4 )
		return;
	
	for (unsigned i = 0; i < pak->Buffer.size(); i++)
	{
		if ( pak->Buffer[i] != 0)
		{
			pak->Buffer[ i ] = unknown_key2[i % 0x0B] ^ pak->Buffer[i];
		}
	}
	
	pak->Buffer.insert(pak->Buffer.begin(), arr, arr+4 ); // insert 4 values of arr into front of pak->Buffer vector
}


btw is it save to call
 
unsigned char arr[4] ={ pak->Buffer.size()+4 & 0xFF,( pak->Buffer.size() & 0xFF00 ) >> 8,0x01,0x00};


all the time or should i be zeroing it out each time as this function gets called a million times.
Topic archived. No new replies allowed.