EaseFilter Demo Project
FormFileCrypt.cs
Go to the documentation of this file.
1 //
3 // (C) Copyright 2012 EaseFilter Technologies Inc.
4 // All Rights Reserved
5 //
6 // This software is part of a licensed software product and may
7 // only be used or copied in accordance with the terms of that license.
8 //
10 
11 using System;
12 using System.IO;
13 using System.Collections.Generic;
14 using System.ComponentModel;
15 using System.Data;
16 using System.Drawing;
17 using System.Linq;
18 using System.Text;
19 using System.Windows.Forms;
20 using System.Configuration;
21 
23 
24 namespace AutoFileCryptTool
25 {
26  public partial class Form_FileCrypt : Form
27  {
28  bool isServiceRunning = true;
29 
30  public Form_FileCrypt()
31  {
32  GlobalConfig.filterType = FilterAPI.FilterType.FILE_SYSTEM_CONTROL_ENCRYPTION;
33 
34  InitializeComponent();
35 
36  StartPosition = FormStartPosition.CenterScreen;
37 
38  WebFormServices.GetUpdatedInfo();
39 
40  string lastError = string.Empty;
41  if (!FilterDriverService.StartFilterService(out lastError))
42  {
43  isServiceRunning = false;
44 
45  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
46  MessageBox.Show("Start encryption service failed with error:" + lastError + ", auto folder encryption service will stop.", "Auto FileCrypt Service", MessageBoxButtons.OK, MessageBoxIcon.Error);
47  }
48 
49 
50  InitializeFileCrypt();
51 
52  }
53 
54  private void InitializeFileCrypt()
55  {
56  if (GlobalConfig.FilterRules.Count > 0)
57  {
58  //delete the predefined items for users.
59  listView_Folders.Items.Clear();
60  }
61 
62  foreach (string folder in GlobalConfig.FilterRules.Keys)
63  {
64  string folderName = folder;
65  if (folderName.EndsWith("\\*"))
66  {
67  folderName = folderName.Substring(0, folderName.Length - 2);
68  }
69 
70  ListViewItem item = new ListViewItem(folderName);
71  item.ImageIndex = 0;
72  listView_Folders.Items.Add(item);
73  }
74 
75  foreach (uint pid in GlobalConfig.IncludePidList)
76  {
77  if (textBox_IncludePID.Text.Length > 0)
78  {
79  textBox_IncludePID.Text += ";";
80  }
81 
82  textBox_IncludePID.Text += pid.ToString();
83  }
84 
85  foreach (uint pid in GlobalConfig.ExcludePidList)
86  {
87  if (textBox_ExcludePID.Text.Length > 0)
88  {
89  textBox_ExcludePID.Text += ";";
90  }
91 
92  textBox_ExcludePID.Text += pid.ToString();
93  }
94 
95  textBox_ExcludedUsers.Text = GlobalConfig.ExcludedUsers;
96  textBox_IncludedUsers.Text = GlobalConfig.IncludedUsers;
97 
98  label_VersionInfo.Text = "Current version:" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + ",© 2016EaseFilter Inc. All rights reserved.";
99 
100  if (!isServiceRunning)
101  {
102  listView_Folders.Items.Clear();
103  string warningMessage = "The encryption service failed to start.";
104  string message2 = "Please run with administrator permission.";
105  ListViewItem item = new ListViewItem(warningMessage);
106  listView_Folders.Items.Add(item);
107  item = new ListViewItem(message2);
108  listView_Folders.Items.Add(item);
109  }
110 
111 
112  }
113 
114  private bool AddEncyrptFolder(string folderName)
115  {
116  if (GlobalConfig.FilterRules.Count == 0)
117  {
118  //delete the predefined items for users.
119  listView_Folders.Items.Clear();
120  }
121 
122  uint accessFlags = FilterAPI.ALLOW_MAX_RIGHT_ACCESS|(uint)FilterAPI.AccessFlag.FILE_ENCRYPTION_RULE;
123  string includeFilterMask = folderName + "\\*";
124 
125  if (!GlobalConfig.IsFilterRuleExist(includeFilterMask))
126  {
127  FilterRule filterRule = new FilterRule();
128  filterRule.IncludeFileFilterMask = includeFilterMask;
129  filterRule.ExcludeFileFilterMasks = "";
131  filterRule.IncludeProcessNames = "";
132  filterRule.ExcludeProcessNames = "";
133  filterRule.IncludeProcessIds = "";
134  filterRule.ExcludeProcessIds = "";
135  filterRule.HiddenFileFilterMasks = "";
136  filterRule.AccessFlag = accessFlags;
137  filterRule.EventType = 0;
138  GlobalConfig.AddFilterRule(filterRule);
139 
141 
142  ListViewItem item = new ListViewItem(folderName);
143  item.ImageIndex = 0;
144  listView_Folders.Items.Add(item);
145 
146  listView_Folders.EnsureVisible(listView_Folders.Items.Count - 1);
147 
148  return true;
149  }
150  else
151  {
152  return false;
153  }
154  }
155 
156  private void button_AddFolder_Click(object sender, EventArgs e)
157  {
158  FolderBrowserDialog fdDiaglog = new FolderBrowserDialog();
159  if (fdDiaglog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
160  {
161  string folderName = fdDiaglog.SelectedPath;
162  AddEncyrptFolder(folderName);
163  }
164  }
165 
166  private void RemoveEncyrptFolder(string folderName)
167  {
168  GlobalConfig.RemoveFilterRule(folderName + "\\*");
170  }
171 
172  private void button_RemoveFolder_Click(object sender, EventArgs e)
173  {
174  foreach (ListViewItem item in listView_Folders.SelectedItems)
175  {
176  string folderName = item.Text;
177  RemoveEncyrptFolder(folderName);
178 
179  listView_Folders.Items.Remove(item);
180  }
181  }
182 
183  private void listView_Folders_DragDrop(object sender, DragEventArgs e)
184  {
185  string[] fileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
186 
187  if (null != fileList)
188  {
189  foreach (string folder in fileList)
190  {
191  if (Directory.Exists(folder))
192  {
193  AddEncyrptFolder(folder);
194  }
195  }
196  }
197 
198  }
199 
200  private void listView_Folders_DragEnter(object sender, DragEventArgs e)
201  {
202  e.Effect = DragDropEffects.Move;
203  }
204 
205  private void button_StartToEncrypt_Click(object sender, EventArgs e)
206  {
207  try
208  {
209  string fileName = textBox_FileNameToEncrypt.Text;
210  string passwordPhrase = textBox_PasswordToEncrypt.Text;
211  byte[] key = Utils.GetKeyByPassPhrase(passwordPhrase);
212 
213  if (!FilterAPI.AESEncryptFile(fileName, (uint)key.Length, key, (uint)FilterAPI.DEFAULT_IV_TAG.Length, FilterAPI.DEFAULT_IV_TAG, false))
214  {
215  string lastError = "Encrypt file " + fileName + " failed with error:" + FilterAPI.GetLastErrorMessage();
216  ShowMessage(lastError, MessageBoxIcon.Error);
217  }
218  else
219  {
220  string lastError = "Encrypt file " + fileName + " succeeded.";
221  ShowMessage(lastError, MessageBoxIcon.Information);
222  }
223  }
224  catch (Exception ex)
225  {
226  string lastError = "Encrypt file failed with error:" + ex.Message;
227  ShowMessage(lastError, MessageBoxIcon.Error);
228  }
229  }
230 
231  private void button_BrowseEncryptFile_Click(object sender, EventArgs e)
232  {
233  OpenFileDialog fileDiag = new OpenFileDialog();
234  if (fileDiag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
235  {
236  textBox_FileNameToEncrypt.Text = fileDiag.FileName;
237  }
238  }
239 
240  private void button_BrowseFileToDecrypt_Click(object sender, EventArgs e)
241  {
242  OpenFileDialog fileDiag = new OpenFileDialog();
243  if (fileDiag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
244  {
245  textBox_FileNameToDecrypt.Text = fileDiag.FileName;
246  }
247  }
248 
249  private void button_StartToDecrypt_Click(object sender, EventArgs e)
250  {
251  try
252  {
253  string fileName = textBox_FileNameToDecrypt.Text;
254  string passwordPhrase = textBox_PasswordToDecrypt.Text;
255  byte[] key = Utils.GetKeyByPassPhrase(passwordPhrase);
256 
257  if (!FilterAPI.AESEncryptFile(fileName, (uint)key.Length, key, (uint)FilterAPI.DEFAULT_IV_TAG.Length, FilterAPI.DEFAULT_IV_TAG, false))
258  {
259  string lastError = "Decrypt file " + fileName + " failed with error:" + FilterAPI.GetLastErrorMessage();
260  ShowMessage(lastError, MessageBoxIcon.Error);
261  }
262  else
263  {
264  string lastError = "Decrypt file " + fileName + " succeeded.";
265  ShowMessage(lastError, MessageBoxIcon.Information);
266  }
267  }
268  catch (Exception ex)
269  {
270  string lastError = "Decrypt file failed with error:" + ex.Message;
271  ShowMessage(lastError, MessageBoxIcon.Error);
272  }
273  }
274 
275  private void ShowMessage(string message,MessageBoxIcon messageIcon)
276  {
277  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
278  MessageBox.Show(message, "Message", MessageBoxButtons.OK, messageIcon);
279  }
280 
281  private void checkBox_ShowPasswordToEncrypt_CheckedChanged(object sender, EventArgs e)
282  {
283  if (checkBox_ShowPasswordToEncrypt.Checked)
284  {
285  textBox_PasswordToEncrypt.UseSystemPasswordChar = false;
286  }
287  else
288  {
289  textBox_PasswordToEncrypt.UseSystemPasswordChar = true;
290  }
291  }
292 
293  private void checkBox_ShowPasswordToDecrypt_CheckedChanged(object sender, EventArgs e)
294  {
295  if (checkBox_ShowPasswordToDecrypt.Checked)
296  {
297  textBox_PasswordToDecrypt.UseSystemPasswordChar = false;
298  }
299  else
300  {
301  textBox_PasswordToDecrypt.UseSystemPasswordChar = true;
302  }
303  }
304 
305  private void linkLabel_Report_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
306  {
307  System.Diagnostics.Process.Start("http://www.easefilter.com/ReportIssue.htm");
308  }
309 
310  private void linkLabel_SDK_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
311  {
312  System.Diagnostics.Process.Start("http://easefilter.com/Forums_Files/Transparent_Encryption_Filter_Driver.htm");
313  }
314 
315  private void button_Activate_Click(object sender, EventArgs e)
316  {
317  }
318 
319  private void Form_FileCrypt_FormClosed(object sender, FormClosedEventArgs e)
320  {
321  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
322  if (MessageBox.Show("Do you want to minimize to system tray?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
323  {
324 
325  }
326  else
327  {
328  GlobalConfig.Stop();
329  FilterAPI.StopFilter();
330 
331  Application.Exit();
332  }
333  }
334 
335  private void button_ChangePassword_Click(object sender, EventArgs e)
336  {
337  SetupPasswordForm setupForm = new SetupPasswordForm();
338  setupForm.ShowDialog();
339  }
340 
341 
342  private void button_SelectIncludePID_Click(object sender, EventArgs e)
343  {
344 
345  OptionForm optionForm = new OptionForm(OptionForm.OptionType.ProccessId, textBox_IncludePID.Text);
346 
347  if (optionForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
348  {
349  textBox_IncludePID.Text = optionForm.ProcessId;
350  }
351  }
352 
353  private void button_SelectExcludePID_Click(object sender, EventArgs e)
354  {
355 
356  OptionForm optionForm = new OptionForm(OptionForm.OptionType.ProccessId, textBox_ExcludePID.Text);
357 
358  if (optionForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
359  {
360  textBox_ExcludePID.Text = optionForm.ProcessId;
361  }
362  }
363 
364 
365  private void button_ApplySetting_Click(object sender, EventArgs e)
366  {
367  List<uint> inPids = new List<uint>();
368  if (textBox_IncludePID.Text.Length > 0)
369  {
370  if (textBox_IncludePID.Text.EndsWith(";"))
371  {
372  textBox_IncludePID.Text = textBox_IncludePID.Text.Remove(textBox_IncludePID.Text.Length - 1);
373  }
374 
375  string[] pids = textBox_IncludePID.Text.Split(new char[] { ';' });
376  for (int i = 0; i < pids.Length; i++)
377  {
378  inPids.Add(uint.Parse(pids[i].Trim()));
379  }
380  }
381 
382  GlobalConfig.IncludePidList = inPids;
383 
384  List<uint> exPids = new List<uint>();
385  if (textBox_ExcludePID.Text.Length > 0)
386  {
387  if (textBox_ExcludePID.Text.EndsWith(";"))
388  {
389  textBox_ExcludePID.Text = textBox_ExcludePID.Text.Remove(textBox_ExcludePID.Text.Length - 1);
390  }
391 
392  string[] pids = textBox_ExcludePID.Text.Split(new char[] { ';' });
393  for (int i = 0; i < pids.Length; i++)
394  {
395  exPids.Add(uint.Parse(pids[i].Trim()));
396  }
397  }
398 
399  GlobalConfig.ExcludePidList = exPids;
400 
401  GlobalConfig.IncludedUsers = textBox_IncludedUsers.Text;
402  GlobalConfig.ExcludedUsers = textBox_ExcludedUsers.Text;
403 
405 
406  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
407  MessageBox.Show("Your setting was saved.", "Apply settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
408 
409  }
410 
411 
412  }
413 }
static bool AddFilterRule(FilterRule newRule)
unsigned char key[]
static void RemoveFilterRule(string includeFilterMask)
uint EventType
The register the file I/O events
static bool IsFilterRuleExist(string includeFilterMask)
static Dictionary< string, FilterRule > FilterRules
static FilterAPI.FilterType filterType
Definition: GlobalConfig.cs:79
static byte [] GetKeyByPassPhrase(string pwStr)
Generate 32 bytes key array by pass phrase string
Definition: Utils.cs:207
static bool StartFilterService(out string lastError)

Social Network


Services Overview

Architect, implement and test file system filter drivers for a wide range of functionality. We can offer several levels of assistance to meet your specific.

Contact Us

You are welcome to contact us for salse or partnership.

Sales: sales@easefilter.com
Support: support@easefilter.com
Info: info@easefilter.com