![]() |
|
#1
|
|||
|
|||
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:
Last edited by dsmith : 27-Apr-2004 at 08:41.
Reason: Please use [c] & [/c] to highlight C code
|
|
#2
|
|||
|
|||
|
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:
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
|
|||
|
|||
thanksQuote:
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
|
||||
|
||||
|
Quote:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#5
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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