直接上代码吧,写的时候遇到了点问题, 找到根源以后,就搞定了,
注意define的 语法, 错的挺郁闷的
// 20180110_01.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <windows.h> #include <stdio.h> #include <stdlib.h> #define FileKey 0x86 //加载PE文件到内存中,并获取文件长度 LPVOID LoadPEFile(LPSTR lpszFile,DWORD &fileSize) { FILE *pFile = NULL; //DWORD fileSize = 0; LPVOID pFileBuffer = NULL; //打开文件 pFile = fopen(lpszFile,"rb"); if(!pFile) { printf("无法打开文件EXE文件"); return NULL; } fseek(pFile,0,SEEK_END); fileSize = ftell(pFile); fseek(pFile,0,SEEK_SET); //分配缓冲区 pFileBuffer = malloc(fileSize); if(!pFileBuffer) { printf("分配空间失败!\n"); fclose(pFile); return NULL; } //文件读取 size_t n = fread(pFileBuffer,fileSize,1,pFile); if(!n) { printf("读取数据失败\n"); free(pFileBuffer); fclose(pFile); return NULL; } //关闭文件 fclose(pFile); return pFileBuffer; } //内存直接写入到文件 void WirteToFile(LPVOID pFileBuffer,size_t fileSize,LPSTR lpszFile) { FILE *pFile = NULL; //打开文件 pFile = fopen(lpszFile,"wb"); if(!pFile) { printf("无法打开文件EXE文件"); return; } size_t writeSize = fwrite(pFileBuffer,fileSize,1,pFile); printf("WirteSize:%d\n",writeSize); //关闭文件 fclose(pFile); return; } int main(int argc, char* argv[]) { //printf("Hello World!\n"); //TCHAR srcFilePath = {0}; LPSTR filePath = "C://noteTemp.exe"; LPSTR outPath = "C://noteTemp1.exe"; DWORD fileSize = 0; LPVOID pFileBuffer = LoadPEFile(filePath,fileSize); CHAR* pOld = (CHAR*)pFileBuffer; LPVOID pFileBufferNew = malloc(fileSize); memset(pFileBufferNew,fileSize,0); CHAR* pNew = (CHAR*)pFileBufferNew; //数据加密 for(int i=0;i<(int)fileSize;i++) { pNew[i] = pOld[i]^FileKey; //pOld[i]^=FileKey; //int a = pOld[i]^FileKey; //memcpy(pNew+i,pOld+i,1); } WirteToFile(pFileBufferNew,fileSize,outPath); return 0; }
0则评论给“异或文件加密测试”