How can I #include a file with encrypted string?
I am writing a C app on Windows with MinGW/gcc. I need to use the #include directive to include a file that contains an encrypted string. The string will be decrypted at runtime. I want to do this s开发者_如何学Goo the string will not be visible if looking at the executable with a hex editor.
I tried this, but it doesn't work. You get the idea, though:
char *hidden_str =
#include "encrypted_text.dat"
There may be unescaped stuff in there that's confusing the compiler. I don't know. I don't know what happens to it after it's encrypted.
If there's a better approach to this, I'm open to it.
If you installed and configured MSYS when you installed MinGW, you should have access to a command called xxd
, which takes a hex dump of a file. By using the -i
command line flag, you can get it to output a C-friendly file which you can easily import.
An example call would be:
xxd -i in_file output.h
The contents of output.h
would then look like:
unsigned char in_file[] = {
0x68, 0x65, 0x6c, 0x6c, 0x6f
};
unsigned int in_file_len = 5;
You can then include that file into your C source code and have access to it:
#include "output.h"
unsigned int i;
for(i = 0; i < in_file_len; i++)
{
printf("%c", in_file[i]);
}
The compiler doesn't like binary data in the file.
Try doing this instead:
char hidden_str[] = {
65, 66, 67, ...
};
It's not that difficult to write a simple program to generate the relevant lines of data from the .dat file, with a limited number of characters per line.
Encode it as hex/octal. I'm not sure what the simplest way would be to encode it -- you could use, eg, "\xAB\xCF...", the same with octal, or you could use an array initializer something like (don't quote me on this):
char hidden_str[64] = {0xAB, 0xCf, ...};
(You could even use decimal values for the initializer.)
Obviously, whatever you do you'll have to pre-process the encrypted text into a hex representation -- an easy job for a "toy" program.
精彩评论