-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass1.cs
161 lines (147 loc) · 5.5 KB
/
Class1.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
using System.Text;
using System.Security.Cryptography;
using Newtonsoft.Json;
using System.Data;
using System;
using System.Xml.Schema;
namespace rJson
{
public class rJs
{
public string getVar(DataTable parsedData, string variableName)
{
string i = "";
foreach (DataRow row in parsedData.Rows)
{
if (row["Variable"].ToString() == variableName)
{
i = row["Value"].ToString();
}
}
return i;
}
public static DataTable parse(string? rJson, string? filePath)
{
string fileOutput = "";
if (filePath != null)
{
fileOutput = File.ReadAllText(filePath);
}
else if (rJson != null)
{
fileOutput = rJson;
}
else
{
throw new ArgumentException("Both params cannot be null: Reading rJson, filePath");
}
fileOutput = Decrypt(fileOutput);
int ix = fileOutput.IndexOf("\t");
fileOutput = fileOutput.Substring(ix + 1);
fileOutput = fileOutput.Replace("\t", "").Replace(")", "").Replace("\n", "").Replace(" ", "");
char[] separator = { ',' };
string[] lines = fileOutput.Split(separator);
DataTable dt = new DataTable();
dt.Columns.Add("Variable", typeof(string));
dt.Columns.Add("Value", typeof(string));
var name = "";
var value = "";
foreach (var line in lines)
{
name = line.Substring(0, line.IndexOf("="));
value = line.Substring(line.LastIndexOf("=")).Replace("=", "");
dt.Rows.Add(name, value);
}
return dt;
}
public string New(string yourCode, string? filePath)
{
yourCode = yourCode.Replace("=", " = ");
yourCode = yourCode.Replace("(", "(\n\t");
yourCode = yourCode.Replace(")", "\n\t)");
yourCode = yourCode.Replace(",", ",\n\t");
yourCode = yourCode.Insert(0, "(\n\t");
yourCode = yourCode.Insert(yourCode.Length, "\n)");
yourCode = Encrypt(yourCode);
if (filePath != null)
{
File.WriteAllText(filePath, yourCode);
}
return yourCode;
}
private static Random random = new Random();
public static string idKey
{
get
{
if (!File.Exists("rJs.json"))
{
using (StreamWriter writer = new StreamWriter("rJs.json"))
{
writer.WriteLine("{");
writer.WriteLine("\tid: \"" + RandomString() + "\"");
writer.WriteLine("}");
}
}
string idJson = File.ReadAllText("rJs.json");
keyGetter o = JsonConvert.DeserializeObject<keyGetter>(idJson);
return o.id;
}
}
private class keyGetter
{
public string id { get; set;}
}
public static string RandomString()
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.!@#$%^&*";
return new string(Enumerable.Repeat(chars, 32)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
private static string Encrypt(string clearText)
{
var rand = new Random();
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
byte[] IV = new byte[15];
rand.NextBytes(IV);
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(idKey, IV);
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(IV) + Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
private static string Decrypt(string cipherText)
{
byte[] IV = Convert.FromBase64String(cipherText.Substring(0, 20));
cipherText = cipherText.Substring(20).Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(idKey, IV);
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
}
}