-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
252 lines (191 loc) · 9.93 KB
/
Program.cs
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
using System;
using System.IO;
using System.Linq;
namespace Cipher {
class Program {
private static string encryptionIndicator = "_encrypted";
private static int menuOptionCount = Message.MenuOptions().Count;
static void Main(string[] args) {
// Uncomment 'SaveArraySampleToDisk' method to generate your own salt array.
// Cipher.SaveArraySampleToDisk();
// Comment the Start method before generating your own salt array.
Start();
}
private static Boolean IsNumeric(string input) {
int value;
bool isNumeric = false;
if (int.TryParse(input, out value)) {
isNumeric = true;
}
return isNumeric;
}
private static void Start() {
// Display main menu.
Message.MainMenu();
// Get user selection.
int userSelection = 0;
do {
Console.Write(" Please make a selection: ");
// Get user selection and run menu.
userSelection = Convert.ToInt32(Console.ReadLine());
switch (userSelection) {
case 1:
EncryptDocument();
Message.MainMenu(); ;
break;
case 2:
DecryptDocumemt();
Message.MainMenu(); ;
break;
case 3:
EncryptDirectory();
Message.MainMenu(); ;
break;
case 4:
DecryptDirectory();
Message.MainMenu(); ;
break;
}
} while (userSelection != 0 && userSelection <= menuOptionCount);
}
private static void EncryptDirectory() {
// Display messages.
Message.SubMenu(" Directory Encryption");
Message.Action(" Enter the directory full path: ");
// Get the directory information.
string directory = Console.ReadLine();
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
// Get all the files in the directory that do not contain the encryption indicator in their name.
var files = directoryInfo.GetFiles().Where(x => !x.Name.Contains(encryptionIndicator));
// Set password.
Message.DisplayPasswordChoice();
string diplayPassword = Console.ReadLine();
Message.EnterPassword(action: "encryption");
var password = Utility.GetPassword(diplayPassword);
// Encrypting file.
foreach (FileInfo file in files) {
var plainTextFileName = directory + "\\" + file.Name;
var encryptedFileName = Path.GetFileNameWithoutExtension(file.Name) + encryptionIndicator + Path.GetExtension(file.Name);
EncryptFile(plainTextFileName, encryptedFileName, password);
}
Message.PressKeyToContinue(" Directory files encrypted.");
}
private static void DecryptDirectory() {
// Display messages.
Message.SubMenu(" Directory Decryption");
Message.Action(" Enter the directory full path: ");
// Get the directory information.
string directory = Console.ReadLine();
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
// Get all the files in the directory that do not contain the encryption indicator in their name.
var files = directoryInfo.GetFiles().Where(x => x.Name.Contains(encryptionIndicator));
// Set password.
Message.DisplayPasswordChoice();
string diplayPassword = Console.ReadLine();
Message.EnterPassword(action: "decryption");
var password = Utility.GetPassword(diplayPassword);
// Decrypt file.
foreach (FileInfo file in files) {
var encryptedFile = directory + "\\" + file.Name;
var plainTextFile = directory + "\\" + Path.GetFileName(file.Name).Replace(encryptionIndicator, "");
DecryptFile(encryptedFile, plainTextFile, password);
}
Message.PressKeyToContinue(" Directory files decrypted.");
}
private static void EncryptDocument() {
// Display message.
Message.SubMenu(" File Encryption");
Message.Action(" Enter the document name with its extension: ");
// Get plaintext file and read it into a byte array.
string plaintextFileName = Console.ReadLine();
string plaintextFile = Path.Combine(Utility.directory, plaintextFileName);
byte[] plaintextByteArray = File.ReadAllBytes(plaintextFile);
// Check if source file should be deleted after encryption.
string deleteFile = DeleteFileChoice(fileName: plaintextFileName, action: "encryption");
Console.WriteLine("");
// Display password choice.
Message.DisplayPasswordChoice();
string diplayPassword = Console.ReadLine();
Message.EnterPassword(action: "encrypting");
// Get password.
var password = Utility.GetPassword(diplayPassword);
// Encrypt file and store it into a byte array.
Message.ProcessingCipher(" Encrypting document");
byte[] encryptedBytes = Cipher.Encrypt(plaintextByteArray, password);
// Save encrypted document to disk.
var encryptedFileName = Path.GetFileNameWithoutExtension(plaintextFileName) + encryptionIndicator + Path.GetExtension(plaintextFileName);
var encryptedFile = Path.Combine(Utility.directory, encryptedFileName);
Utility.SaveFileToDisk(fileName: encryptedFile, fileContents: encryptedBytes);
// Check if source file needs to be deleted.
if (deleteFile == "y") {
Utility.DeleteFile(plaintextFile);
}
Message.PressKeyToContinue(" File encrypted.");
}
private static void DecryptDocumemt() {
// Display message.
Message.SubMenu(" File Decryption");
Message.Action(" Enter the document name with its extension: ");
// Get encrypted file.
string encryptedFileName = Console.ReadLine();
string encryptedFile = Path.Combine(Utility.directory, encryptedFileName);
var decryptedFileName = encryptedFileName.Replace(encryptionIndicator, "");
var decryptedFile = Path.Combine(Utility.directory, decryptedFileName);
// If file exists, cancel decryption.
if (File.Exists(decryptedFile)) {
Message.FileAlreadyExists();
Message.MainMenu();
return;
}
// Read encrypted file array.
byte[] bytesToBeDecrypted = File.ReadAllBytes(encryptedFile);
// Check if source file should be deleted after decryption.
string deleteFile = DeleteFileChoice(fileName: encryptedFileName, action: "decryption");
Console.WriteLine("");
// Display password choice.
Message.DisplayPasswordChoice();
string diplayPassword = Console.ReadLine();
Message.EnterPassword(action: "decrypting");
// Get password.
var password = Utility.GetPassword(diplayPassword);
// Decrypt file and store it into a byte array.
Message.ProcessingCipher(" Decrypting document");
byte[] decryptedBytes = Cipher.Decrypt(bytesToBeDecrypted, password);
// Save decrypted document to disk.
Utility.SaveFileToDisk(fileName: decryptedFile, fileContents: decryptedBytes);
// Check if source file needs to be deleted.
if (deleteFile == "y") {
Utility.DeleteFile(encryptedFile);
}
Message.PressKeyToContinue(" File decrypted.");
}
private static void EncryptFile(string plainTextFile, string encryptedFileName, string password) {
// Read encrypted file array and rncrypt it.
byte[] plaintextByteArray = File.ReadAllBytes(plainTextFile);
byte[] encryptedBytes = Cipher.Encrypt(plaintextByteArray, password);
var encryptedFile = Path.Combine(Utility.directory, encryptedFileName);
// Save data to disk.
Utility.SaveFileToDisk(fileName: encryptedFile, fileContents: encryptedBytes);
}
private static void DecryptFile(string encryptedFile, string plainTextFile, string password) {
// Read encrypted file array and decrypt it.
byte[] encryptedBytes = File.ReadAllBytes(encryptedFile);
byte[] decryptedBytes = Cipher.Decrypt(encryptedBytes, password);
var decryptedFileName = encryptedFile.Replace(encryptionIndicator, "");
var decryptedFile = Path.Combine(Utility.directory, decryptedFileName);
// Save data to disk.
Utility.SaveFileToDisk(fileName: decryptedFile, fileContents: decryptedBytes);
}
private static string DeleteFileChoice(string fileName, string action) {
Console.Write(" Delete '{0}' after {1} (y/n)? ", fileName, action);
string deleteFile = Console.ReadLine();
// Only a 'yes' response will delete the file.
if (deleteFile.ToLower() == "y") {
Console.Write(" File will be deleted!");
} else {
Console.Write(" File will not be deleted.");
}
return deleteFile;
}
}
}