-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
259 lines (213 loc) · 6.43 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
258
259
using System.Security.Cryptography;
using System.Text;
using LiteNetLib.Utils;
const string PasswordCheckValue = "hi";
Console.WriteLine("Hello, World!");
Console.WriteLine("Here is a cryptographically secure, 16-character, randomly generated password in case you need it:");
for (int i = 0; i < 16; i++)
{
char c = (char)RandomNumberGenerator.GetInt32(32, 127);
Console.Write(c);
}
Console.WriteLine();
string currentPath = Directory.GetCurrentDirectory();
string plaintextPath = $@"{currentPath}\plaintext";
string ciphertextPath = $@"{currentPath}\ciphertext";
string initializedPath = $@"{currentPath}\initialized";
Directory.CreateDirectory(ciphertextPath);
string? password = null;
ReenterPassword:
if (!File.Exists(initializedPath))
{
password = CreatePassword();
byte[] encryptedData = EncryptWithPassword(password, Encoding.UTF8.GetBytes(PasswordCheckValue));
File.WriteAllBytes(initializedPath, encryptedData);
}
if (password == null)
{
Console.Write("Enter your password: ");
password = Console.ReadLine()!;
}
// Ensure password is correct
{
byte[] encryptedData = File.ReadAllBytes(initializedPath);
try
{
byte[] data = DecryptWithPassword(password, encryptedData);
string passwordCheckValue = Encoding.UTF8.GetString(data);
if (passwordCheckValue != PasswordCheckValue)
{
throw new Exception();
}
}
catch
{
Console.WriteLine("Password is incorrect!");
password = null;
goto ReenterPassword;
}
}
Console.Clear();
Console.WriteLine($"Warning: Plaintext files cannot not be larger than {ushort.MaxValue} bytes!");
Console.WriteLine($"Warning: Plaintext files must be in a flat directory!");
long? versionToLoad = null;
{
var ciphertextFiles = Directory.GetFiles(ciphertextPath);
foreach (var ciphertextFile in ciphertextFiles)
{
var split = ciphertextFile.Split('\\');
long version = long.Parse(split[^1]);
if (versionToLoad == null)
{
versionToLoad = version;
}
else
{
if (version > versionToLoad)
{
versionToLoad = version;
}
}
}
}
if (Directory.Exists(plaintextPath))
{
bool shouldDelete;
while (true)
{
Console.WriteLine("The plaintext directory exists, are you okay with deleting it? (\"yes\" or \"no\")");
string yesOrNo = Console.ReadLine()!;
yesOrNo = yesOrNo.Trim();
if (yesOrNo == "yes")
{
shouldDelete = true;
break;
}
else if (yesOrNo == "no")
{
shouldDelete = false;
break;
}
}
if (shouldDelete)
{
Directory.Delete(plaintextPath, true);
}
else
{
goto Save;
}
}
Directory.CreateDirectory(plaintextPath);
if (versionToLoad != null)
{
byte[] encryptedData = File.ReadAllBytes($@"{ciphertextPath}\{versionToLoad}");
byte[] data = DecryptWithPassword(password, encryptedData);
NetDataReader reader = new(data);
int fileCount = reader.GetInt();
for (int i = 0; i < fileCount; i++)
{
string fileName = reader.GetString();
string fileText = reader.GetString();
File.WriteAllText($@"{plaintextPath}\{fileName}", fileText);
}
Console.WriteLine($"Loaded version {versionToLoad}.");
}
else
{
Console.WriteLine("No ciphertext was found, nothing was loaded into plaintext.");
}
Console.WriteLine("Enter to save and exit.");
Console.ReadLine();
Save:
NetDataWriter writer = new();
// Load plaintext data into memory
{
List<(string fileName, string fileText)> data = new();
var plaintextFiles = Directory.GetFiles(plaintextPath);
foreach (var plaintextFile in plaintextFiles)
{
var split = plaintextFile.Split('\\');
string fileName = split[^1];
string fileText = File.ReadAllText(plaintextFile);
data.Add((fileName, fileText));
}
{
int fileCount = data.Count;
writer.Put(fileCount);
foreach ((string fileName, string fileText) in data)
{
writer.Put(fileName);
writer.Put(fileText);
}
}
}
// Encrypt and save the data
{
long versionToSave = versionToLoad == null ? 0 : versionToLoad.Value + 1;
byte[] encryptedData = EncryptWithPassword(password, writer.CopyData());
File.WriteAllBytes($@"{ciphertextPath}/{versionToSave}", encryptedData);
}
Directory.Delete(plaintextPath, true);
static string CreatePassword()
{
string password;
while (true)
{
Console.Write("Create a password: ");
string a = Console.ReadLine()!;
Console.Write("Confirm your password: ");
string b = Console.ReadLine()!;
if (a == b)
{
password = a;
break;
}
}
return password;
}
static byte[] EncryptWithPassword(string password, byte[] data)
{
byte[] encryptedData;
using (Aes aes = Aes.Create())
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
byte[] aesKey = SHA256.HashData(passwordBytes);
byte[] aesIV = MD5.HashData(passwordBytes);
aes.Key = aesKey;
aes.IV = aesIV;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform encryptor = aes.CreateEncryptor(aesKey, aesIV);
using MemoryStream msEncrypt = new();
using CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write);
using (BinaryWriter bwEncrypt = new(csEncrypt))
{
bwEncrypt.Write(data);
}
encryptedData = msEncrypt.ToArray();
}
return encryptedData;
}
static byte[] DecryptWithPassword(string password, byte[] encryptedData)
{
byte[] data;
using Aes aes = Aes.Create();
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
byte[] aesKey = SHA256.HashData(passwordBytes);
byte[] aesIV = MD5.HashData(passwordBytes);
aes.Key = aesKey;
aes.IV = aesIV;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform decryptor = aes.CreateDecryptor(aesKey, aesIV);
using (MemoryStream msDecrypt = new(encryptedData))
{
using CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read);
using BinaryReader brDecrypt = new(csDecrypt);
using MemoryStream ms = new();
brDecrypt.BaseStream.CopyTo(ms);
data = ms.ToArray();
}
return data;
}