-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
498 lines (438 loc) · 18.8 KB
/
Form1.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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MiniJSON;
using System.Security.Cryptography;
using System.Runtime.CompilerServices;
using static dataManager;
using System.Diagnostics;
namespace Password_Manager
{
public partial class Form1 : Form
{
public Dictionary<int, object> dict = new Dictionary<int, object>();
public bool isPasswordVisible = false;
public char passwordchar;
public int localcrypt = 0;
public string ASCIIcrypt = "";
public int random = 0;
//public int[] secret = { 1, 2, 3, 4, 5, 6 };
public Int32 passint = 0;
public string pass = "";
public string passwd = "";
public string passwdASCII = "";
public int localrand = 0;
public int[] encrypted;
public Form1()
{
InitializeComponent();
}
private void cmbLogin_Click(object sender, EventArgs e)
{
SaveLogfile("Attempting Login");
if(txtLogin.Text != "")
{
cobEncryption.Visible = true;
passwd = txtLogin.Text;
//passint = ConvertToASCIIaddition(passwd);
passint = ConvertStringToInt(GetStringSha256Hash(passwd));
if (chkPIN.Checked)
{
pass = passint.ToString() + nudPIN.Value.ToString();
passint = Int32.Parse(pass);
}
lstPassword.Items.Clear();
loadAllPasswords();
lblA.Text = passint + "";
txtPass.Visible = true;
txtPasswordName.Visible = true;
cmbEncrypt.Visible = true;
cmbPassVisibility.Visible = true;
label1.Visible = true;
label2.Visible = true;
SaveLogfile("Login Succesful");
}
else
{
SaveLogfile("Login failed");
}
//lblRandom.Text = System.Convert.ToString(GetRandomNumber(SetRandomObject(ConvertToASCIIint(passwd))));
//GetRandomNumber(SetRandomObject(ConvertToASCIIstring(passwd)));
}
public int ConvertToASCIIint(string s)
{
SaveLogfile("Calling method 'ConvertToASCIIint'");
int localpassint = 0;
pass = "";
foreach(char c in s)
{
pass += Convert.ToInt32(c);
}
localpassint = Convert.ToInt32(pass);
return localpassint;
}
public int ConvertToASCIIaddition(string s)
{
SaveLogfile("Calling method 'ConvertToASCIIaddition'");
//pass = "";
int localpassint = 0;
foreach (char c in s)
{
localpassint += Convert.ToInt32(c);
}
return localpassint;
}
public Random SetRandomObject(int seed) // Initialisiert eine Zufallszahl mit dem Seed
{
//SaveLogfile("Calling method 'SetRandomObject'"); //Das müllt das Logfile zu!
Random randomobject = new Random(seed);
return randomobject;
}
public int GetRandomNumber(Random randobj, int number) //Holt die number-nte Zufallszahl mit dem Seed des randobj. Sollte mit SetRandomObject benutzt werden
{
//SaveLogfile("Calling method 'GetRandomNumber'"); //Das müllt das Logfile zu!
lblRandom.Text = "";
for(int j = 1; j < number; j++)
{
//lblRandom.Text += randobj.Next() + "\n";
localrand = randobj.Next();
}
lblRandom.Text = localrand.ToString();
return localrand;
}
public int[] encrypt(string plain)
{
SaveLogfile("Try encrypting plaintext (ASCII-SHUFFLER)");
int x = 0;
foreach (char c in plain)
{
x++;
}
encrypted = new int[x];
/*for (i = 0; i < 2; i++)
{*/
int i = 0;
int j = 2;
foreach (char c in plain)
{
//int k = 0;
localcrypt = Convert.ToInt32(c);
encrypted[i] = localcrypt + GetRandomNumber(SetRandomObject(passint), j);
//localcrypt += GetRandomNumber(SetRandomObject(passint), i);
//lblCrypt.Text += localcrypt + "\n";
//encrypted[i] = localcrypt;
//localcrypt = localcrypt
i++;
j++;
}
/*if (i == 0)
{
for (int j = 0; j < encrypted.Length; j++)
{
encrypted[j] = 0;
}
}*/
//}
SaveLogfile("Encryption succesfully (ASCII-SHUFFLER)");
return encrypted;
}
private void cmbEncrypt_Click(object sender, EventArgs e)
{
SaveLogfile("Starting encryption");
lblCrypt.Text = "";
if(cobEncryption.Text == "Use ASCII-Shuffler encryption")
{
SaveLogfile("Using ASCII-SHUFLLER");
int[] showcrypto;
showcrypto = encrypt(txtPass.Text);
foreach (int i in showcrypto)
{
lblCrypt.Text += /*Convert.ToString(i)*/i + "\n";
}
if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
"/ThePasswordManager/" + txtPasswordName.Text + ".gppass"))
{
MessageBox.Show("Please enter the password you first encrypted this file to overwrite it " +
"in the text box named ", "Overwrite password",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
}
dataManager.SavePasswordArray(txtPasswordName.Text, encrypt(txtPass.Text));
SaveLogfile("Saved encrypted password (" + txtPasswordName.Text + ")");
}
else if(cobEncryption.Text == "Use AES encryption")
{
SaveLogfile("Using AES");
string aescrypt = AES_Manager.Encrypt(txtPass.Text, GetStringSha256Hash(txtLogin.Text + nudPIN.Value));
if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
"/ThePasswordManager/" + txtPasswordName.Text + ".gppass"))
{
MessageBox.Show("Please enter the password you first encrypted this file to overwrite it " +
"in the text box named ", "Overwrite password",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
}
dataManager.SavePasswordString(txtPasswordName.Text, aescrypt, GetStringSha256Hash(txtLogin.Text + nudPIN.Value));
SaveLogfile("Saved encrypted password (" + txtPasswordName.Text + ")");
}
}
private void cmbClose_Click(object sender, EventArgs e)
{
SaveLogfile("Closing greenonionPass");
Close();
}
private void Form1_Load(object sender, EventArgs e)
{
SaveLogfile("Starting greenonionPass");
if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\AES\\")) //Nötige Ordner erstellen, falls nicht vorhanden
{
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\AES\\");
}
if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\ASCII_Shuffler\\"))
{
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\ASCII_Shuffler\\");
}
if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\Log\\"))
{
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\Log\\");
}
if(!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\Log\\greenonionPass.log")) //Logfile erstellen, falls nicht vorhanden
{
var logfile = File.Create(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\Log\\greenonionPass.log");
logfile.Close();
}
label6.Text = "View log at " + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\Logs\\greenonionPass.log";
passwordchar = txtPass.PasswordChar;
cobEncryption.Text = "Use ASCII-Shuffler encryption";
}
private void cmbPassVisibility_Click(object sender, EventArgs e)
{
if (!isPasswordVisible)
{
SaveLogfile("Enabled password visibility");
txtPass.Font = new Font("Microsoft Sans Serif", 10f);
txtPass.PasswordChar = (char)0;
isPasswordVisible = true;
cmbPassVisibility.Text = "Make password invisible";
}
else
{
SaveLogfile("Disabled password visibility");
txtPass.Font = new Font("Microsoft Sans Serif", 4f);
txtPass.PasswordChar = passwordchar;
isPasswordVisible = false;
cmbPassVisibility.Text = "Make password visible";
}
}
public void loadAllPasswords()
{
SaveLogfile("Attempting load all passwords");
//Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/ThePasswordManager/");
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/ThePasswordManager/ASCII_Shuffler/";
DirectoryInfo d = new DirectoryInfo(filepath);
int l = 0;
foreach(var file in d.GetFiles("*.gppass"))
{
l++;
}
if(l != 0)
{
lstPassword.Items.Add("ASCII-SHUFFLER ENCRYPTED PASSWORDS:");
lstPassword.Items.Add("");
}
foreach (var file in d.GetFiles("*.gppass")) // ASCII-Shuffler Passwörter entschlüsseln
{
string[] parts = file.Name.Split(new char[] { '.' });
lblA.Text = file.Name;
//File.SetAttributes(file.Name.ToString(), FileAttributes.Normal);
string decryptedPassword = "";
lstPassword.Items.Add("\nPassword name: " + parts[0]);
char[] decryptedPasswordChar = new char[DecryptEncryptedPasswordArray(dataManager.LoadEncryptedPasswordArray(file.Name)).Length];
decryptedPasswordChar = DecryptEncryptedPasswordArray(dataManager.LoadEncryptedPasswordArray(file.Name));
foreach (char c in decryptedPasswordChar)
{
decryptedPassword += c;
//lstPassword.Items.Add(c);
}
lstPassword.Items.Add(decryptedPassword);
lstPassword.Items.Add("");
/*lstPassword.Items.Add(DecryptAesCipherPassword(dataManager.LoadEncryptedPasswordString(file.Name), GetStringSha256Hash(txtLogin.Text)));
lstPassword.Items.Add("");*/
}
filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/ThePasswordManager/AES/";
d = new DirectoryInfo(filepath);
int abc = 0;
foreach (var file in d.GetFiles("*.gppass")) //Dateien die das Passwort matchen zählen
{
if (dataManager.LoadEncryptedPasswordString(file.Name)[1] == GetStringSha256Hash(txtLogin.Text + nudPIN.Value))
{
abc++;
}
}
if(abc != 0)
{
lstPassword.Items.Add("AES ENCRPYPTED PASSWORDS:");
lstPassword.Items.Add("");
}
int x = 0;
foreach (var file in d.GetFiles("*.gppass")) //AES Passwörter entschlüsseln
{
string[] parts = file.Name.Split(new char[] { '.' });
if (dataManager.LoadEncryptedPasswordString(file.Name)[1] == GetStringSha256Hash(txtLogin.Text + nudPIN.Value))
{
lstPassword.Items.Add("Password name: " + parts[0]);
lstPassword.Items.Add(DecryptAesCipherPassword(dataManager.LoadEncryptedPasswordString(file.Name)[0], GetStringSha256Hash(txtLogin.Text)));
lstPassword.Items.Add("");
}
else
{
x++;
}
}
if (x != 0)
{
lstPassword.Items.Add("NOT DECRYPTED:");
lstPassword.Items.Add("");
foreach (var file in d.GetFiles("*.gppass"))
{
if (dataManager.LoadEncryptedPasswordString(file.Name)[1] != GetStringSha256Hash(txtLogin.Text + nudPIN.Value))
{
lstPassword.Items.Add(file.Name);
}
}
}
SaveLogfile("Successfully loaded all passwords");
}
public char[] DecryptEncryptedPasswordArray(int[] passwordarray)
{
int decryptedPasswordCharASCII = 0;
int x = 2;
int j = 0;
char[] decryptedPasswordArray = new char[passwordarray.Length];
foreach(int i in passwordarray)
{
decryptedPasswordCharASCII = i - GetRandomNumber(SetRandomObject(passint), x);
decryptedPasswordArray[j] = (char)decryptedPasswordCharASCII;
x++;
j++;
}
return decryptedPasswordArray;
}
public string DecryptAesCipherPassword(string cipherPassword, string password)
{
string dec = AES_Manager.Decrypt(cipherPassword, password);
return dec;
}
private void chkPIN_CheckedChanged(object sender, EventArgs e)
{
if (!chkPIN.Checked)
{
nudPIN.Visible = false;
label4.Visible = false;
}
else
{
nudPIN.Visible = true;
label4.Visible = true;
}
}
private void cmbLock_Click(object sender, EventArgs e)
{
cobEncryption.Visible = false;
lstPassword.Items.Clear();
txtPass.Text = "";
nudPIN.Value = 0;
txtLogin.Text = "";
txtPasswordName.Text = "";
lblA.Text = "";
lblCrypt.Text = "";
lblRandom.Text = "";
label1.Visible = false;
label2.Visible = false;
txtPasswordName.Visible = false;
txtPass.Visible = false;
cmbEncrypt.Visible = false;
cmbPassVisibility.Visible = false;
}
//public static byte[] GetHash(string inputString)
//{
// using (HashAlgorithm algorithm = SHA256.Create())
// return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString));
//}
//public static string GetHashString(string inputString)
//{
// StringBuilder sb = new StringBuilder();
// foreach (byte b in GetHash(inputString))
// sb.Append(b.ToString("X2"));
// return sb.ToString();
//}
public static string GetStringSha256Hash(string text)
{
if (String.IsNullOrEmpty(text))
return String.Empty;
using (var sha = new System.Security.Cryptography.SHA256Managed())
{
byte[] textData = System.Text.Encoding.UTF8.GetBytes(text);
byte[] hash = sha.ComputeHash(textData);
return BitConverter.ToString(hash).Replace("-", String.Empty);
}
}
public static byte[] GetByteSha256Hash(string text)
{
using (var sha = new System.Security.Cryptography.SHA256Managed())
{
byte[] textData = System.Text.Encoding.UTF8.GetBytes(text);
byte[] hash = sha.ComputeHash(textData);
return hash;
}
}
public static int ConvertStringToInt(string inputstring)
{
int result = 0;
foreach(char c in inputstring)
{
result += (int)c;
}
return result;
}
private void cmdDelete_Click(object sender, EventArgs e)
{
if (lstPassword.SelectedItems.Count != 0)
{
//MessageBox.Show(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\" + lstPassword.SelectedItem.ToString());
if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ThePasswordManager\\" + lstPassword.SelectedItem.ToString()))
{
lstPassword.Items.RemoveAt(lstPassword.SelectedIndex + 1);
lstPassword.Items.RemoveAt(lstPassword.SelectedIndex);
File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
"/ThePasswordManager/" + lstPassword.SelectedItem.ToString());
}
else
{
MessageBox.Show("File does not exist or no item selected" +
"or item selected is no file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}
}
/*else
{
MessageBox.Show("File does not exist or no item selected" +
"or item selected is no file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
}*/
}
public byte[] ComputeIV(string password)
{
using (SymmetricAlgorithm crypt = Aes.Create())
using (HashAlgorithm hash = MD5.Create())
{
crypt.Key = hash.ComputeHash(Encoding.UTF8.GetBytes(password));
crypt.GenerateIV();
return crypt.IV;
}
}
}
}