How can I #include a file with encrypted string?

拥有回忆 提交于 2020-01-03 05:20:07

问题


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 so 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.


回答1:


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]);
}



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6240283/how-can-i-include-a-file-with-encrypted-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!