-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCrypt.cs
169 lines (153 loc) · 5.21 KB
/
Crypt.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
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace TroubleTool
{
static class Crypt
{
private static readonly string keyInit = "d4152d461ab5308429e49774a042a318";
private static readonly string ivInit = "86afc43868fea6abd40fbf6d5ed50905";
private static byte[] GenerateIV(byte[] word)
{
byte[] crypto = new byte[16];
int v4 = 0;
int v5 = 0;
int v6, v7, v8, v9;
while (v4 < 0x20)
{
v6 = word[v5];
v7 = word[v4 + 1];
if ((v6 & 0x40) != 0)
{
if ((v6 & 0x20) != 0)
v8 = v6 - 87;
else
v8 = v6 - 55;
}
else
v8 = v6 - 48;
if ((v7 & 0x40) != 0)
{
if ((v7 & 0x20) != 0)
v9 = v7 - 87;
else
v9 = v7 - 55;
}
else
v9 = v7 - 48;
crypto[v5 >> 1] = (byte)(v9 | (16 * v8));
v4 += 2;
v5 = v4;
}
return crypto;
}
private static byte[] GenerateKey(byte[] a1)
{
long r9_1; // r9
long r10_1; // r10
long rax1; // rax
int ebx5; // ebx
long r11_6; // r11
byte dl7; // dl
byte r8_7; // r8
byte al9; // al
byte cl14; // cl
byte[] key = new byte[16];
r9_1 = a1.Length;
rax1 = a1.Length;
if (rax1 == 16)
{
ebx5 = 0;
if (r9_1 > 0)
{
r11_6 = 0;
do
{
dl7 = a1[r11_6];
r8_7 = a1[ebx5 + 1];
if ((dl7 & 0x40) != 0)
{
if ((dl7 & 0x20) != 0)
al9 = ((byte)(dl7 - 87));
else
al9 = (byte)(dl7 - 55);
}
else
{
al9 = (byte)(dl7 - 48);
}
if ((r8_7 & 0x40) != 0)
{
if ((r8_7 & 0x20) != 0)
cl14 = (byte)(r8_7 - 87);
else
cl14 = (byte)(r8_7 - 55);
}
else
{
cl14 = (byte)(r8_7 - 48);
}
ebx5 += 2;
rax1 = cl14 | (16 * al9);
key[r11_6 >> 1] = (byte)rax1;
r11_6 = ebx5;
} while (ebx5 < r9_1);
}
}
return key;
}
public static byte[] Encrypt(byte[] cipherText)
{
byte[] encrypted;
using (var aes = new AesManaged
{
KeySize = 16 * 8,
BlockSize = 16 * 8,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
})
{
using (var encrypter = aes.CreateEncryptor(GenerateKey(Encoding.ASCII.GetBytes(keyInit)), GenerateIV(Encoding.ASCII.GetBytes(ivInit))))
{
using (var cipherStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(cipherStream, encrypter, CryptoStreamMode.Write))
using (var binaryWriter = new BinaryWriter(cryptoStream))
{
//Encrypt Data
binaryWriter.Write(cipherText);
}
encrypted = cipherStream.ToArray();
}
}
}
return encrypted;
}
public static byte[] Decrypt(byte[] cipherText)
{
byte[] decrypted;
using (var aes = new AesManaged
{
KeySize = 16 * 8,
BlockSize = 16 * 8,
Mode = CipherMode.CBC,
Padding = PaddingMode.Zeros
})
{
using (var encrypter = aes.CreateDecryptor(GenerateKey(Encoding.ASCII.GetBytes(keyInit)), GenerateIV(Encoding.ASCII.GetBytes(ivInit))))
{
using (var cipherStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(cipherStream, encrypter, CryptoStreamMode.Write))
using (var binaryWriter = new BinaryWriter(cryptoStream))
{
binaryWriter.Write(cipherText);
}
decrypted = cipherStream.ToArray();
}
}
}
return decrypted;
}
}
}