-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaster.cs
More file actions
164 lines (148 loc) · 5.23 KB
/
Master.cs
File metadata and controls
164 lines (148 loc) · 5.23 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace OffSyncPasswordManager
{
class Master
{
public static string[] KeyData
{
get
{
return new string[5] { Key, IV, KeySalt, AuthKeySalt, AuthKey };
}
set
{
Key = value[0];
IV = value[1];
KeySalt = value[2];
AuthKeySalt = value[3];
AuthKey = value[4];
}
}
public static string Key = "";
public static string IV = "";
public static string KeySalt = "";
public static string AuthKeySalt = "";
public static string AuthKey = "";
public static string PlainKey = "";
private static string keyFile = "encryptedKey.txt";
private static string pwordsFile = "encryptedPasswords.txt";
public static void InitializeMasterKeyData()
{
try
{
KeyData = RetrieveMasterKeyDataIfCorrect();
Key = KeyData[0];
}
catch (Exception e)
{
string[] encryptedData = AesEncryption.EncryptString(Key, GenerateMasterKeyKey());
KeyData = new string[5] { Key, encryptedData[1], encryptedData[2], encryptedData[3], encryptedData[4] };
File.WriteAllLines(keyFile, encryptedData);
}
}
public static bool KeyExists()
{
if (File.Exists(keyFile))
{
string[] storedData = File.ReadAllLines(keyFile);
string storedKey = storedData[0];
if (storedKey.Equals(""))
{
return false;
}
return true;
}
else
{
File.WriteAllLines(keyFile, KeyData);
File.WriteAllLines(pwordsFile, new string[0]);
return false;
}
}
public static string[] RetrieveMasterKeyDataIfCorrect()
{
string[] storedData = File.ReadAllLines(keyFile);
string storedKey = storedData[0];
string decryptedKey = AesEncryption.DecryptToString(Convert.FromBase64String(storedKey), GenerateMasterKeyKey(), storedData[1], storedData[2], storedData[3], storedData[4]);
if (decryptedKey.Equals(Key))
{
return new string[5] { decryptedKey, storedData[1], storedData[2], storedData[3], storedData[4] };
}
return new string[0];
}
public static bool KeyCorrect()
{
string[] storedData = File.ReadAllLines(keyFile);
string storedKey = storedData[0];
string storedIv = storedData[1];
string storedKeySalt = storedData[2];
string storedAuthKeySalt = storedData[3];
string storedAuthKey = storedData[4];
string decryptedKey = AesEncryption.DecryptToString(Convert.FromBase64String(storedKey), GenerateMasterKeyKey(), storedIv, storedKeySalt, storedAuthKeySalt, storedAuthKey);
return KeyMatches(decryptedKey);
}
public static bool KeyMatches(string toMatch)
{
if (toMatch != null)
{
if (toMatch.Equals(Key))
{
return true;
}
}
return false;
}
public static bool KeyDataNotEmpty()
{
foreach (string line in KeyData)
{
if (line.Equals(""))
{
return false;
}
}
return true;
}
public static string[] EncryptAndSaveMasterKey()
{
if (KeyDataNotEmpty())
{
string[] encryptedMasterKey = AesEncryption.EncryptString(Key, GenerateMasterKeyKey(), KeyData[1], KeyData[2], KeyData[3], KeyData[4]);
File.WriteAllLines(keyFile, encryptedMasterKey);
return encryptedMasterKey;
}
InitializeMasterKeyData();
return new string[5] { Key, KeyData[1], KeyData[2], KeyData[3], KeyData[4] };
}
public static void ChangeMasterKey(string newKey)
{
Key = newKey;
EncryptAndSaveMasterKey();
}
private static string DecryptMasterKey(string encryptedKey)
{
if (KeyCorrect() && KeyDataNotEmpty())
{
string decryptedMasterKey = AesEncryption.DecryptToString(Convert.FromBase64String(encryptedKey), GenerateMasterKeyKey(), IV, KeySalt, AuthKeySalt, AuthKey);
return decryptedMasterKey;
}
return "";
}
public static string GenerateMasterKeyKey()
{
int initLength = Key.Length;
string rmdr = Key.Substring(Key.Length - Key.Length % 5);
string MasterKeyKey = "";
for (int i = 4; i < initLength; i += 5)
{
MasterKeyKey += Key.Substring(i - 4, 5);
MasterKeyKey += "|";
}
MasterKeyKey += rmdr;
return MasterKeyKey;
}
}
}