-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.cs
82 lines (70 loc) · 2.46 KB
/
FileManager.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
using System;
using System.IO;
using System.Text;
namespace FilesEnc;
public static class MyCipher
{
private const int ShiftValue = 96; // Example shift value, adjust as needed
public static void EncryptFilesInDirectory(string directoryPath, string fileExtension = "*.*")
{
if (!Directory.Exists(directoryPath))
{
Console.WriteLine("Directory does not exist.");
return;
}
foreach (string file in Directory.GetFiles(directoryPath, $"*.{fileExtension}", SearchOption.AllDirectories))
{
try
{
byte[] contentBytes = File.ReadAllBytes(file);
byte[] encryptedContent = EncryptByteArray(contentBytes);
File.WriteAllBytes(file, encryptedContent);
Console.WriteLine($"File '{file}' encrypted successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to encrypt file '{file}'. Error: {ex.Message}");
}
}
}
public static void DecryptFilesInDirectory(string directoryPath, string fileExtension = "*.*")
{
if (!Directory.Exists(directoryPath))
{
Console.WriteLine("Directory does not exist.");
return;
}
foreach (string file in Directory.GetFiles(directoryPath, $"*.{fileExtension}", SearchOption.AllDirectories))
{
try
{
byte[] encryptedBytes = File.ReadAllBytes(file);
byte[] decryptedBytes = DecryptByteArray(encryptedBytes);
File.WriteAllBytes(file, decryptedBytes);
Console.WriteLine($"File '{file}' decrypted successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to decrypt file '{file}'. Error: {ex.Message}");
}
}
}
private static byte[] EncryptByteArray(byte[] data)
{
byte[] encryptedData = new byte[data.Length];
for (int i = 0; i < data.Length; i++)
{
encryptedData[i] = (byte)(data[i] + ShiftValue);
}
return encryptedData;
}
private static byte[] DecryptByteArray(byte[] encryptedData)
{
byte[] decryptedData = new byte[encryptedData.Length];
for (int i = 0; i < encryptedData.Length; i++)
{
decryptedData[i] = (byte)(encryptedData[i] - ShiftValue);
}
return decryptedData;
}
}