-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
532 lines (450 loc) · 21.3 KB
/
Copy pathMainForm.cs
File metadata and controls
532 lines (450 loc) · 21.3 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 YJZ
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
namespace FolderEncryptor
{
public sealed class MainForm : Form
{
private readonly TextBox encryptSource = new TextBox();
private readonly TextBox encryptOutput = new TextBox();
private readonly TextBox encryptPassword = new TextBox();
private readonly TextBox encryptConfirm = new TextBox();
private readonly Label replaceSourceLabel = new Label();
private readonly Button encryptButton = new Button();
private readonly TextBox decryptPackage = new TextBox();
private readonly TextBox decryptDestination = new TextBox();
private readonly TextBox decryptPassword = new TextBox();
private readonly Button decryptButton = new Button();
private readonly MenuStrip menuStrip = new MenuStrip();
private readonly ToolStripMenuItem languageMenu = new ToolStripMenuItem();
private readonly ToolStripMenuItem englishMenuItem = new ToolStripMenuItem();
private readonly ToolStripMenuItem chineseMenuItem = new ToolStripMenuItem();
private readonly TabControl mainTabs = new TabControl();
private readonly TextBox logBox = new TextBox();
private readonly Label sourceLabel = new Label();
private readonly Label encryptOutputLabel = new Label();
private readonly Label encryptPasswordLabel = new Label();
private readonly Label encryptConfirmLabel = new Label();
private readonly Label decryptPackageLabel = new Label();
private readonly Label decryptDestinationLabel = new Label();
private readonly Label decryptPasswordLabel = new Label();
private readonly Button encryptFileButton = new Button();
private readonly Button encryptFolderButton = new Button();
private readonly Button decryptPackageBrowseButton = new Button();
private readonly Button decryptDestinationBrowseButton = new Button();
private TabPage encryptPage;
private TabPage decryptPage;
public MainForm()
{
StartPosition = FormStartPosition.CenterScreen;
MinimumSize = new Size(600, 450);
Size = new Size(700, 520);
encryptOutput.ReadOnly = true;
encryptPassword.UseSystemPasswordChar = true;
encryptConfirm.UseSystemPasswordChar = true;
decryptPassword.UseSystemPasswordChar = true;
BuildMenu();
mainTabs.Dock = DockStyle.Fill;
mainTabs.TabPages.Add(BuildEncryptTab());
mainTabs.TabPages.Add(BuildDecryptTab());
logBox.Dock = DockStyle.Fill;
logBox.Multiline = true;
logBox.ReadOnly = true;
logBox.ScrollBars = ScrollBars.Vertical;
logBox.Font = new Font("Consolas", 9f);
TableLayoutPanel mainLayout = new TableLayoutPanel();
mainLayout.Dock = DockStyle.Fill;
mainLayout.ColumnCount = 1;
mainLayout.RowCount = 2;
mainLayout.Padding = new Padding(8);
mainLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 235));
mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
mainLayout.Controls.Add(mainTabs, 0, 0);
mainLayout.Controls.Add(logBox, 0, 1);
Controls.Add(mainLayout);
Controls.Add(menuStrip);
MainMenuStrip = menuStrip;
ApplyLanguage();
}
public void ShowEncryptTabAndBringToFront()
{
if (mainTabs.TabPages.Count > 0)
mainTabs.SelectedIndex = 0;
if (WindowState == FormWindowState.Minimized)
WindowState = FormWindowState.Normal;
Show();
Activate();
TopMost = true;
TopMost = false;
Focus();
}
private void BuildMenu()
{
menuStrip.Dock = DockStyle.Top;
englishMenuItem.CheckOnClick = false;
chineseMenuItem.CheckOnClick = false;
englishMenuItem.Click += delegate { SetLanguage(AppLanguage.English); };
chineseMenuItem.Click += delegate { SetLanguage(AppLanguage.Chinese); };
languageMenu.DropDownItems.Add(englishMenuItem);
languageMenu.DropDownItems.Add(chineseMenuItem);
menuStrip.Items.Add(languageMenu);
}
private void SetLanguage(AppLanguage language)
{
AppText.CurrentLanguage = language;
ApplyLanguage();
}
private void ApplyLanguage()
{
Text = AppText.Get("AppTitle");
languageMenu.Text = AppText.Get("Language");
englishMenuItem.Text = AppText.Get("English");
chineseMenuItem.Text = AppText.Get("Chinese");
englishMenuItem.Checked = AppText.CurrentLanguage == AppLanguage.English;
chineseMenuItem.Checked = AppText.CurrentLanguage == AppLanguage.Chinese;
if (encryptPage != null)
encryptPage.Text = AppText.Get("TabEncrypt");
if (decryptPage != null)
decryptPage.Text = AppText.Get("TabDecrypt");
sourceLabel.Text = AppText.Get("SourcePath");
encryptOutputLabel.Text = AppText.Get("OutputPackage");
encryptPasswordLabel.Text = AppText.Get("Password");
encryptConfirmLabel.Text = AppText.Get("ConfirmPassword");
decryptPackageLabel.Text = AppText.Get("Package");
decryptDestinationLabel.Text = AppText.Get("Destination");
decryptPasswordLabel.Text = AppText.Get("Password");
encryptFileButton.Text = AppText.Get("BrowseFile");
encryptFolderButton.Text = AppText.Get("BrowseFolder");
decryptPackageBrowseButton.Text = AppText.Get("BrowseChoose");
decryptDestinationBrowseButton.Text = AppText.Get("BrowseDirectory");
replaceSourceLabel.Text = AppText.Get("EncryptNote");
encryptButton.Text = AppText.Get("ButtonEncrypt");
decryptButton.Text = AppText.Get("ButtonDecrypt");
}
private TabPage BuildEncryptTab()
{
encryptPage = new TabPage();
TableLayoutPanel grid = CreateGrid(6);
AddSourceRow(grid, 0);
AddTextRow(grid, 1, encryptOutputLabel, encryptOutput);
AddTextRow(grid, 2, encryptPasswordLabel, encryptPassword);
AddTextRow(grid, 3, encryptConfirmLabel, encryptConfirm);
AddReplaceSourceRow(grid, 4);
encryptButton.Click += EncryptClicked;
grid.Controls.Add(encryptButton, 3, 5);
encryptPage.Controls.Add(grid);
return encryptPage;
}
private TabPage BuildDecryptTab()
{
decryptPage = new TabPage();
TableLayoutPanel grid = CreateGrid(5);
AddPathRow(grid, 0, decryptPackageLabel, decryptPackage, decryptPackageBrowseButton, BrowseDecryptPackage);
AddPathRow(grid, 1, decryptDestinationLabel, decryptDestination, decryptDestinationBrowseButton, BrowseDecryptDestination);
AddTextRow(grid, 2, decryptPasswordLabel, decryptPassword);
decryptButton.Click += DecryptClicked;
grid.Controls.Add(decryptButton, 3, 3);
decryptPage.Controls.Add(grid);
return decryptPage;
}
private static TableLayoutPanel CreateGrid(int rows)
{
TableLayoutPanel grid = new TableLayoutPanel();
grid.Dock = DockStyle.Fill;
grid.ColumnCount = 4;
grid.RowCount = rows;
grid.Padding = new Padding(8);
grid.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 96));
grid.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
grid.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 86));
grid.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 96));
for (int i = 0; i < rows; i++)
grid.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
return grid;
}
private void AddSourceRow(TableLayoutPanel grid, int row)
{
ConfigureLabel(sourceLabel);
grid.Controls.Add(sourceLabel, 0, row);
encryptSource.Anchor = AnchorStyles.Left | AnchorStyles.Right;
grid.Controls.Add(encryptSource, 1, row);
encryptFileButton.Click += BrowseEncryptFile;
grid.Controls.Add(encryptFileButton, 2, row);
encryptFolderButton.Click += BrowseEncryptFolder;
grid.Controls.Add(encryptFolderButton, 3, row);
}
private static void ConfigureLabel(Label labelControl)
{
labelControl.TextAlign = ContentAlignment.MiddleLeft;
labelControl.Dock = DockStyle.Fill;
}
private static void AddTextRow(TableLayoutPanel grid, int row, Label labelControl, TextBox textBox)
{
ConfigureLabel(labelControl);
grid.Controls.Add(labelControl, 0, row);
textBox.Anchor = AnchorStyles.Left | AnchorStyles.Right;
grid.SetColumnSpan(textBox, 3);
grid.Controls.Add(textBox, 1, row);
}
private void AddReplaceSourceRow(TableLayoutPanel grid, int row)
{
replaceSourceLabel.AutoSize = true;
replaceSourceLabel.Anchor = AnchorStyles.Left;
grid.SetColumnSpan(replaceSourceLabel, 3);
grid.Controls.Add(replaceSourceLabel, 1, row);
}
private static void AddPathRow(TableLayoutPanel grid, int row, Label labelControl, TextBox textBox, Button button, EventHandler handler)
{
ConfigureLabel(labelControl);
grid.Controls.Add(labelControl, 0, row);
textBox.Anchor = AnchorStyles.Left | AnchorStyles.Right;
grid.SetColumnSpan(textBox, 2);
grid.Controls.Add(textBox, 1, row);
button.Click += handler;
grid.Controls.Add(button, 3, row);
}
private void BrowseEncryptFile(object sender, EventArgs e)
{
HiddenPathDialog dialog = new HiddenPathDialog(AppText.Get("DialogEncryptFileTitle"), HiddenPathDialogMode.OpenFile, encryptSource.Text);
if (dialog.ShowDialog(this) != DialogResult.OK)
return;
encryptSource.Text = dialog.SelectedPath;
SetDefaultOutput(dialog.SelectedPath);
}
private void BrowseEncryptFolder(object sender, EventArgs e)
{
HiddenPathDialog dialog = new HiddenPathDialog(AppText.Get("DialogEncryptFolderTitle"), HiddenPathDialogMode.OpenFolder, encryptSource.Text);
if (dialog.ShowDialog(this) != DialogResult.OK)
return;
encryptSource.Text = dialog.SelectedPath;
SetDefaultOutput(dialog.SelectedPath);
}
private void SetDefaultOutput(string sourcePath)
{
encryptOutput.Text = GetSiblingPackagePath(sourcePath);
}
private void BrowseDecryptPackage(object sender, EventArgs e)
{
HiddenPathDialog dialog = new HiddenPathDialog(AppText.Get("DialogDecryptPackageTitle"), HiddenPathDialogMode.OpenFile, decryptPackage.Text);
if (dialog.ShowDialog(this) != DialogResult.OK)
return;
decryptPackage.Text = dialog.SelectedPath;
if (string.IsNullOrEmpty(decryptDestination.Text))
decryptDestination.Text = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), AppText.Get("DefaultDecryptOutputFolder"));
}
private void BrowseDecryptDestination(object sender, EventArgs e)
{
HiddenPathDialog dialog = new HiddenPathDialog(AppText.Get("DialogDecryptDestinationTitle"), HiddenPathDialogMode.OpenFolder, decryptDestination.Text);
if (dialog.ShowDialog(this) == DialogResult.OK)
decryptDestination.Text = dialog.SelectedPath;
}
private void EncryptClicked(object sender, EventArgs e)
{
string sourcePath = encryptSource.Text;
string outputPath = GetSiblingPackagePath(sourcePath);
string password = encryptPassword.Text;
encryptOutput.Text = outputPath;
if (!File.Exists(sourcePath) && !Directory.Exists(sourcePath))
{
MessageBox.Show(this, AppText.Get("SourceMissing"), AppText.Get("SourceMissingTitle"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(outputPath))
{
MessageBox.Show(this, AppText.Get("OutputPathInvalid"), AppText.Get("PathErrorTitle"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.Equals(Path.GetExtension(sourcePath), ".sfe", StringComparison.OrdinalIgnoreCase))
{
MessageBox.Show(this, AppText.Get("AlreadySfe"), AppText.Get("AlreadySfeTitle"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (password.Length < 8)
{
MessageBox.Show(this, AppText.Get("PasswordTooShort"), AppText.Get("PasswordTooShortTitle"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (password != encryptConfirm.Text)
{
MessageBox.Show(this, AppText.Get("PasswordMismatch"), AppText.Get("PasswordMismatchTitle"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (IsDangerousSourcePath(sourcePath))
{
MessageBox.Show(this, AppText.Get("DangerousSource"), AppText.Get("DangerousSourceTitle"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (File.Exists(outputPath))
{
DialogResult overwrite = MessageBox.Show(
this,
string.Format(AppText.Get("OverwritePackagePrompt"), outputPath),
AppText.Get("OverwritePackageTitle"),
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (overwrite != DialogResult.Yes)
return;
}
DialogResult confirm = MessageBox.Show(
this,
AppText.Get("ConfirmEncryptPrompt"),
AppText.Get("ConfirmEncryptTitle"),
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (confirm != DialogResult.Yes)
return;
RunWork(encryptButton, delegate
{
string temporaryPackage = CreateTemporaryPackagePath(outputPath);
try
{
FolderPackage.EncryptPath(sourcePath, temporaryPackage, password, Log);
FolderPackage.VerifyPackage(temporaryPackage, password);
ReplacePackageFile(temporaryPackage, outputPath);
Log(string.Format(AppText.Get("LogPackageVerified"), outputPath));
Log(string.Format(AppText.Get("LogSourceKept"), sourcePath));
}
finally
{
DeleteTemporaryPackage(temporaryPackage);
}
}, delegate
{
ShowEncryptionSuccessNotice(sourcePath, outputPath);
});
}
private void ShowEncryptionSuccessNotice(string sourcePath, string packagePath)
{
MessageBox.Show(
this,
string.Format(AppText.Get("EncryptSuccessMessage"), packagePath),
AppText.Get("EncryptSuccessTitle"),
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
private void DecryptClicked(object sender, EventArgs e)
{
if (!File.Exists(decryptPackage.Text))
{
MessageBox.Show(this, AppText.Get("PackageMissing"), AppText.Get("PackageMissingTitle"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(decryptDestination.Text))
{
MessageBox.Show(this, AppText.Get("DestinationMissing"), AppText.Get("DestinationMissingTitle"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
RunWork(decryptButton, delegate
{
FolderPackage.DecryptPackage(decryptPackage.Text, decryptDestination.Text, decryptPassword.Text, Log);
});
}
private static string GetSiblingPackagePath(string sourcePath)
{
if (string.IsNullOrEmpty(sourcePath))
return string.Empty;
string fullSource = Path.GetFullPath(sourcePath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));
string parent;
if (File.Exists(fullSource))
{
parent = Path.GetDirectoryName(fullSource);
}
else
{
DirectoryInfo parentInfo = Directory.GetParent(fullSource);
parent = parentInfo == null ? string.Empty : parentInfo.FullName;
}
string name = Path.GetFileName(fullSource);
if (string.IsNullOrEmpty(parent) || string.IsNullOrEmpty(name))
return string.Empty;
return Path.Combine(parent, name + ".sfe");
}
private static string CreateTemporaryPackagePath(string outputPath)
{
string directory = Path.GetDirectoryName(Path.GetFullPath(outputPath));
string name = Path.GetFileNameWithoutExtension(outputPath);
return Path.Combine(directory, name + "." + Guid.NewGuid().ToString("N") + ".tmp");
}
private static void ReplacePackageFile(string temporaryPackage, string outputPath)
{
string directory = Path.GetDirectoryName(Path.GetFullPath(outputPath));
if (!string.IsNullOrEmpty(directory))
Directory.CreateDirectory(directory);
if (File.Exists(outputPath))
{
File.SetAttributes(outputPath, FileAttributes.Normal);
File.Delete(outputPath);
}
File.SetAttributes(temporaryPackage, FileAttributes.Normal);
File.Move(temporaryPackage, outputPath);
}
private static void DeleteTemporaryPackage(string temporaryPackage)
{
if (string.IsNullOrEmpty(temporaryPackage) || !File.Exists(temporaryPackage))
return;
File.SetAttributes(temporaryPackage, FileAttributes.Normal);
File.Delete(temporaryPackage);
}
private static bool IsDangerousSourcePath(string sourcePath)
{
if (!Directory.Exists(sourcePath))
return false;
string full = TrimDirectoryEnd(Path.GetFullPath(sourcePath));
string root = TrimDirectoryEnd(Path.GetPathRoot(full));
string user = TrimDirectoryEnd(Environment.GetEnvironmentVariable("USERPROFILE"));
return string.Equals(full, root, StringComparison.OrdinalIgnoreCase) ||
string.Equals(full, user, StringComparison.OrdinalIgnoreCase);
}
private static string TrimDirectoryEnd(string path)
{
if (string.IsNullOrEmpty(path))
return path;
return path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
}
private void RunWork(Button activeButton, Action work)
{
RunWork(activeButton, work, null);
}
private void RunWork(Button activeButton, Action work, Action afterSuccess)
{
SetBusy(activeButton, true);
logBox.Clear();
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate { work(); };
worker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e)
{
SetBusy(activeButton, false);
if (e.Error != null)
{
Log(e.Error.Message);
MessageBox.Show(this, e.Error.Message, AppText.Get("ErrorTitle"), MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (afterSuccess != null)
afterSuccess();
else
MessageBox.Show(this, AppText.Get("OperationComplete"), AppText.Get("AppTitle"), MessageBoxButtons.OK, MessageBoxIcon.Information);
};
worker.RunWorkerAsync();
}
private void Log(string message)
{
if (InvokeRequired)
{
BeginInvoke(new Action<string>(Log), message);
return;
}
logBox.AppendText("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + message + Environment.NewLine);
}
private void SetBusy(Button activeButton, bool busy)
{
encryptButton.Enabled = !busy;
decryptButton.Enabled = !busy;
activeButton.Text = busy ? AppText.Get("Processing") : activeButton == encryptButton ? AppText.Get("ButtonEncrypt") : AppText.Get("ButtonDecrypt");
}
}
}