-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
105 lines (82 loc) · 3.27 KB
/
Program.cs
File metadata and controls
105 lines (82 loc) · 3.27 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
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Secure File Vault (Console Version)");
while (true)
{
Console.Write("Enter 'e' to encrypt, 'd' to decrypt, or 'q' to quit: ");
string action = Console.ReadLine().ToLower();
if (action == "q") break;
if (action != "e" && action != "d")
{
Console.WriteLine("Invalid option.");
continue;
}
Console.Write("Enter full file path: ");
string inputPath = Console.ReadLine();
if (!File.Exists(inputPath))
{
Console.WriteLine("File does not exist.");
continue;
}
Console.Write("Enter password: ");
string password = ReadPassword();
try
{
byte[] fileBytes = File.ReadAllBytes(inputPath);
if (action == "e")
{
byte[] encrypted = CryptoHelper.Encrypt(fileBytes, password);
// Save encrypted file
string vaultDir = "vault";
Directory.CreateDirectory(vaultDir);
string outputFile = Path.Combine(vaultDir, Path.GetFileName(inputPath) + ".vault");
File.WriteAllBytes(outputFile, encrypted);
// Compute checksum of original file
string checksum = CryptoHelper.ComputeSHA256(fileBytes);
Console.WriteLine($"File encrypted successfully and saved to {outputFile}");
Console.WriteLine($"SHA256 checksum of original file: {checksum}");
}
else if (action == "d")
{
byte[] decrypted = CryptoHelper.Decrypt(fileBytes, password);
string outputDir = "output";
Directory.CreateDirectory(outputDir);
string originalFileName = Path.GetFileNameWithoutExtension(inputPath); // remove .vault
string outputFile = Path.Combine(outputDir, originalFileName);
File.WriteAllBytes(outputFile, decrypted);
// Compute checksum of decrypted file
string checksum = CryptoHelper.ComputeSHA256(decrypted);
Console.WriteLine($"File decrypted successfully and saved to {outputFile}");
Console.WriteLine($"SHA256 checksum of decrypted file: {checksum}");
}
}
catch (Exception ex)
{
Console.WriteLine("Error during processing: " + ex.Message);
}
Console.WriteLine();
}
Console.WriteLine("Exiting Secure File Vault.");
}
// Read password without echoing input
private static string ReadPassword()
{
string password = "";
ConsoleKeyInfo info;
do
{
info = Console.ReadKey(true);
if (info.Key != ConsoleKey.Enter)
{
password += info.KeyChar;
Console.Write("*");
}
} while (info.Key != ConsoleKey.Enter);
Console.WriteLine();
return password;
}
}