-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncryptedInt.cs
More file actions
301 lines (237 loc) · 9.49 KB
/
EncryptedInt.cs
File metadata and controls
301 lines (237 loc) · 9.49 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//Copyright 2023 Kostasel
//See license.txt for license details
using System;
using System.Threading;
namespace EncryptedTypes
{
public struct EncryptedInt: IComparable<int>, IComparable<EncryptedInt>, IEquatable<EncryptedInt>, IEquatable<int>
{
[NonSerialized]
private int EncryptionKey;
[NonSerialized]
private int EncryptedValue;
private byte initialized;
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private EncryptedInt(ref int value)
{
EncryptionKey = CreateNewEncryptionKey();
EncryptedValue = EncryptValue(ref value, ref EncryptionKey);
initialized = 1;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
/// <summary>
/// Encrypts the value with the given key.
/// </summary>
public static int EncryptValue(ref int value, ref int key)
{
return ProccessValue(ref value, ref key, 0);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
/// <summary>
/// Decrypts the value with the given key.
/// </summary>
public static int DecryptValue(ref int value, ref int key)
{
return ProccessValue(ref value, ref key, 1);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private static int ProccessValue(ref int value, ref int key, byte mode)
{
//encrypt mode
if (mode == 0)
{
int origkey = key ^ ((0x1505 << 33) + 0x1505);
return (BitConverter.ToInt32(Transforms.TransformValueArray(value))) ^ origkey;
}
//decrypt mode
else if (mode == 1)
{
int origkey = key ^ ((0x1505 << 33) + 0x1505);
int decryptvalue = (value ^ origkey);
return BitConverter.ToInt32(Transforms.InvertTransformValueArray(decryptvalue));
}
return 0;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
/// <summary>
/// Get a new key for encryption.
/// </summary>
private static int CreateNewEncryptionKey()
{
//Get some random bytes for the key
int result = 0;
Random.GetNew(1000, 10000,ref result);
Span<byte> RandomData = Transforms.TransformValueArray(result);
byte a = 33;
for (int i = 0; i < 4; i += 2)
{
RandomData[i] = (byte) (((a << 5) + a) + RandomData[i]);
RandomData[i + 1] = (byte) (((a << 5) + a) + RandomData[i + 1]);
}
//Shuffle the key bytes
for (int i = 0; i < 4; i += 2)
{
RandomData[i] = Transforms.Transform((byte) (RandomData[i] ^ Transforms.Transform((byte) i)));
RandomData[i + 1] = Transforms.Transform((byte) (RandomData[i] ^ Transforms.Transform((byte) i)));
}
//Scramble the key before writing to variable
return (BitConverter.ToInt32(RandomData.ToArray(), 0)) ^ ((0x1505 << 33) + 0x1505);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public int GetDecryptedValue()
{
return DecryptedValue;
}
private int DecryptedValue
{
get { return DecryptValue(); }
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private int DecryptValue()
{
if (initialized == 0)
{
EncryptionKey = 0;
EncryptedValue = 0;
initialized = 1;
return 0;
}
return ProccessValue(ref EncryptedValue, ref EncryptionKey, 1);
}
public static implicit operator EncryptedInt(int value) => new(ref value);
public static implicit operator int(EncryptedInt value)
{
return value.DecryptValue();
}
public static EncryptedInt operator ++(EncryptedInt input)
{
return IncreaseValue(input);
}
public static EncryptedInt operator --(EncryptedInt input)
{
return DecreaseValue(input);
}
public static EncryptedInt operator +(EncryptedInt input, int value)
{
return AddValue(input, value);
}
public static EncryptedInt operator -(EncryptedInt input, int value)
{
return SubValue(input, value);
}
public static bool operator <(EncryptedInt left, EncryptedInt right)
{
return left.CompareTo(right) < 0;
}
public static bool operator <=(EncryptedInt left, EncryptedInt right)
{
return left.CompareTo(right) <= 0;
}
public static bool operator >(EncryptedInt left, EncryptedInt right)
{
return left.CompareTo(right) > 0;
}
public static bool operator >=(EncryptedInt left, EncryptedInt right)
{
return left.CompareTo(right) >= 0;
}
public static bool operator <(EncryptedInt left, int right)
{
return left.CompareTo(right) < 0;
}
public static bool operator <=(EncryptedInt left, int right)
{
return left.CompareTo(right) <= 0;
}
public static bool operator >(EncryptedInt left, int right)
{
return left.CompareTo(right) > 0;
}
public static bool operator >=(EncryptedInt left, int right)
{
return left.CompareTo(right) >= 0;
}
public static bool operator ==(EncryptedInt left, EncryptedInt right)
{
return left.Equals(right);
}
public static bool operator !=(EncryptedInt left, EncryptedInt right)
{
return !left.Equals(right);
}
public static bool operator ==(EncryptedInt left, int right)
{
return left.Equals(right);
}
public static bool operator !=(EncryptedInt left, int right)
{
return !left.Equals(right);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private static EncryptedInt IncreaseValue(EncryptedInt value)
{
int tmp = value.DecryptValue();
Interlocked.Increment(ref tmp);
value.EncryptedValue = EncryptValue(ref tmp, ref value.EncryptionKey);
return value;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private static EncryptedInt DecreaseValue(EncryptedInt value)
{
int tmp = value.DecryptValue();
Interlocked.Decrement(ref tmp);
value.EncryptedValue = EncryptValue(ref tmp, ref value.EncryptionKey);
return value;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private static EncryptedInt AddValue(EncryptedInt value, int amount)
{
int tmp = value.DecryptValue();
Interlocked.Add(ref tmp, amount);
value.EncryptedValue = EncryptValue(ref tmp, ref value.EncryptionKey);
return value;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private static EncryptedInt SubValue(EncryptedInt value, int amount)
{
int tmp = value.DecryptValue();
Interlocked.Exchange(ref tmp, (tmp - amount));
value.EncryptedValue = EncryptValue(ref tmp, ref value.EncryptionKey);
return value;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)
{
return obj is EncryptedInt && Equals((EncryptedInt) obj);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool Equals(EncryptedInt obj)
{
if (EncryptionKey == obj.EncryptionKey)
{
return EncryptedValue.Equals(obj.EncryptedValue);
}
return DecryptValue(ref EncryptedValue, ref EncryptionKey).Equals(DecryptValue(ref obj.EncryptedValue, ref obj.EncryptionKey));
}
public bool Equals(int other)
{
return DecryptedValue.Equals(other);
}
public int CompareTo(EncryptedInt other)
{
return DecryptValue().CompareTo(other.DecryptValue());
}
public int CompareTo(int other)
{
return DecryptValue().CompareTo(other);
}
public override int GetHashCode()
{
return DecryptValue().GetHashCode();
}
public override string ToString()
{
return DecryptedValue.ToString();
}
}
}