Fixing Converted Code [Solved]

Posts 16–19 of 19 · Page 2 of 2
Well was finally able to solve it and here it is...

Code:
void CCrypt::Decrypt(unsigned char* buffer, unsigned size )
{
	// check data correctness
	if ( !buffer )
		return;
	if ( size < 4 )
		return;

	unsigned char * decBuffer = new unsigned char[ size - 4 ];

	unsigned m = size - 4;

	// copy data from the decBuffer to Buffer  
	memcpy( decBuffer, 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 ( decBuffer == NULL)
		return;
        if ( m < 4 )
		return;
	
	for (unsigned i = 0; i < m; i++)
	{
		if ( decBuffer[i] != 0)
		{
			decBuffer[ i ] = unknown_key2[i % 0xB] ^ decBuffer[i];
		}
	}
	
	// swap buffers
	delete [] buffer;
	buffer = decBuffer;
	size = m;
}
GG
/Marked Solved.
That's really messy code. unknown_key should be a private member of the CCrypto class not a local variable. The last two lines also do nothing. Once the function returns your changes to buffer and size will disappear as they are local as well.
Quote Originally Posted by Fovea View Post
That's really messy code. unknown_key should be a private member of the CCrypto class not a local variable. The last two lines also do nothing. Once the function returns your changes to buffer and size will disappear as they are local as well.

How is it messy code? The last two lines swap pointers and then from there those pointers are used to access more functions? Those 2 functions arnt local there just local based inside the function outside the function 2 differeent pointers are passing that data along. Plus my decontructor does all the zero'in and clearing off all pointers once im done with the data.

As for the unkown_key ya you're probly right i could of stuck it in the private member but seeing as this function gets accessed idk... 50 times a sec who knows.. (this C++ is a server base application)..

Also can someone tell me whats wrong with my loop and why its not using the decoded key to xor the remaning buffer...

Code:
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;
	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;
}
this here..
Code:
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
should turn into this...
Code:
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
At wich it doesn't..... Any help will be thankfull....
Posts 16–19 of 19 · Page 2 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us