-
Notifications
You must be signed in to change notification settings - Fork 0
/
Internals.cs
299 lines (261 loc) · 10.8 KB
/
Internals.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using CsvHelper;
using System.Globalization;
namespace The_vault
{
public static class Internals
{
private static string pepper= Properties.Settings.Default.Pepper;//the pepper i should change this every time but ehh
public static string key = Properties.Settings.Default.Pepper;//KEY, change every update
public static string iv = Properties.Settings.Default.Pepper;//THE IV
public static string directory = AppDomain.CurrentDomain.BaseDirectory + @"Vault";//where data will be stored
public static string file = AppDomain.CurrentDomain.BaseDirectory + @"Vault\login" + @"\logindata.data";//file locatuin
public static bool start(string username, string password)
{
var prop = Properties.Settings.Default;
Random rnd = new Random();
string s = generategoodrandom(rnd.Next(100));
string u = username;//grab the username
string p =hash(password, s); //grab the password
prop.Reset();
prop.Key = "";
prop.InitializationVector = "";
prop.Pepper = "";
pepper = prop.Pepper;
key = prop.Key;
prop.Key = generategoodrandom(32);
prop.InitializationVector = generategoodrandom(16);
prop.Pepper = generategoodrandom(rnd.Next(10000));
pepper = prop.Pepper;
key = prop.Key;
iv = prop.InitializationVector;
prop.Save();
if (File.Exists(file) == true)
{
return false;
}
else//if the file doesnt exist than created and everything
{
Directory.CreateDirectory(directory+@"\login");//create it
try
{
byte[] converted = Encoding.ASCII.GetBytes($"{u}:{p}:{s}");//convert the user/pwd(string) into bytes so that it can be encrypted
byte[] enc = encryptdata(converted, key, iv);
Savetofile(file, enc);//save the encrypted text to a file.
}
catch (Exception )
{
return false;
}
return true;
}
}
public static void initialize()
{
var abc = Properties.Settings.Default;
pepper = abc.Pepper;
key = abc.Key;
iv = abc.InitializationVector;
abc.Save();
}
public static string generategoodrandom(int length)
{
SHA512CryptoServiceProvider c = new SHA512CryptoServiceProvider();
var rnd = new Random();
string abcc = "";
for (int i = 0; i < rnd.Next(80, 10999); i++)
{
byte[] cool = Encoding.ASCII.GetBytes(rnd.Next(0, int.MaxValue).ToString());
System.Threading.Thread.Sleep(5);
abcc += Convert.ToBase64String(c.ComputeHash(cool));
}
return abcc.Replace("=", "").Substring(0, length);
}//im unsure why the program isnt starting
public static string hash(string inp,string salt)
{
SHA512 s = SHA512.Create();//creatae new sha512
byte[] hashit = Encoding.UTF8.GetBytes(inp + salt + pepper) ;//convert to bytes and add salt+pepper
string hashed = null;
for (int i = 0; i < 2; i++)
{
hashed += Convert.ToBase64String(s.ComputeHash(hashit));//HASHHHH
}
return hashed;
}
public static bool validateuserandpass(string inp)
{
if (inp.Length >= 4)//make sure the input is greator than 4
{
return true;
}
else
{
return false;
}
}
public static bool validatewebsite(string inp)
{
if (inp.Length > 9)
{
if (inp.ToLower().Contains("http://") || inp.Contains("https://"))//make sure the site is http or https if not than its not even a site lol
{
if (inp.Contains("."))//this is for domains for example (.)com or (.)tech see
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
public static bool checklogin()
{
try
{
if(File.Exists(file) == true)//check if the file where the login exists
{
return true;
}
else
{
return false;
}
}
catch(Exception ){
return false;
}
}
public static string grabusername()
{
// byte[] converted = Encoding.ASCII.GetBytes(inp);
byte[] p = File.ReadAllBytes(Internals.file);
string username = Encoding.ASCII.GetString(decryptdata(p,key,iv));//cibvert the byte to a string
string[] user = username.Split(':');//split the decrypted string into two parts
return user[0];
}
public static string grabpassword()
{
// byte[] converted = Encoding.ASCII.GetBytes(inp);
byte[] p = File.ReadAllBytes(Internals.file);
string username = Encoding.ASCII.GetString(decryptdata(p, key, iv));//cibvert the byte to a string
string[] user = username.Split(':');//split the decrypted string into two parts
return user[1];
}
public static string grabsalt()
{
// byte[] converted = Encoding.ASCII.GetBytes(inp);
byte[] p = File.ReadAllBytes(Internals.file);
string username = Encoding.ASCII.GetString(decryptdata(p, key, iv));//cibvert the byte to a string
string[] user = username.Split(':');//split the decrypted string into two parts
return user[2];
}
public static bool validate(string inp)//simple input validation just to make sure the user and or pass is sorta secure
{
if (inp !="")//check if box is equal to nothing
{
if (inp.Length >= 5)//make sure its greator tahn 5
{
return true;//its good
}
else
{
return false;//its bad
}
}
else
{
return false;//its bad
}
}
public static void writeerro(string input)
{
if (!Directory.Exists(directory + @"\errors"))
{
Directory.CreateDirectory(directory + @"\errors");
}
File.AppendAllText( directory+@"\errors\errors.data", $"{DateTime.Now.ToString("hh:mm:ssss MM/dd/yyyy")} | {input}\n---------------\n");
}
private static void Savetofile(string location, byte[] input)
{
try
{
/* using (var SW = new StreamWriter(location))
{
SW.WriteLine(input);
SW.Close();
}
*/
File.WriteAllBytes(location, input);
}
catch (Exception)
{
throw;
}
}
public static byte[] encryptdata(byte[] bytearraytoencrypt, string key, string iv)//make it byte just in case we need to encrypt a file :shrug:
{
try
{
using (var dataencrypt = new AesCryptoServiceProvider())
{ //Block size : Gets or sets the block size, in bits, of the cryptographic operation.
dataencrypt.BlockSize = 128;
//KeySize: Gets or sets the size, in bits, of the secret key
dataencrypt.KeySize = 128;
//Key: Gets or sets the symmetric key that is used for encryption and decryption.
dataencrypt.Key = System.Text.Encoding.UTF8.GetBytes(key);
//IV : Gets or sets the initialization vector (IV) for the symmetric algorithm
dataencrypt.IV = System.Text.Encoding.UTF8.GetBytes(iv);
//Padding: Gets or sets the padding mode used in the symmetric algorithm
dataencrypt.Padding = PaddingMode.PKCS7;
//Mode: Gets or sets the mode for operation of the symmetric algorithm
dataencrypt.Mode = CipherMode.CBC;
//Creates a symmetric AES encryptor object using the current key and initialization vector (IV).
ICryptoTransform crypto1 = dataencrypt.CreateEncryptor(dataencrypt.Key, dataencrypt.IV);
//TransformFinalBlock is a special function for transforming the last block or a partial block in the stream.
//It returns a new array that contains the remaining transformed bytes. A new array is returned, because the amount of
//information returned at the end might be larger than a single block when padding is added.
byte[] encrypteddata = crypto1.TransformFinalBlock(bytearraytoencrypt, 0, bytearraytoencrypt.Length);
crypto1.Dispose();
//return the encrypted data
return encrypteddata;
}
}
catch (Exception)
{
throw;
}
}
public static byte[] decryptdata(byte[] bytearraytodecrypt, string key, string iv)
{//do i even have to explain??
using (var keydecrypt = new AesCryptoServiceProvider())
{
keydecrypt.BlockSize = 128;
keydecrypt.KeySize = 128;
keydecrypt.Key = System.Text.Encoding.UTF8.GetBytes(key);
keydecrypt.IV = System.Text.Encoding.UTF8.GetBytes(iv);
keydecrypt.Padding = PaddingMode.PKCS7;
keydecrypt.Mode = CipherMode.CBC;
ICryptoTransform crypto1 = keydecrypt.CreateDecryptor(keydecrypt.Key, keydecrypt.IV);
byte[] returnbytearray = crypto1.TransformFinalBlock(bytearraytodecrypt, 0, bytearraytodecrypt.Length);
crypto1.Dispose();
return returnbytearray;
}
}
}
}