· 7 years ago · Jan 11, 2019, 10:22 PM
1class UrlParser
2 {
3 private const string secretKey = "TT18WlV5TXVeLXFXYn1WTF5qSmR9TXYpOHklYlFXWGY+SUZCRGNKPiU0emcyQ2l8dGVsamBkVlpA";
4 private string registeredSecretCharPool = String.Empty;
5
6 public UrlParser()
7 {
8 // Register secret char pool
9 registeredSecretCharPool = RegisterCharPool(secretKey);
10 }
11
12 private string RegisterCharPool(string key)
13 {
14 Dictionary<char, int> k = new Dictionary<char, int>();
15 int a = 0, l = 0, d = 0, c = 0, h = key.Length, b;
16 string charPool = "";
17
18 for (a = 0; 64 > a; a++)
19 {
20 k.Add("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[a], a);
21 }
22
23 for (d = 0; d < h; d++)
24 {
25 char index = key[d];
26 for (a = (k.ContainsKey(index) ? k[index] : 0), l =(l<<6)+a, c+=6; 8 <=c;)
27 {
28 b = (int)((uint)l >> (c -= 8) & 255);
29 bool bIsGreaterThan0 = Convert.ToBoolean(b);
30
31 if (bIsGreaterThan0 || d < h - 2)
32 charPool += (char)(b);
33 }
34 }
35
36 return charPool;
37 }
38
39 public string ProcessRecording(string urlEncoded)
40 {
41 if (2 > urlEncoded.Length || 0 != urlEncoded.IndexOf("e:"))
42 return urlEncoded;
43
44 string publicCharPool = RegisterCharPool(urlEncoded.Substring(2));
45 Dictionary<int, int> a = new Dictionary<int, int>();
46 int h = 0;
47 int d;
48 string urlDecoded = "";
49 int b;
50
51 for (b = 0; 256 > b; b++)
52 a.Add(b, b);
53
54 for (b = 0; 256 > b; b++)
55 {
56 h = (h + a[b] + registeredSecretCharPool.ToCharArray()[(b % registeredSecretCharPool.Length)]) % 256;
57 d = a[b];
58 a[b] = a[h];
59 a[h] = d;
60 }
61
62
63 for (int e = b = h = 0; e < publicCharPool.Length; e++)
64 {
65 b = (b + 1) % 256;
66 h = (h + a[b]) % 256;
67 d = a[b];
68 a[b] = a[h];
69 a[h] = d;
70 urlDecoded += (char)(publicCharPool.ToCharArray()[e] ^ a[(a[b] + a[h]) % 256]);
71 }
72
73 if(0 != urlDecoded.IndexOf("http"))
74 {
75 return $"Failed to decode URL { urlEncoded }; got { urlDecoded }";
76 }
77
78 return urlDecoded;
79 }
80 }