我正在尝试制作一种简单的加密类型的东西。因此,我想做的是读取可执行文件的内容,对其进行加密,然后生成一个头文件,该文件将包含具有加密字节/二进制文件的变量,然后将其解密,等等。所以问题是如何将加密后的内容导出到头文件。因为例如,如果您尝试打印内容的字节表示形式,则可以使用
printf("%x", byte);
但是我认为您不能使用这种格式将字节存储在无符号字符中,因为通常的格式是
unsigned char bytes[] = {0x010, 0x038, 0x340 etc...}
在Python中,我可以做到,但是我似乎无法弄清楚如何直接在C语言中做到这一点。
如果您对来源有建议,请分享。
目前,我正在尝试关注Windows可执行文件,很可能会尝试在虚拟分配的内存上执行二进制代码,我已经看到了一些可执行的代码,所以我想尝试自己做。
你想要这样的东西吗:
#include <stdio.h>
int encode(int c)
{
return (unsigned char) (c ^ 0xf);
}
int main(int argc, char ** argv)
{
if (argc != 3) {
fprintf(stderr, "usage: %s <file in> <file out>\n", *argv);
}
else {
FILE * fpin;
FILE * fpout;
if ((fpin = fopen(argv[1], "rb")) == NULL) /* under Windows 'b' is necessary to read binary */
perror("cannot open inpout file");
else if ((fpout = fopen(argv[2], "w")) == NULL)
perror("cannot open inpout file");
else {
const char * sep = "unsigned char bytes[] = {";
int c;
while ((c = fgetc(fpin)) != EOF) {
fprintf(fpout, "%s0x%x", sep, encode(c));
sep = ", ";
}
fputs("};\n", fpout);
fclose(fpin);
fclose(fpout);
}
}
return 0;
}
当然修改编码
编译与执行:
pi@raspberrypi:/tmp $ gcc -Wall e.c
pi@raspberrypi:/tmp $ ./a.out ./a.out h
pi@raspberrypi:/tmp $ cat h
unsigned char bytes[] = {0x70, 0x4a, 0x43, 0x49, 0xe, 0xe, 0xe, 0xf ... 0xf, 0xf, 0xf, 0xf, 0xf};
pi@raspberrypi:/tmp $ ls -l h
-rw-r--r-- 1 pi pi 43677 juil. 4 18:44 h
(我剪切cat h
结果仅显示其开始和结束)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句