Help with loop..

Could you tell me what im doing wrong here.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void CCrypt::Encrypt(unsigned char* buffer, unsigned size )
{
	// check data correctness
	if ( !buffer )
		return;
	if ( size < 4 )
		return;

	unsigned char * encBuffer = new unsigned char[ size + 4 ];

	unsigned m = size + 4;

	// copy data from the encBuffer to Buffer  
	memcpy( encBuffer, buffer + 4, sizeof( unsigned char ) * m );

	const char unknown_key[] = "qmfaktnpgjs";
        unsigned char unknown_key2[sizeof(unknown_key)];
        std::copy(unknown_key, unknown_key + sizeof(unknown_key), unknown_key2);

	if ( encBuffer == NULL)
		return;
        if ( m < 4 )
		return;
       // perform header encrypting
	encBuffer[ 0 ] = m & 0xFF;
	encBuffer[ 1 ] = ( m & 0xFF00 ) >> 8;
	encBuffer[ 2 ] = 0x01;
	encBuffer[ 3 ] = 0x00;
	encBuffer[ 4 ] = ( unsigned char )( ( int )unknown_key2[ 0 % 0x0B ] ^ ( int )buffer[ 0 ] );
	
	for (unsigned i = 5; i < m; i++)
	{
		if ( encBuffer[i] != 0)
		{
			encBuffer[ i ] = ( unsigned char )( encBuffer[ i ] ^ encBuffer[ i - 1 ] );
		}
	}
	
	// swap buffers
	delete [] buffer;
	buffer = encBuffer;
	size = m;
}


what its suppose to do is encrypt the header once the size is giving. So say you have(BUFFER) data thats this..

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

now say i send this (Buffer) data thru that function..

 
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


Could u tell me why everything after the 5th array is wrong and looks nothing like the top one?
Last edited on
At least in the very beginning I see incorrect statement

1
2
	// copy data from the encBuffer to Buffer  
	memcpy( encBuffer, buffer + 4, sizeof( unsigned char ) * m );


In fact you are coping more bytes than buffer contains. Th size of buffer is size, but you are coping m bytes that is greater than size ( m = size + 4 );

In any case you should copy no more characters than buffer contains and it contains only size characters. Taking into account that you start coping from buffer + 4 then you should use size - 4 as the third argument.
updated Code..

Still no luck..

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void CCrypt::Encrypt(unsigned char* buffer, unsigned size )
{
	// check data correctness
	if ( !buffer )
		return;
	if ( size < 4 )
		return;

	unsigned char * encBuffer = new unsigned char[ size + 4 ];

	unsigned m = size + 4;

	// copy data from the Buffer to encBuffer  
	memcpy( encBuffer, buffer + 4, sizeof( unsigned char ) * m );

	const char unknown_key[] = "qmfaktnpgjs";
        unsigned char unknown_key2[sizeof(unknown_key)];
        std::copy(unknown_key, unknown_key + sizeof(unknown_key), unknown_key2);

	if ( encBuffer == NULL)
		return;
        if ( m < 4 )
		return;
       // perform header encrypting
	encBuffer[ 0 ] = m & 0xFF;
	encBuffer[ 1 ] = ( m & 0xFF00 ) >> 8;
	encBuffer[ 2 ] = 0x01;
	encBuffer[ 3 ] = 0x00;
	for (unsigned i = 4; i < m; i++)
	{
		if ( encBuffer[i] != 0)
		{
			encBuffer[ i ] = unknown_key2[i % 0xB] ^ encBuffer[i];
		}
	}
	
	// swap buffers
	delete [] buffer;
	buffer = encBuffer;
	size = m;
}


i put this in the buffer

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

and it turns out to this..

 
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 


and it should be this.
1
2
3
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
anyone have any suggestions?
Hi

What are doing , or what do you want to do ? do you expect your code not to change the buffer?


faceofdevil said
i put this in the buffer
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



faceofdevil said

and it should be this.
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


Sorry I don't understand what you are trying to do ! encrypting or not encrypting ?

Last edited on
Topic archived. No new replies allowed.