EaseFilter Demo Project
EncryptionForm.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.Collections.Generic;
13 using System.ComponentModel;
14 using System.Data;
15 using System.Drawing;
16 using System.Linq;
17 using System.Text;
18 using System.IO;
19 using System.Windows.Forms;
20 
22 
23 namespace FileProtector
24 {
25  public partial class EncryptedFileForm : Form
26  {
27  FilterAPI.EncryptType encryptType = FilterAPI.EncryptType.Encryption;
28 
29  public EncryptedFileForm(string formName, FilterAPI.EncryptType encryptType)
30  {
31  InitializeComponent();
32  this.Text = formName;
33  this.encryptType = encryptType;
34  }
35 
36  private void button_Start_Click(object sender, EventArgs e)
37  {
38 
39  string passPhrase = textBox_PassPhrase.Text.Trim();
40  string fileName = textBox_FileName.Text.Trim();
41  string lastError = string.Empty;
42  bool retVal = false;
43 
44  if (passPhrase.Length == 0)
45  {
46  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
47  MessageBox.Show("Password phrase can't be empty.", "Encryption", MessageBoxButtons.OK, MessageBoxIcon.Error);
48  return;
49  }
50 
51  if (fileName.Length == 0)
52  {
53  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
54  MessageBox.Show("File name can't be empty.", "Encryption", MessageBoxButtons.OK, MessageBoxIcon.Error);
55  return;
56  }
57 
58  byte[] key = Utils.GetKeyByPassPhrase(passPhrase);
59  byte[] iv = Utils.GetRandomIV();
60 
61  if (checkBox_UseDefaultIV.Checked)
62  {
63  iv = FilterAPI.DEFAULT_IV_TAG;
64  }
65 
66 
67  if (encryptType == FilterAPI.EncryptType.Encryption)
68  {
69  retVal = FilterAPI.AESEncryptFile(fileName, (uint)key.Length, key, (uint)iv.Length, iv, true);
70  }
71  else
72  {
73  if (!checkBox_UseDefaultIV.Checked)
74  {
75  retVal = FilterAPI.GetIVTag(fileName, ref iv, out lastError);
76 
77  if (!retVal)
78  {
79  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
80  MessageBox.Show(" Get file " + fileName + " IV tag failed with error:" + lastError, encryptType.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
81  return;
82  }
83  }
84 
85  retVal = FilterAPI.AESDecryptFile(fileName, (uint)key.Length, key, (uint)iv.Length, iv);
86 
87  }
88 
89 
90  if (!retVal)
91  {
92  string errorMessage = FilterAPI.GetLastErrorMessage();
93 
94  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
95  MessageBox.Show(encryptType.ToString() + " file " + fileName + " failed with error:" + errorMessage, encryptType.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
96 
97  return;
98  }
99  else
100  {
101  MessageBoxHelper.PrepToCenterMessageBoxOnForm(this);
102  MessageBox.Show(encryptType.ToString() + " file " + fileName + " succeeded.", encryptType.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
103  }
104 
105  }
106 
107  private void checkBox_DisplayPassword_CheckedChanged(object sender, EventArgs e)
108  {
109  if (checkBox_DisplayPassword.Checked)
110  {
111  textBox_PassPhrase.UseSystemPasswordChar = false;
112  }
113  else
114  {
115  textBox_PassPhrase.UseSystemPasswordChar = true;
116  }
117  }
118 
119  private void button_OpenFile_Click(object sender, EventArgs e)
120  {
121  OpenFileDialog openFileDialog = new OpenFileDialog();
122 
123  if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
124  {
125  textBox_FileName.Text = openFileDialog.FileName;
126  }
127  }
128  }
129 }
static byte [] GetRandomIV()
Definition: Utils.cs:229
unsigned char key[]
unsigned char iv[]
static byte [] GetKeyByPassPhrase(string pwStr)
Generate 32 bytes key array by pass phrase string
Definition: Utils.cs:207
EncryptedFileForm(string formName, FilterAPI.EncryptType encryptType)

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