-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.cs
More file actions
100 lines (81 loc) · 3.03 KB
/
FileHandler.cs
File metadata and controls
100 lines (81 loc) · 3.03 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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace WMITest
{
class FileHandler
{
// Global variable(s)
Random random = new Random();
private string keyGlob;
/// Constructor
public FileHandler() { }
/// Mutators and Accesors
public string getKey() { return keyGlob; }
/// Chooses wheter to read from file for a key, or to generate a new key and file
public void init()
{
if (fileExists("key.xml"))
{
keyGlob = readKeyFromFile();
}
else
{
keyGlob = generateKey();
createKeyFile(keyGlob);
}
Console.WriteLine("Your key: " + keyGlob);
}
/// Searches for a file in the local directory
private bool fileExists(string file)
{
string[] data;
Console.WriteLine(Directory.GetCurrentDirectory().ToString());
data = Directory.GetFiles(Directory.GetCurrentDirectory().ToString(), file);
/*// displays found file(s)
foreach (string fileFound in data){Console.WriteLine("Found {0}", fileFound);}*/
// if file exists data's length will be 1 thus MORETHAN 0 so this will return true
return !(data.Length == 0);
}
/// Writes to a file
private void createKeyFile(string key)
{
string[] data = new string[6];
data[0] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
data[1] = "<SystemInfo>";
data[2] = " <Details>";
data[3] = " <Key>" + key + "</Key>";
data[4] = " </Details>";
data[5] = "</SystemInfo>";
try
{
System.IO.StreamWriter file = new System.IO.StreamWriter("key.xml");
foreach (string line in data) { file.WriteLine(line); }
file.Flush(); file.Close();
}
catch (Exception err) { Console.WriteLine("[-]\tException generating KEY: "
+ err.Message + "[-]"); }
}
/// Get key from xml file
private string readKeyFromFile()
{
TextReader tr = new StreamReader("key.xml");
string keyLine = "";
try
{
for (int i = 0; i < 3; i++) /*Console.WriteLine(*/tr.ReadLine()/*)*/;
keyLine = tr.ReadLine();
char[] splitBy = new char[2];
splitBy[0] = '>';
splitBy[1] = '<';
string[] key = keyLine.Split(splitBy);
return key[2].Trim();
}
catch (Exception err) { Console.WriteLine("[-]\tException reading KEY file: "
+ err.Message + "[-]"); return null; }
}
/// Generates a key for a user based on their CPU ticks and a random number
private string generateKey(){ return DateTime.Now.Ticks.ToString() + random.Next(1000).ToString(); }
}
}