GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Apr-2004, 07:25
pablowablo pablowablo is offline
New Member
 
Join Date: Apr 2004
Posts: 24
pablowablo is on a distinguished road

Help with binary files (encryption?)


Let's say I open any file in any format(JPG, DOC, HTML) as a binary file.

My question is can I perform some encryption techniques on the binary file and then save it as another file. Then later on open that file then decrypt it and save it as the orignal file.

Any tips on how I approach this?


I tried some tests. Here's what I did so far, I open the binary file and store it in a character array. And then I have another array of characters... I copied the first character array to the second in reverse order (last element of 2nd goes to first of first array)..

When I write the second array(the reversed array) the file is naturally invalid (encrypted)..

Then I try to open the encrypted file and I reverse the process. Ideally, I will have the original file, however when I write the file, it is still invalid. Anyone know what went wrong? Is this even possible?


here's what I have so far


CPP / C++ / C Code:
#include 
#include 




void main()
{

long size = 0;
char *pBuffer = 0;


fstream myFile;
myFile.open ("input.jpg", ios::in | ios::out | ios::binary);

// struct stat results;

char ch = 'a';
//long size =0;


cout << size;
pBuffer = new char [81832];

myFile.read(pBuffer, 81832);
char *temp = new char[81832];

//temp[0] = 'a';
for(int i=1;i<81832;i++)
temp[i] = pBuffer[81832 - i]; //reverse ("encrypt") the file

// temp = pBuffer;
ofstream myFile2 ("output2.jpg", ios::out | ios::binary);
myFile2.write (temp, 81832);




}


Last edited by dsmith : 27-Apr-2004 at 08:41. Reason: Please use [c] & [/c] to highlight C code
  #2  
Old 27-Apr-2004, 08:44
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Hello and welcome pablo, yes it can be done and i just tried it

I have a file called sunset.jpg I took down its size on disk, not the actual size, which was 73728 bytes and then i copied it into a char buffer just like you did.
Then i created a temp buffer and reversed it just like you did except i started my for loop at i = 0 and ran it till 73728 - 2 bytes, that's because every file has an eof character at the end and we dont wanna copy the eof character at the end of your buffer into the start of your reverse buffer. once that was done, i manually copied the eof character from the end of the buffer to the end of the reverse buffer. Than i ran the program and it created revSunset.jpg
then i commented out the code, copied it and pasted it but i switched buffer read file name to revSunset.jpg and then the code reversed it again and put it in revrevSunset.jpg, and it displays the same image as the original Sunset.jpg
the surprising thing was that it ran perfectly on first compile and i dint have to make any changes, and it worked on the first try!!! amazing!

heres the code, i used most of your code, with some changes according to my preference:
i first ran the commented part, then i ran the identical code except with changes to file name the second time.
CPP / C++ / C Code:
#include<iostream>
#include<fstream>
using namespace std;



int main()
{
	
	/*char *buffer;
	buffer = new char[73728];
	ifstream is("Sunset.jpg",ios::binary);
	is.read(buffer,73728);
	is.close();
	
	char *temp = new char[73728];
	for(int i=0;i<73727;i++)
		temp[i]=buffer[73726-i];
	temp[73727] = buffer[73727];  //eof character
	
	ofstream os("revSunset.jpg",ios::binary);
	os.write(temp,73728);
	os.close();*/
	
	char *buffer;
	buffer = new char[73728];
	ifstream is("revSunset.jpg",ios::binary);
	is.read(buffer,73728);
	is.close();
	
	char *temp = new char[73728];
	for(int i=0;i<73727;i++)
		temp[i]=buffer[73726-i];
	temp[73727] = buffer[73727];  //eof character
	
	ofstream os("revrevSunset.jpg",ios::binary);
	os.write(temp,73728);
	os.close();
	
	
	
	
	return 0;
}
if you have winxp, i think you have Sunset.jpg somewhere on ur harddrive. try it.
Enjoy the sunset!!!

P.S. instead of looking up the file size and manually creating the same size buffer, you can use while loop till eof and have a counter which would count your file size, i think that would be much better.
  #3  
Old 27-Apr-2004, 09:01
pablowablo pablowablo is offline
New Member
 
Join Date: Apr 2004
Posts: 24
pablowablo is on a distinguished road

thanks


Quote:
Originally Posted by machinated
Hello and welcome pablo, yes it can be done and i just tried it

I have a file called sunset.jpg I took down its size on disk, not the actual size, which was 73728 bytes and then i copied it into a char buffer just like you did.
Then i created a temp buffer and reversed it just like you did except i started my for loop at i = 0 and ran it till 73728 - 2 bytes, that's because every file has an eof character at the end and we dont wanna copy the eof character at the end of your buffer into the start of your reverse buffer. once that was done, i manually copied the eof character from the end of the buffer to the end of the reverse buffer. Than i ran the program and it created revSunset.jpg
then i commented out the code, copied it and pasted it but i switched buffer read file name to revSunset.jpg and then the code reversed it again and put it in revrevSunset.jpg, and it displays the same image as the original Sunset.jpg
the surprising thing was that it ran perfectly on first compile and i dint have to make any changes, and it worked on the first try!!! amazing!

heres the code, i used most of your code, with some changes according to my preference:
i first ran the commented part, then i ran the identical code except with changes to file name the second time.
CPP / C++ / C Code:
#include<iostream>
#include<fstream>
using namespace std;



int main()
{
	
	/*char *buffer;
	buffer = new char[73728];
	ifstream is("Sunset.jpg",ios::binary);
	is.read(buffer,73728);
	is.close();
	
	char *temp = new char[73728];
	for(int i=0;i<73727;i++)
		temp[i]=buffer[73726-i];
	temp[73727] = buffer[73727];  //eof character
	
	ofstream os("revSunset.jpg",ios::binary);
	os.write(temp,73728);
	os.close();*/
	
	char *buffer;
	buffer = new char[73728];
	ifstream is("revSunset.jpg",ios::binary);
	is.read(buffer,73728);
	is.close();
	
	char *temp = new char[73728];
	for(int i=0;i<73727;i++)
		temp[i]=buffer[73726-i];
	temp[73727] = buffer[73727];  //eof character
	
	ofstream os("revrevSunset.jpg",ios::binary);
	os.write(temp,73728);
	os.close();
	
	
	
	
	return 0;
}
if you have winxp, i think you have Sunset.jpg somewhere on ur harddrive. try it.
Enjoy the sunset!!!

P.S. instead of looking up the file size and manually creating the same size buffer, you can use while loop till eof and have a counter which would count your file size, i think that would be much better.


Doh! I can't believe it was that simple...

At first I thought It was just not possible placing binary files in characters but then it turns out I just have to review my programming.. Lol thank you very much for the help.
  #4  
Old 28-Apr-2004, 00:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by machinated
I have a file called sunset.jpg I took down its size on disk, not the actual size, which was 73728 bytes and then i copied it into a char buffer just like you did.
Then i created a temp buffer and reversed it just like you did except i started my for loop at i = 0 and ran it till 73728 - 2 bytes, that's because every file has an eof character at the end and we dont wanna copy the eof character at the end of your buffer into the start of your reverse buffer.
Today there is no EOF character in most files. The system looks at the size of the file and simply reads that many bytes. I've checked a few files and the EOF character (ctrl-Z) is not present at the end.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #5  
Old 28-Apr-2004, 09:11
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by WaltP
Today there is no EOF character in most files. The system looks at the size of the file and simply reads that many bytes. I've checked a few files and the EOF character (ctrl-Z) is not present at the end.

Basically, a binary file does not have an EOF. Or rather it may have several EOF's but it doesn't mean what you think it means . I figured this out the hard way, when I was messing with binary files trying to use an EOF. So in order to read a binary file, you should get the filesize first.

However, with a text (ascii) file, I have always been able to use the EOF character. I thought it was standard MO on a text file.
  #6  
Old 28-Apr-2004, 09:51
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
ah thx for telling me, i dont much know about text files let alone binary files. i just assumed that every file had an eof character. But i am sure that the .eof() function works on all files.
  #7  
Old 28-Apr-2004, 23:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by machinated
ah thx for telling me, i dont much know about text files let alone binary files. i just assumed that every file had an eof character. But i am sure that the .eof() function works on all files.
Yes, EOF works on all files. You don't need to get the filesize to read the file. Just keep reading until EOF gets set. The only reason you might need the filesize is in the rare case you must input the entire file in one huge read.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
uploading files into server prinzekay MySQL / PHP Forum 5 16-Mar-2004 01:00
opening files and displaying text pin215 C++ Forum 7 21-Feb-2004 22:27
gxx linker accepts only 7 object files danielxs66 C++ Forum 1 12-Dec-2003 10:27
any one knows about the address of the deleted files in FAT shaam C++ Forum 1 09-Aug-2003 17:18
[Linux] How to upgrade an rpm with source files? JdS Computer Software Forum - Linux 2 18-Jun-2003 10:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:06.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.