code_ninja Posted July 31, 2009 Share Posted July 31, 2009 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 More sharing options...
0 ekw Posted July 31, 2009 Share Posted July 31, 2009 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 More sharing options...
0 code_ninja Posted July 31, 2009 Author Share Posted July 31, 2009 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 More sharing options...
0 code_ninja Posted July 31, 2009 Author Share Posted July 31, 2009 Found a solution. http://www.daniweb.com/code/snippet356.html Then, after looking at that I changed this line: FILE* hFile = fopen ("aha.txt", "rwb"); into FILE* hFile = fopen ("aha.txt", "wb"); and it worked. Thanks!!! :D Link to comment Share on other sites More sharing options...
0 _dandy_ Posted July 31, 2009 Share Posted July 31, 2009 Based on that code, you sure that file is "unreadable" by anything but your own app? Link to comment Share on other sites More sharing options...
0 Bazenga Posted July 31, 2009 Share Posted July 31, 2009 It willl be readable, however, they'll see garbage. Link to comment Share on other sites More sharing options...
0 _dandy_ Posted July 31, 2009 Share Posted July 31, 2009 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 More sharing options...
0 ViZioN Posted July 31, 2009 Share Posted July 31, 2009 ...and do you know why it'll be garbage? Because it's only a char* ? ;) Link to comment Share on other sites More sharing options...
0 code_ninja Posted July 31, 2009 Author Share Posted July 31, 2009 (edited) 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 July 31, 2009 by dramonai Link to comment Share on other sites More sharing options...
0 code.kliu.org Posted July 31, 2009 Share Posted July 31, 2009 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 More sharing options...
0 code_ninja Posted July 31, 2009 Author Share Posted July 31, 2009 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 More sharing options...
0 _dandy_ Posted July 31, 2009 Share Posted July 31, 2009 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 More sharing options...
0 code_ninja Posted July 31, 2009 Author Share Posted July 31, 2009 Let's put it this way. I NEED to make a file that is only read by my program. Link to comment Share on other sites More sharing options...
0 code_ninja Posted July 31, 2009 Author Share Posted July 31, 2009 (edited) 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 July 31, 2009 by dramonai Link to comment Share on other sites More sharing options...
0 hdood Posted July 31, 2009 Share Posted July 31, 2009 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 More sharing options...
0 code_ninja Posted August 1, 2009 Author Share Posted August 1, 2009 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 More sharing options...
0 ekw Posted August 1, 2009 Share Posted August 1, 2009 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 More sharing options...
0 code.kliu.org Posted August 1, 2009 Share Posted August 1, 2009 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 More sharing options...
0 hdood Posted August 1, 2009 Share Posted August 1, 2009 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 More sharing options...
0 _dandy_ Posted August 1, 2009 Share Posted August 1, 2009 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 More sharing options...
0 code_ninja Posted August 4, 2009 Author Share Posted August 4, 2009 The key is at another struct (I didn't want to release the struct). And I encrypt the first struct. NOT the second. Link to comment Share on other sites More sharing options...
0 hdood Posted August 5, 2009 Share Posted August 5, 2009 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 More sharing options...
Question
code_ninja
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