• 0

Binary File Help


Question

Hi, it's me again.

I want to make a binary file that is unreadable in any text editor.

i.e. I want to 'hide' a string in a file that can be read by my program, but not with notepad or anything.

Can any of you point me in the right direction here?

Link to comment
Share on other sites

21 answers to this question

Recommended Posts

  • 0

you can store the string in a structure then write it like so:

fwrite (&mystruct, (size_t)sizeof(mystruct),1,MYFILE);

to get it back:

fread (&mystruct, (size_t)sizeof(mystruct),1,MYFILE);

src

Link to comment
Share on other sites

  • 0
you can store the string in a structure then write it like so:

fwrite (&mystruct, (size_t)sizeof(mystruct),1,MYFILE);

to get it back:

fread (&mystruct, (size_t)sizeof(mystruct),1,MYFILE);

src

Tried. Failed.

Code:

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;

typedef struct{
char* str; 
}Foo;

int main(int argc, char *argv[])
{
	Foo mystruct;
	mystruct.str = "yoyo";

FILE* hFile = fopen ("aha.txt", "rwb");
if(!hFile){
cout<<"file not opened\n";
}else{		   
if(!fwrite(&mystruct,(size_t)sizeof(mystruct),1,hFile)){
cout <<"nope";
}
fclose(hFile);
}

system("pause");
return 0;
}

On the console I get:

nope

Press any key to continue. . .

Why won't it write?

Link to comment
Share on other sites

  • 0
It willl be readable, however, they'll see garbage.

...and do you know why it'll be garbage?

I'll give you a hint: not even the original application will be able to re-read that string.

Link to comment
Share on other sites

  • 0

Well, when I open the data file, I get a bunch of >> □ <<those, and I can clearly see the strings.

But, the int's i put in are unreadable by notepad but are readable by my program(yay?).

Is there a way to completely hide the strings?

Edited by dramonai
Link to comment
Share on other sites

  • 0
Is there a way to completely hide the strings?

Never. Though I hear there are a lot of misguided people working for the RIAA who seem to think that such a thing might be possible.

Link to comment
Share on other sites

  • 0
Never. Though I hear there are a lot of misguided people working for the RIAA who seem to think that such a thing might be possible.

Well, I was thinking of writing an int for each character i.e. a=1 b=2 c=3 and decoding it using strtok.

so like my file would have 1-2-3-4 and I would use strtok(myString, "-")

do you think it would work?

(I know it would take a seriously long time to code.)

Link to comment
Share on other sites

  • 0
Well, I was thinking of writing an int for each character i.e. a=1 b=2 c=3 and decoding it using strtok.

so like my file would have 1-2-3-4 and I would use strtok(myString, "-")

do you think it would work?

(I know it would take a seriously long time to code.)

Not really. If you're just looking at transposing characters such that a=1, b=2, etc, all you need to do is subtract 96 from each byte (a = ascii 97, b = ascii 98, etc) before writing it to your file.

But how badly do you want to prevent this from being read--because your approach is trivial to decode. You may want to look at an encryption library if you're serious about this.

Link to comment
Share on other sites

  • 0

Found a solution.

I found a library with file encrypt() and decrypt() functions.

I'll use the encrypt() function to encrypt the file after writing to it.

Then, use the decrypt() function to decrypt it before reading it.

Edited by dramonai
Link to comment
Share on other sites

  • 0
Found a solution.

I found a library with file encrypt() and decrypt() functions.

I'll use the encrypt() function to encrypt the file after writing to it.

Then, use the decrypt() function to decrypt it before reading it.

And the encryption key used by this routine is stored where? In your program? If so, it would take about five minutes to reverse engineer. As long as both the key and data are together, you cannot have security.

Link to comment
Share on other sites

  • 0
And the encryption key used by this routine is stored where? In your program? If so, it would take about five minutes to reverse engineer. As long as both the key and data are together, you cannot have security.

actually, it is stored in the encrypted file.

Link to comment
Share on other sites

  • 0

just out of curiosity whats with the added security? A binary file is more than enough to ward away the average user. Anyone with a low-level programming background can be simply patch, or as said earlier, reverse engineer the program. There are special compilers that supposedly ward off reversing, but honestly there's no point in it anyway. If the software is popular, its just a matter of time before it is cracked.

Nonetheless i still think its cool to play with encryption :).

Link to comment
Share on other sites

  • 0
actually, it is stored in the encrypted file.

So, if the key is stored in an encrypted file, then how will you decrypt the key? Where is the decryption key for that? This is a lot like CSS, where there are several silly layers of encryption like this, and we all know how well that worked out, eh? Look, when I said "never", I meant "never". It's futile. It's impossible. If your program can read the data, any program can read the data. Any tricks you try will just provide amusement and fodder for ridicule for anyone who is inclined to break it.

Link to comment
Share on other sites

  • 0
actually, it is stored in the encrypted file.

I think maybe you misunderstand slightly. The key is the information your program uses to decrypt the file. It can't be in the encrypted file, because then how would you read it?

Link to comment
Share on other sites

  • 0

Is it too late to take a step back and ask what this is for anyway? The way this is progressing, I'm thinking you might end up giving yourself a false sense of security.

Is something like TrueCrypt not applicable to your situation?

Link to comment
Share on other sites

  • 0
The key is at another struct (I didn't want to release the struct).

And I encrypt the first struct. NOT the second.

You're not really making any sense. If you are storing the key in a "struct" in your program, then you have released it. There is no protection there at all.

Or do you mean that you are literally writing two structs to the file? If so, that's even worse. Firstly because you've now created a non-portable file, and second because the key is still right there in plain text. No protection at all. You might as well just rot-13 the file.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.