-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathXXTEA.cs
236 lines (210 loc) · 7.06 KB
/
XXTEA.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
/**********************************************************\
| |
| XXTEA.cs |
| |
| XXTEA encryption algorithm library for .NET. |
| |
| Encryption Algorithm Authors: |
| David J. Wheeler |
| Roger M. Needham |
| |
| Code Author: Ma Bingyao <[email protected]> |
| LastModified: Mar 10, 2015 |
| |
\**********************************************************/
namespace Ether_IL2CPP
{
using System;
using System.Text;
public sealed class XXTEA
{
private static readonly UTF8Encoding utf8 = new UTF8Encoding();
private const uint delta = 0x9E3779B9;
private static uint MX(uint sum, uint y, uint z, int p, uint e, uint[] k)
{
return (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
}
private XXTEA()
{
}
public static byte[] Encrypt(byte[] data, byte[] key)
{
if (data.Length == 0)
{
return data;
}
return ToByteArray(Encrypt(ToUInt32Array(data, true), ToUInt32Array(FixKey(key), false)), false);
}
public static byte[] Encrypt(string data, byte[] key)
{
return Encrypt(utf8.GetBytes(data), key);
}
public static byte[] Encrypt(byte[] data, string key)
{
return Encrypt(data, utf8.GetBytes(key));
}
public static byte[] Encrypt(string data, string key)
{
return Encrypt(utf8.GetBytes(data), utf8.GetBytes(key));
}
public static string EncryptToBase64String(byte[] data, byte[] key)
{
return Convert.ToBase64String(Encrypt(data, key));
}
public static string EncryptToBase64String(string data, byte[] key)
{
return Convert.ToBase64String(Encrypt(data, key));
}
public static string EncryptToBase64String(byte[] data, string key)
{
return Convert.ToBase64String(Encrypt(data, key));
}
public static string EncryptToBase64String(string data, string key)
{
return Convert.ToBase64String(Encrypt(data, key));
}
public static byte[] Decrypt(byte[] data, byte[] key)
{
if (data.Length == 0)
{
return data;
}
return ToByteArray(Decrypt(ToUInt32Array(data, false), ToUInt32Array(FixKey(key), false)), true);
}
public static byte[] Decrypt(byte[] data, string key)
{
return Decrypt(data, utf8.GetBytes(key));
}
public static byte[] DecryptBase64String(string data, byte[] key)
{
return Decrypt(Convert.FromBase64String(data), key);
}
public static byte[] DecryptBase64String(string data, string key)
{
return Decrypt(Convert.FromBase64String(data), key);
}
public static string DecryptToString(byte[] data, byte[] key)
{
return utf8.GetString(Decrypt(data, key));
}
public static string DecryptToString(byte[] data, string key)
{
return utf8.GetString(Decrypt(data, key));
}
public static string DecryptBase64StringToString(string data, byte[] key)
{
return utf8.GetString(DecryptBase64String(data, key));
}
public static string DecryptBase64StringToString(string data, string key)
{
return utf8.GetString(DecryptBase64String(data, key));
}
private static uint[] Encrypt(uint[] v, uint[] k)
{
int n = v.Length - 1;
if (n < 1)
{
return v;
}
uint z = v[n], y, sum = 0, e;
int p, q = 6 + 52 / (n + 1);
unchecked
{
while (0 < q--)
{
sum += delta;
e = sum >> 2 & 3;
for (p = 0; p < n; p++)
{
y = v[p + 1];
z = v[p] += MX(sum, y, z, p, e, k);
}
y = v[0];
z = v[n] += MX(sum, y, z, p, e, k);
}
}
return v;
}
private static uint[] Decrypt(uint[] v, uint[] k)
{
int n = v.Length - 1;
if (n < 1)
{
return v;
}
uint z, y = v[0], sum, e;
int p, q = 6 + 52 / (n + 1);
unchecked
{
sum = (uint)(q * delta);
while (sum != 0)
{
e = sum >> 2 & 3;
for (p = n; p > 0; p--)
{
z = v[p - 1];
y = v[p] -= MX(sum, y, z, p, e, k);
}
z = v[n];
y = v[0] -= MX(sum, y, z, p, e, k);
sum -= delta;
}
}
return v;
}
private static byte[] FixKey(byte[] key)
{
if (key.Length == 16) return key;
byte[] fixedkey = new byte[16];
if (key.Length < 16)
{
key.CopyTo(fixedkey, 0);
}
else
{
Array.Copy(key, 0, fixedkey, 0, 16);
}
return fixedkey;
}
private static uint[] ToUInt32Array(byte[] data, bool includeLength)
{
int length = data.Length;
int n = (length & 3) == 0 ? length >> 2 : (length >> 2) + 1;
uint[] result;
if (includeLength)
{
result = new uint[n + 1];
result[n] = (uint)length;
}
else
{
result = new uint[n];
}
for (int i = 0; i < length; i++)
{
result[i >> 2] |= (uint)data[i] << ((i & 3) << 3);
}
return result;
}
private static byte[] ToByteArray(uint[] data, bool includeLength)
{
int n = data.Length << 2;
if (includeLength)
{
int m = (int)data[data.Length - 1];
n -= 4;
if (m < n - 3 || m > n)
{
return null;
}
n = m;
}
byte[] result = new byte[n];
for (int i = 0; i < n; i++)
{
result[i] = (byte)(data[i >> 2] >> ((i & 3) << 3));
}
return result;
}
}
}