这将是我的第一个问题。我正在编写一个程序,它接收一个文件,对其进行加密,并将这些加密数据存储在新文件的资源中。此新文件将查找并解密加密数据。问题是当新文件试图解密加密数据时,我在写入时会遇到访问冲突。
将资源写入新文件:
#include <iostream>
#include <Windows.h>
using namespace std;
char * memblock; //The Buffer that will store the File's data
DWORD fileSize; // We will store the File size here
void readFile() //The Function that Reads the File and Copies the stub
{
DWORD bt;
wchar_t name[MAX_PATH];
wcin >> name;
CopyFile(L"stub.exe",L"encrypted.exe",0);
cout << "\nGetting the HANDLE of the file to be encrypted\n";
HANDLE efile= CreateFile(name,GENERIC_WRITE | GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
cout << "Getting the File size\n";
fileSize = GetFileSize(efile,NULL);
cout << "The File Size is: ";
cout << fileSize;
cout << " Bytes\n";
cout << "Allocating Memory for the ReadFile function\n";
memblock = new char[fileSize];
cout << "Reading the file\n";
ReadFile(efile,memblock,fileSize,&bt,NULL);
CloseHandle(efile);
}
void enc()
{
cout << "Encrypting the Data\n";
const char cipher[] = "random";
for (unsigned long i = 0; i < fileSize; i++)
{
int nKeyMod = i % strlen(cipher);
memblock[i] ^= cipher[nKeyMod];
}
}
void WriteToResources(LPTSTR szTargetPE, int id, LPBYTE lpBytes, DWORD dwSize)
{
cout << "Writing Encrypted data to stub's resources\n";
HANDLE hResource = NULL;
hResource = BeginUpdateResource(szTargetPE, FALSE);
if (hResource == NULL)
{
cout << "Could not open file for writing.\n";
system("PAUSE");
exit(0);
}
BOOL result;
result = UpdateResource(hResource,RT_RCDATA, MAKEINTRESOURCE(id),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPVOID) lpBytes,dwSize);
if (result == FALSE)
{
cout << "Could not add resource.\n";
system("PAUSE");
exit(0);
}
if (!EndUpdateResource(hResource, FALSE))
{
cout << "Could not write changes to file.\n";
system("PAUSE");
exit(0);
}
}
int main() // The main function (Entry point)
{
readFile();//Read the file
enc();//Encrypt it
WriteToResources(L"Crypted.exe",1,(BYTE *)memblock,fileSize);
cout << "The file is encrypted\n";
system("PAUSE");
return 0;
}
读取和解密资源:
#include <iostream>
#include <Windows.h>
unsigned long Rsize;
LPVOID RData;
void Resource(int id)
{
HRSRC hResource = NULL;
hResource = FindResource(NULL, MAKEINTRESOURCE(1), RT_RCDATA);
if (hResource == NULL)
{
MessageBox(NULL,L"Could not locate 'RT_RCDATA' resource",L"Error",MB_ICONERROR);
exit(0);
}
HGLOBAL temp = NULL;
temp = LoadResource(NULL, hResource);
if (temp == NULL)
{
MessageBox(NULL,L"Could not load the resource",L"Error",MB_ICONERROR);
exit(0);
}
Rsize = SizeofResource(NULL,hResource);
RData = LockResource(temp);
if (RData == NULL)
{
MessageBox(NULL,L"Could not lock dialog box",L"Error",MB_ICONERROR);
exit(0);
}
}
void decrypt()
{
char *cRData = (char*)RData;
char cipher[] = "random";
for (unsigned int i = 0; i < Rsize; i++)
{
int nKeyMod = i % strlen(cipher);
cRData[i] ^= cipher[nKeyMod];
}
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
Resource(1);
decrypt();
.....
return 0;
};