二进制文件加密

背景:
1.bin文件,exe文件,图片文件等都可以使用二进制加密方式进行加密。
2.使用while循环来取代for循环进行字符加密。
3.exe文件加密解密的应用:先将exe文件伪装成txt文件,避免杀毒软件检测,再通过解密,txt–>exe

#define _CRT_SECURE_NO_WARNINGS
#define SRC_PATH "C:\\Users\\michael\\Desktop\\pis.exe"
#define CODE_PATH "C:\\Users\\michael\\Desktop\\pis11.exe"
#define DECODE_PATH "C:\\Users\\michael\\Desktop\\pis22.exe"
#define CODE_PATH_MUMA "C:\\Users\\michael\\Desktop\\pis11.txt"
#define DECODE_PATH_MUMA "C:\\Users\\michael\\Desktop\\pis33.exe"
#include 
#include 
#include 

void code_decode_file_with_psw(char* path, char* newpath,char* psw)
{
    FILE* pRead = fopen(path, "rb");
    FILE* pWrite = fopen(newpath, "wb");
    if (pRead == NULL || pWrite == NULL)
    {
        return;
    }
    else
    {
        int ch = 0;
        int newCh = 0;//加密或解密后的字符
        int pswLength = strlen(psw);        
        int index=0;

        while (!(ch = feof(pRead)))
        {
            ch = fgetc(pRead);
            newCh = ch ^psw[index%pswLength];
            fputc(newCh, pWrite);
            index++;
        }
    }

    fclose(pRead);
    fclose(pWrite);
    pRead = NULL;
    pWrite = NULL;

}
void main()
{
    code_decode_file_with_psw(SRC_PATH, CODE_PATH,"112");
    code_decode_file_with_psw(CODE_PATH, DECODE_PATH,"112");
    //在生成加密文件后,重命令exe-->txt,再利用解密将其还原为exe
    code_decode_file_with_psw(CODE_PATH_MUMA , CODE_PATH_MUMA,"112");
    system("pause");

}

你可能感兴趣的:(c)