文件加密

新建MFC应用程序

image-20240710100204329

选择MFC EditBrowse Control

image-20240710100352565

修改ID

image-20240710100608377

image-20240710100822903

image-20240710101132137

image-20240710101339250

添加新建项目

image-20240710101927824

Encrypt.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

// EncryptUIDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "EncryptUI.h"
#include "EncryptUIDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持

// 实现
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CEncryptUIDlg 对话框



CEncryptUIDlg::CEncryptUIDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_ENCRYPTUI_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEncryptUIDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CEncryptUIDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(BTN_ENCRYPT, &CEncryptUIDlg::OnBnClickedEncrypt)
END_MESSAGE_MAP()


// CEncryptUIDlg 消息处理程序

BOOL CEncryptUIDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// 将“关于...”菜单项添加到系统菜单中。

// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标

// TODO: 在此添加额外的初始化代码

return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

void CEncryptUIDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。

void CEncryptUIDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CEncryptUIDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}



void CEncryptUIDlg::OnBnClickedEncrypt()
{
//获取文件路径
CString strFilePath;
GetDlgItemText(EDT_FILEPATH, strFilePath);
if (strFilePath.IsEmpty())
{
AfxMessageBox("路径不能为空!!");
return;
}
//启动工作进程
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = {};
CString strCmdLine("Work.exe \"" + strFilePath + "\"");
BOOL bRet = CreateProcess(NULL,
strCmdLine.GetBuffer(),
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
if (!bRet)
{
AfxMessageBox("启动工作进程失败,请重试!!");
return;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}

Work.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Work.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>

int main(int argc, char** argv)
{
getchar();
//判断
if (argc < 2)
{
return 0;
}
char* pFilePath = argv[1];//文件路径
}

运行EncryptUI.exe,选择一个文件,然后点击开始加密

image-20240710104336038

把work设为启动项目,下断点后调试选择附加到进程

image-20240710104522367

选择work.exe,然后附加

image-20240710104812786

调试后查看两个参数的值,分别为附加到进程的文件的名字和加密文件的地址

image-20240710105157804

添加新建项目

image-20240710105508397

添加头文件

image-20240710105826261

在属性页中找到目标的宏

image-20240710110121474

dllmain.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#include "common.h"

#pragma data_seg("EncryptValue")
CHAR g_szFilePath[MAX_PATH] = {};
#pragma data_seg()

#pragma comment(linker,"/SECTION:EncryptValue,RWS")
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

common.h

1
2
3
4
5
6
7
8
9
10
#pragma once
#include <windows.h>

#ifdef DLLCOMMOM_EXPORTS
#define COMMONAPI __declspec(dllexport)
#else
#define COMMONAPI __declspec(dllimport)
#endif

extern COMMONAPI CHAR g_szFilePath[MAX_PATH];

在EncryptUIDlg.h里面添加头文件

1
2
3
4
5
6
// EncryptUIDlg.h: 头文件
//

#pragma once
#include "../DllCommon/common.h"
#pragma comment(lib,"./debug/dllcommon.lib")

给Work.cpp添加头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Work.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "../DllCommon/common.h"
#pragma comment(lib,"../EncryptUI/debug/dllcommon.lib")

int main(int argc, char** argv)
{
getchar();
//判断
if (argc < 2)
{
return 0;
}
//char* pFilePath = argv[1];//文件路径
}

在EncryptUIDlg.h中新建变量

1
2
3
4
5
// 实现
protected:
HICON m_hIcon;
HANDLE m_hFileMap = NULL;//添加
LPVOID m_pBuff = NULL;//添加

修改项目生成顺序

EncryptUI依赖于DllCommon

image-20240710143303205

Work也依赖于DllCommon

image-20240710143419293

项目生成顺序如下

image-20240710143450096

work.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Work.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "../DllCommon/common.h"
#pragma comment(lib,"../EncryptUI/debug/Dllcommon.lib")

int main(int argc, char** argv)
{
getchar();
//判断
if (argc < 2)
{
return 0;
}
//char* pFilePath = argv[1];//文件路径
HANDLE hFileMap = OpenFileMapping(
FILE_MAP_READ | FILE_MAP_WRITE,
FALSE,
"EncryptShared");
if (hFileMap == NULL)
{
return 0;
}
}

在EncryptUIDlg中添加消息

1
2
3
4
enum MYMSG
{
MM_ENCRIPTFAILD = WM_USER + 1
};

类向导中添加自定义消息

image-20240710145519114

编辑代码

1
2
3
4
5
6
7
afx_msg LRESULT CEncryptUIDlg::OnMmEncriptfaild(WPARAM wParam, LPARAM lParam)
{
AfxMessageBox("加密失败!!");
UnmapViewOfFile(m_pBuff);
CloseHandle(m_hFileMap);
return 0;
}

image-20240710150049748

Work.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Work.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "../DllCommon/common.h"
#pragma comment(lib,"../EncryptUI/debug/Dllcommon.lib")

enum MYMSG
{
MM_ENCRIPTFAILD = WM_USER + 1
};

int main(int argc, char** argv)
{
getchar();
//判断
//if (argc < 2)
//{
// return 0;
//}

HWND hWndUi = FindWindow(NULL, "UI");
if (hWndUi == NULL)
{
return 0;
}
//char* pFilePath = argv[1];//文件路径
HANDLE hFileMap = OpenFileMapping(
FILE_MAP_READ | FILE_MAP_WRITE,
FALSE,
"EncryptShared");
if (hFileMap == NULL)
{
SendMessage(hWndUi, MM_ENCRIPTFAILD, NULL, NULL);
return 0;
}
LPVOID pViewBuff = MapViewOfFile(
hFileMap,
FILE_MAP_WRITE | FILE_MAP_READ,
0, 0,
0x1000
);
if (pViewBuff == NULL)
{
CloseHandle(hFileMap);
SendMessage(hWndUi, MM_ENCRIPTFAILD, NULL, NULL);
return 0;
}
char* pFilePath = (char*)pViewBuff;
return 0;
}

将SDL检查改为否

image-20240710151236301

EncryptUIDlg.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

// EncryptUIDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "EncryptUI.h"
#include "EncryptUIDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持

// 实现
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CEncryptUIDlg 对话框



CEncryptUIDlg::CEncryptUIDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_ENCRYPTUI_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEncryptUIDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CEncryptUIDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(BTN_ENCRYPT, &CEncryptUIDlg::OnBnClickedEncrypt)
ON_MESSAGE(MM_ENCRIPTFAILD, &CEncryptUIDlg::OnMmEncriptfaild)
END_MESSAGE_MAP()


// CEncryptUIDlg 消息处理程序

BOOL CEncryptUIDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// 将“关于...”菜单项添加到系统菜单中。

// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标

// TODO: 在此添加额外的初始化代码

return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

void CEncryptUIDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。

void CEncryptUIDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CEncryptUIDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}



void CEncryptUIDlg::OnBnClickedEncrypt()
{
//获取文件路径
CString strFilePath;
GetDlgItemText(EDT_FILEPATH, strFilePath);
if (strFilePath.IsEmpty())
{
AfxMessageBox("路径不能为空!!");
return;
}

#if 0
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = {};
CString strCmdLine("Work.exe \"" + strFilePath + "\"");
BOOL bRet = CreateProcess(NULL,
strCmdLine.GetBuffer(),
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
if (!bRet)
{
AfxMessageBox("启动工作进程失败,请重试!!");
return;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
#endif //使用命令行参数传入文件路径
//使用dll共享段,传入路径
//strcpy_s(g_szFilePath, strFilePath.GetBuffer());

//使用文件映射传入路径
m_hFileMap = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0, 0x1000,
"EncryptShared");
if (m_hFileMap == NULL)
{
AfxMessageBox("无法创建文件映射对象");
return;
}

m_pBuff = MapViewOfFile(m_hFileMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0x1000);;
if (m_pBuff == NULL)
{
CloseHandle(m_hFileMap);
AfxMessageBox("无法映射内存");
return;
}
strcpy((char*)m_pBuff, strFilePath.GetBuffer());
//启动工作进程
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = {};
CString strCmdLine("Work.exe \"" + strFilePath + "\"");
BOOL bRet = CreateProcess(NULL,
"Work.exe",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
if (!bRet)
{
AfxMessageBox("启动工作进程失败,请重试!!");
return;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}


afx_msg LRESULT CEncryptUIDlg::OnMmEncriptfaild(WPARAM wParam, LPARAM lParam)
{
AfxMessageBox("加密失败!!");
UnmapViewOfFile(m_pBuff);
CloseHandle(m_hFileMap);
return 0;
}

在EncryptUIDlg中给窗口添加名字

1
SetWindowText(_T("UI"));//在函数OnInitDialog()中

按照和上面相同的步骤运行(先运行EncryptUi.exe,然后打开压缩包,再把Work.exe附加到进程调试)然后调试可以拿到路径

image-20240710155521199

image-20240710160045278

image-20240710163432593

添加消息(Work.cpp和EncryptUIDlg.cpp都要)

1
2
3
4
5
enum MYMSG
{
MM_ENCRIPTFAILD = WM_USER + 1
MM_PERCENT
};

类向导中添加消息

image-20240710170123701

编辑代码

1
2
3
4
5
6
7
8
afx_msg LRESULT CEncryptUIDlg::OnMmPercent(WPARAM wParam, LPARAM lParam)
{
m_pcPercent.SetPos(wParam);
CString strFmt;
strFmt.Format("%d\%", wParam);
SetDlgItemText(LBL_PERCENT, strFmt);
return 0;
}

在对话框中选中进度条添加控制变量

image-20240710170539103

Work.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Work.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "../DllCommon/common.h"
#pragma comment(lib,"../EncryptUI/debug/Dllcommon.lib")

enum MYMSG
{
MM_ENCRIPTFAILD = WM_USER + 1,
MM_PERCENT
};

int main(int argc, char** argv)
{
getchar();
//判断
//if (argc < 2)
//{
// return 0;
//}

HWND hWndUi = FindWindow(NULL, "UI");
if (hWndUi == NULL)
{
return 0;
}
//char* pFilePath = argv[1];//文件路径
HANDLE hFileMap = OpenFileMapping(
FILE_MAP_READ | FILE_MAP_WRITE,
FALSE,
"EncryptShared");
if (hFileMap == NULL)
{
SendMessage(hWndUi, MM_ENCRIPTFAILD, NULL, NULL);
return 0;
}
LPVOID pViewBuff = MapViewOfFile(
hFileMap,
FILE_MAP_WRITE | FILE_MAP_READ,
0, 0,
0x1000
);
if (pViewBuff == NULL)
{
CloseHandle(hFileMap);
SendMessage(hWndUi, MM_ENCRIPTFAILD, NULL, NULL);
return 0;
}
char* pFilePath = (char*)pViewBuff;

HANDLE hFile = CreateFile(
pFilePath,
GENERIC_READ| GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hFile == INVALID_HANDLE_VALUE)
{
UnmapViewOfFile(pViewBuff);
CloseHandle(hFileMap);
SendMessage(hWndUi, MM_ENCRIPTFAILD, NULL, NULL);
return 0;
}
ULONGLONG nFileSize = 0;
GetFileSizeEx(hFile, (PLARGE_INTEGER)&nFileSize);

const DWORD dwBlockSize = 0x100000;//每次处理1M
LPBYTE pData = new BYTE[dwBlockSize];
ULONGLONG dwBytesHandled = 0;
while (dwBytesHandled < nFileSize)
{
//读取
DWORD dwBytesReaded = 0;
if (!ReadFile(hFile, pData, dwBlockSize, &dwBytesReaded, NULL))
{
CloseHandle(hFile);
UnmapViewOfFile(pViewBuff);
CloseHandle(hFileMap);
SendMessage(hWndUi, MM_ENCRIPTFAILD, NULL, NULL);
return 0;
}
//加密
for (DWORD i = 0; i < dwBytesReaded; ++i)
{
pData[i] ^= 0x55;
}
//移动回文件指针
SetFilePointerEx(hFile, *(PLARGE_INTEGER)&dwBytesHandled, NULL, FILE_BEGIN);

//写回文件
DWORD dwBytesWrited = 0;
if (!WriteFile(hFile, pData, dwBytesReaded, &dwBytesWrited, NULL))
{
CloseHandle(hFile);
UnmapViewOfFile(pViewBuff);
CloseHandle(hFileMap);
SendMessage(hWndUi, MM_ENCRIPTFAILD, NULL, NULL);
return 0;
}
//记录处理过的字节数
dwBytesHandled += dwBytesReaded;
//发送消息通知
DWORD dwPercent = (double)dwBytesHandled * 100 / nFileSize;
g_dwPos = dwPercent;
//SendMessage(hWndUi, MM_PERCENT, (WPARAM)dwPercent, NULL);
}
CloseHandle(hFile);
UnmapViewOfFile(pViewBuff);
CloseHandle(hFileMap);
return 0;
}

在dllmain.cpp中添加

1
2
3
4
#pragma data_seg("EncryptValue")
CHAR g_szFilePath[MAX_PATH] = {};
DWORD g_dwPos = 0;//添加
#pragma data_seg()

common.h中也添加

1
2
extern COMMONAPI CHAR g_szFilePath[MAX_PATH];
extern COMMONAPI DWORD g_dwPos;//添加

添加消息

image-20240710174505269

编辑代码

1
2
3
4
5
6
7
8
9
10
11
void CEncryptUIDlg::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1)
{
m_pcPercent.SetPos(g_dwPos);
CString strFmt;
strFmt.Format("%d%%", g_dwPos);
SetDlgItemText(LBL_PERCENT, strFmt);
}
CDialogEx::OnTimer(nIDEvent);
}

EncryptUIDlg.cpp完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

// EncryptUIDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "EncryptUI.h"
#include "EncryptUIDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持

// 实现
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CEncryptUIDlg 对话框



CEncryptUIDlg::CEncryptUIDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_ENCRYPTUI_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEncryptUIDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PROGRESS1, m_pcPercent);
}

BEGIN_MESSAGE_MAP(CEncryptUIDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(BTN_ENCRYPT, &CEncryptUIDlg::OnBnClickedEncrypt)
ON_MESSAGE(MM_ENCRIPTFAILD, &CEncryptUIDlg::OnMmEncriptfaild)
ON_MESSAGE(MM_PERCENT, &CEncryptUIDlg::OnMmPercent)
ON_WM_TIMER()
END_MESSAGE_MAP()


// CEncryptUIDlg 消息处理程序

BOOL CEncryptUIDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// 将“关于...”菜单项添加到系统菜单中。

// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标

// TODO: 在此添加额外的初始化代码
SetWindowText(_T("UI"));
m_pcPercent.SetRange(0, 100);
m_pcPercent.SetPos(0);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}

void CEncryptUIDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。

void CEncryptUIDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CEncryptUIDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}



void CEncryptUIDlg::OnBnClickedEncrypt()
{
//获取文件路径
CString strFilePath;
GetDlgItemText(EDT_FILEPATH, strFilePath);
if (strFilePath.IsEmpty())
{
AfxMessageBox("路径不能为空!!");
return;
}

#if 0
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = {};
CString strCmdLine("Work.exe \"" + strFilePath + "\"");
BOOL bRet = CreateProcess(NULL,
strCmdLine.GetBuffer(),
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
if (!bRet)
{
AfxMessageBox("启动工作进程失败,请重试!!");
return;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
#endif //使用命令行参数传入文件路径
//使用dll共享段,传入路径
//strcpy_s(g_szFilePath, strFilePath.GetBuffer());

//使用文件映射传入路径
m_hFileMap = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0, 0x1000,
"EncryptShared");
if (m_hFileMap == NULL)
{
AfxMessageBox("无法创建文件映射对象");
return;
}

m_pBuff = MapViewOfFile(m_hFileMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0x1000);;
if (m_pBuff == NULL)
{
CloseHandle(m_hFileMap);
AfxMessageBox("无法映射内存");
return;
}
strcpy((char*)m_pBuff, strFilePath.GetBuffer());
//启动工作进程
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = {};
CString strCmdLine("Work.exe \"" + strFilePath + "\"");
BOOL bRet = CreateProcess(NULL,
"Work.exe",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
if (!bRet)
{
AfxMessageBox("启动工作进程失败,请重试!!");
return;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
m_pcPercent.SetPos(0);
SetTimer(1, 100, NULL);
}


afx_msg LRESULT CEncryptUIDlg::OnMmEncriptfaild(WPARAM wParam, LPARAM lParam)
{
AfxMessageBox("加密失败!!");
UnmapViewOfFile(m_pBuff);
CloseHandle(m_hFileMap);
return 0;
}


afx_msg LRESULT CEncryptUIDlg::OnMmPercent(WPARAM wParam, LPARAM lParam)
{
m_pcPercent.SetPos(wParam);
CString strFmt;
strFmt.Format("%d%%", wParam);
SetDlgItemText(LBL_PERCENT, strFmt);
return 0;
}


void CEncryptUIDlg::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1)
{
m_pcPercent.SetPos(g_dwPos);
CString strFmt;
strFmt.Format("%d%%", g_dwPos);
SetDlgItemText(LBL_PERCENT, strFmt);
}
CDialogEx::OnTimer(nIDEvent);
}

重新生成后运行EncryptUI.exe吗,选择文件开始加密

image-20240711120210545

查看加密后的MD5值和加密前的MD5值,发现是一样的,加密成功

image-20240711120333767