Archived

This topic is now archived and is closed to further replies.

Eckker

Removing a string from a file

Recommended Posts

Eckker    122
I''m trying to do a very simple image encrpytion by adding a string such as "don''thackme" at the beginning of my image file using a hex editor. This was a sugguested by somebody on a different forum. This indeeds stops me from being able to open the file using a paint program. However, how do I remove the string from the file so that my program can use it as normal. Please put me right if I''m way off track here. Thanks

Share this post


Link to post
Share on other sites
zackriggle    130
That's not exactly image encryption... but rather corrupting the image. I assume that by "adding a string such as .... at the beginning" you mean to prepend all of the image data. To undo this, I would open up the image file along with another file in Binary mode (ios::binary). Then, I would move the Get ptr (seekg()) to the first byte AFTER your string. Then, just read characters from the 'enctypted' file and write them to the output file.

Look at the following bit of code:

#include <fstream>
#include <stdio.h>

#define FILEIN "in.jpg"
#define FILEOUT "out.jpg"
#define MYSTRING "DontHackMe"

using namespace std;

void main()
{
// Create our file streams

fstream fin, fout;

// Open the files

fin.open( FILEIN, ios::binary | ios::in );
fout.open( FILEOUT, ios::binary | ios::out );
printf("Moving data from %s to %s...\n", FILEIN, FILEOUT);


// Make sure the files opened

if( fin.is_open() == false ||
fout.is_open() == false )
return;

// Move the 'get' (read) pointer to the first byte AFTER the string

fin.seekg( strlen(MYSTRING), ios::beg );

// Read the data and then write it until we have reached the end of the file.

char pBuffer [ 1024 ];
const int iBuffSz = 1024;

while( !fin.eof() )
{
// Read the data

fin.read( pBuffer, iBuffSz );

// Write the data

fout.write( pBuffer, fin.gcount() );

// Show status

printf("#");
}
}


[edited by - zackriggle on January 15, 2004 3:33:21 PM]

Share this post


Link to post
Share on other sites