Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single Byte Manipulation 1

Status
Not open for further replies.

alanmusician

Programmer
Jun 30, 2005
17
US
I'm an old programmer but new to C++ and I'd like to ask a confused question. I have a chunk of data in a buffer in memory (type of LPVOID and virtually allocated with VirtualAlloc) that is read from an image file. This chunk of data is a tif file that has been encrypted by switching the two digits in each byte and changing the characters according to this table:

0-0
1-8
2-4
3-C
4-2
5-A
6-6
7-E
8-1
9-9
A-5
B-D
C-3
D-B
E-7
F-F

for example 0x97 becomes 0xE9.

What is the easiest way to convert this data into a usuable chunk? How can I scan through my buffer and convert each byte?
 
there are probably much more elegant ways.

but heres a quick stab

Code:
#include "stdafx.h"

void SubstituteByte(unsigned char &temp)
{
	switch(temp)
	{
		case 0x01: 
			temp=0x08;
			break;
		case 0x02: 
			temp=0x04;
			break;
		case 0x03: 
			temp=0x0C;
			break;
		case 0x04: 
			temp=0x02;
			break;
		case 0x05: 
			temp=0x0A;
			break;
		case 0x07: 
			temp=0x0E;
			break;
		case 0x08: 
			temp=0x01;
			break;
		case 0x0A: 
			temp=0x05;
			break;
		case 0x0B: 
			temp=0x0D;
			break;
		case 0x0C: 
			temp=0x03;
			break;
		case 0x0D: 
			temp=0x0B;
			break;
		case 0x0E: 
			temp=0x07;
			break;
		default:
			//no change to 0x00 0x06 0x09 0x0F
			break;
	}
}

int main(int argc, char* argv[])
{
	const unsigned char NIBBLEMASK1=0xF0;
	const unsigned char NIBBLEMASK2=0x0F;
	unsigned char TestByte=0x97;
	unsigned char TempByte=0x00;

	TempByte=TestByte & NIBBLEMASK1;
	TestByte&=NIBBLEMASK2;
	TempByte>>=4;

	SubstituteByte(TestByte);
	SubstituteByte(TempByte);

	TestByte<<=4;
	TestByte=TestByte | TempByte;
	
	return 0;
}

If somethings hard to do, its not worth doing - Homer Simpson
 
Code:
#include <iostream>
using namespace std;

unsigned char Convert( unsigned char byte )
{
	unsigned char reverse = 0x00;
	unsigned char mask = 0x10;

	for ( int i = 0; i < 4; ++i )
	{
		unsigned char temp = mask & (byte << (i * 2 + 1) );
		reverse |= temp;
		mask <<= 1;
	}

	return (reverse >> 4);
}

int main()
{
	for ( unsigned char byte = 0; byte < 16; ++byte )
	{
		cout << (unsigned int)byte << "-" << (unsigned int)Convert( byte ) << endl;
	}

	return 0;
}
 
I was just about to go to bed when I noticed that my code only does half the job you need. Here is something that should decrypt the whole byte instead of just the lower half:
Code:
#include <iostream>
using namespace std;

unsigned char Reverse( unsigned char byte )
{
	unsigned char reverse = 0x00;
	unsigned char mask = 0x10;

	for ( int i = 0; i < 4; ++i )
	{
		unsigned char temp = mask & (byte << (i * 2 + 1) );
		reverse |= temp;
		mask <<= 1;
	}

	return reverse;
}

unsigned char Decrypt( unsigned char byte )
{
	unsigned char high = Reverse( (0xF0 & byte) >> 4 ) >> 4;
	unsigned char low  = Reverse( 0x0F & byte );
	return (high | low);
}

int main()
{
	for ( unsigned char byte = 0; byte < 255; ++byte )
	{
		cout << (unsigned int)byte << "-" << (unsigned int)Decrypt( byte ) << endl;
	}

	return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top