0 Replies - 1960 Views - Last Post: 18 April 2009 - 07:40 PM Rate Topic: -----

#1 amiv4  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 19-February 09

Program to Hide a text File in a BMP File

Post icon  Posted 18 April 2009 - 07:40 PM

I need to write a program that takes a BMP file and then copys it but does some bit twiddling to hide a text message in it. The BMP file needs to come out the same size and look the same. I have a start on the program in the first code, but I am sure there is some wrong stuff. And then the second code is the decoder that I have to use to decode the BMP file and display the message. Any help on how to copy the BMP file and encode a text message

//Austin Mivshek
//COSC 1030 Program10
//BMP Coder

#include <iostream>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
#include <fstream>
using std::fstream;
#include <ios>
using std::ios;
using std::ios_base;
#include <string>
using std::string;
#include <limits>
using std::numeric_limits;

enum BMPErrorCode { FILE_OPEN_FAILED, FILE_NOT_BMP };

void OpenInputFile( fstream& In )
{
	string FileName;
	cout << "Enter input file name:  ";
	cin >> FileName;
	In.open( FileName.c_str(), ios::binary | ios::in);
	if ( In.fail() )
		throw( FILE_OPEN_FAILED );
}

void ConfirmBMP( fstream& In )
{
	char tmp1, tmp2;
	In.seekg(0);
	tmp1=In.get();
	tmp2=In.get();
	if ( tmp1!='B' || tmp2!='M' || In.fail() )
		throw( FILE_NOT_BMP );

	unsigned int startBitmap;
	In.seekg(10);
	In.read(reinterpret_cast<char* >(&startBitmap),4);
	In.seekg(startBitmap);
	if ( In.fail() )
		throw( FILE_NOT_BMP );
}

void CopyInputFile (fstream& In, fstream& Out)
{
	string DecodedFileName;
	OpenInputFile(In);
	unsigned int startBitmap;
	In.read(reinterpret_cast<char* >(&startBitmap),4);
	In.close();

	Out.open(DecodedFileName.c_str(),ios::binary | ios::out,4);
	Out.write(reinterpret_cast<char* >(&startBitmap),4);
	Out.close();
}

int main()
{
	fstream InputFile;
	fstream OutputFile;
	try 
	{
		OpenInputFile(InputFile);
		ConfirmBMP(InputFile);
		CopyInputFile(InputFile, OutputFile);
	}
	catch(BMPErrorCode ErrorCode) {
		switch(ErrorCode) {
			case FILE_OPEN_FAILED:
				cerr << "Error attempting to open input file" << endl;
				break;
			case FILE_NOT_BMP:
				InputFile.close();
				cerr << "Error in format of input file, not a BMP" << endl;
				break;
			default:
				cerr << "Unknown error" << endl;
		}
	}

	return 0;
}

// BMPDecoder.cpp
// Retrieve coded message from BMP graphics file.
// The "hidden" message is contained in the least significant
// bits of the bitmap RGB data.  If a data item is odd, that's
// a one-bit, if even, that's a zero-bit. Combine the bits
// from 8 contiguous data items together (big-endian order)
// to form a character.  End of message is a '$'.

#include <iostream>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
#include <fstream>
using std::fstream;
#include <ios>
using std::ios;
using std::ios_base;
#include <string>
using std::string;
#include <limits>
using std::numeric_limits;

enum BMPErrorCode { FILE_OPEN_FAILED, FILE_NOT_BMP };

void OpenInputFile( fstream& In )
{
	string FileName;
	cout << "Enter input file name:  ";
	cin >> FileName;
	In.open( FileName.c_str(), ios::binary | ios::in);
	if ( In.fail() )
		throw( FILE_OPEN_FAILED );
}

void ConfirmBMP( fstream& In )
{
	char tmp1, tmp2;
	In.seekg(0);
	tmp1=In.get();
	tmp2=In.get();
	if ( tmp1!='B' || tmp2!='M' || In.fail() )
		throw( FILE_NOT_BMP );

	unsigned int startBitmap;
	In.seekg(10);
	In.read(reinterpret_cast<char* >(&startBitmap),4);
	In.seekg(startBitmap);
	if ( In.fail() )
		throw( FILE_NOT_BMP );
}

char Decode(char* theBytes)
{
	int tmp=0;
	int thebit;
	for(int i=0; i<8; i++) {
		thebit = static_cast<unsigned char>(theBytes[i])%2;
		tmp = (tmp*2)+(thebit);
	}
	return static_cast<char>(tmp);
}
void ReadDecodeDisplay( fstream& In )
{
	char ByteList[8];
	char TheChar;
	bool done=false;
	In.exceptions( ios_base::failbit );
	while(!done) {
		try {
			In.read(ByteList, 8);
			TheChar=Decode( ByteList );
			if( TheChar=='$' )
				done=true;
			else
				cout << TheChar;
		}
		catch(ios_base::failure) {
			if( In.eof() )
				done=true;
		}
	}
}

int main()
{
	fstream InputFile;
	try {
		OpenInputFile( InputFile );
		ConfirmBMP( InputFile );
		ReadDecodeDisplay( InputFile );
		InputFile.close();
	}
	catch(BMPErrorCode ErrorCode) {
		switch(ErrorCode) {
			case FILE_OPEN_FAILED:
				cerr << "Error attempting to open input file" << endl;
				break;
			case FILE_NOT_BMP:
				InputFile.close();
				cerr << "Error in format of input file, not a BMP" << endl;
				break;
			default:
				cerr << "Unknown error" << endl;
		}
	}

	return 0;
}



Is This A Good Question/Topic? 1
  • +

Page 1 of 1