-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathProgram.cs
257 lines (229 loc) · 10.9 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
253
254
255
256
257
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
namespace CypherRSA;
// CypherRSA For Text Files MSasanMH
internal class Program
{
[STAThread]
static async Task Main()
{
try
{
// Commands
// Generate
// Encrypt FileToEncrypt PublicKey
// Decrypt FileToDecrypt PrivateKey
// Title
string title = $"Cypher Tool v{Assembly.GetExecutingAssembly().GetName().Version}";
if (OperatingSystem.IsWindows()) Console.Title = title;
// Invariant Culture
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
string generate = "generate", encrypt = "encrypt", decrypt = "decrypt";
bool isGenerate = false, isEncrypt = false, isDecrypt = false;
string fileToEncrypt = string.Empty, publicKeyPath = string.Empty;
string fileToDecrypt = string.Empty, privateKeyPath = string.Empty;
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 0)
{
if (args.Length == 2)
{
string arg = args[1].Trim();
if (arg.Equals(generate, StringComparison.OrdinalIgnoreCase)) isGenerate = true;
}
if (!isGenerate)
{
if (args.Length == 4)
{
string arg1 = args[1].Trim();
string arg2 = args[2].Trim();
string arg3 = args[3].Trim();
if (arg1.Equals(encrypt, StringComparison.OrdinalIgnoreCase))
{
isEncrypt = true;
fileToEncrypt = Path.GetFullPath(arg2);
publicKeyPath = Path.GetFullPath(arg3);
}
else if (arg1.Equals(decrypt, StringComparison.OrdinalIgnoreCase))
{
isDecrypt = true;
fileToDecrypt = Path.GetFullPath(arg2);
privateKeyPath = Path.GetFullPath(arg3);
}
}
}
}
if (isGenerate) // Generate
{
CypherRSA.GenerateKeys(out byte[] publicKey, out byte[] privateKey);
string? pubKeyPath = GetPath("PublicKey.bin");
if (!string.IsNullOrEmpty(pubKeyPath))
{
await File.WriteAllBytesAsync(pubKeyPath, publicKey);
Console.WriteLine($"Public Key Generated:{Environment.NewLine}{pubKeyPath}");
}
string? privKeyPath = GetPath("PrivateKey.bin");
if (!string.IsNullOrEmpty(privKeyPath))
{
await File.WriteAllBytesAsync(privKeyPath, privateKey);
Console.WriteLine($"Private Key Generated:{Environment.NewLine}{privKeyPath}");
}
}
else if (isEncrypt) // Encrypt
{
if (!File.Exists(fileToEncrypt))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"File Not Exist:{Environment.NewLine}{fileToEncrypt}");
Console.ResetColor();
Environment.Exit(0);
}
if (!File.Exists(publicKeyPath))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Public Key File Not Exist:{Environment.NewLine}{publicKeyPath}");
Console.ResetColor();
Environment.Exit(0);
}
string contentToEncrypt = await File.ReadAllTextAsync(fileToEncrypt);
List<string> linesToEncrypt = contentToEncrypt.ReplaceLineEndings().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList();
if (linesToEncrypt.Count == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"File Is Empty:{Environment.NewLine}{fileToEncrypt}");
Console.ResetColor();
Environment.Exit(0);
}
byte[] publicKeyBytes = await File.ReadAllBytesAsync(publicKeyPath);
if (publicKeyBytes.Length == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Public Key File Is Empty:{Environment.NewLine}{publicKeyPath}");
Console.ResetColor();
Environment.Exit(0);
}
List<string> encryptedLines = new();
for (int n = 0; n < linesToEncrypt.Count; n++)
{
string lineToEncrypt = linesToEncrypt[n].Trim();
if (string.IsNullOrEmpty(lineToEncrypt)) continue;
bool isEncryptionSuccess = CypherRSA.TryEncrypt(lineToEncrypt, publicKeyBytes, out _, out string encryptedLine);
if (isEncryptionSuccess)
{
encryptedLines.Add(encryptedLine);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Failed To Encrypt Line:{Environment.NewLine}{lineToEncrypt}{Environment.NewLine}");
Console.WriteLine($"Line Length: {lineToEncrypt.Length}{Environment.NewLine}");
Console.ResetColor();
}
}
string fileNameOut = Path.GetFileNameWithoutExtension(fileToEncrypt);
string fileExtOut = Path.GetExtension(fileToEncrypt);
string? fileOut = GetPath($"{fileNameOut}_Encrypted{fileExtOut}");
if (!string.IsNullOrEmpty(fileOut) && encryptedLines.Count > 0)
{
string encryptedContent = string.Join(Environment.NewLine, encryptedLines);
await File.WriteAllTextAsync(fileOut, encryptedContent);
Console.WriteLine("Encryption Success.");
Environment.Exit(0);
}
}
else if (isDecrypt) // Decrypt
{
if (!File.Exists(fileToDecrypt))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"File Not Exist:{Environment.NewLine}{fileToDecrypt}");
Console.ResetColor();
Environment.Exit(0);
}
if (!File.Exists(privateKeyPath))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Private Key File Not Exist:{Environment.NewLine}{privateKeyPath}");
Console.ResetColor();
Environment.Exit(0);
}
string contentToDecrypt = await File.ReadAllTextAsync(fileToDecrypt);
List<string> linesToDecrypt = contentToDecrypt.ReplaceLineEndings().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList();
if (linesToDecrypt.Count == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"File Is Empty:{Environment.NewLine}{fileToDecrypt}");
Console.ResetColor();
Environment.Exit(0);
}
byte[] privateKeyBytes = await File.ReadAllBytesAsync(privateKeyPath);
if (privateKeyBytes.Length == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Private Key File Is Empty:{Environment.NewLine}{privateKeyPath}");
Console.ResetColor();
Environment.Exit(0);
}
List<string> decryptedLines = new();
for (int n = 0; n < linesToDecrypt.Count; n++)
{
string lineToDecrypt = linesToDecrypt[n].Trim();
if (string.IsNullOrEmpty(lineToDecrypt)) continue;
bool isDecryptionSuccess = CypherRSA.TryDecrypt(lineToDecrypt, privateKeyBytes, out string decryptedLine);
if (isDecryptionSuccess)
{
decryptedLines.Add(decryptedLine);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Failed To Decrypt Line:{Environment.NewLine}{lineToDecrypt}{Environment.NewLine}");
Console.WriteLine($"Line Length: {lineToDecrypt.Length}{Environment.NewLine}");
Console.ResetColor();
}
}
string fileNameOut = Path.GetFileNameWithoutExtension(fileToDecrypt);
string fileExtOut = Path.GetExtension(fileToDecrypt);
string? fileOut = GetPath($"{fileNameOut}_Decrypted{fileExtOut}");
if (!string.IsNullOrEmpty(fileOut) && decryptedLines.Count > 0)
{
string decryptedContent = string.Join(Environment.NewLine, decryptedLines);
await File.WriteAllTextAsync(fileOut, decryptedContent);
Console.WriteLine("Decryption Success.");
Environment.Exit(0);
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wrong Command.");
Console.ResetColor();
Environment.Exit(0);
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.Message);
Console.ResetColor();
Environment.Exit(0);
}
}
public static string? GetPath(string fileNameWithExt)
{
try
{
using Process currentProcess = Process.GetCurrentProcess();
string? dir = Path.GetDirectoryName(Path.GetFullPath(currentProcess.ProcessName));
Debug.WriteLine("====== " + dir);
return !string.IsNullOrEmpty(dir) ? Path.GetFullPath(Path.Combine(dir, fileNameWithExt)) : null;
}
catch (Exception ex)
{
Debug.WriteLine("GetPath: " + ex.Message);
return null;
}
}
}