forked from marcosvf132/TibiaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXtea.cs
More file actions
116 lines (104 loc) · 4.1 KB
/
Xtea.cs
File metadata and controls
116 lines (104 loc) · 4.1 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
using System;
namespace OXGaming.TibiaAPI.Network
{
/// <summary>
/// The <see cref="Xtea"/> class contains methods for encrypting and decrypting Tibia packets using the XTEA algorithm.
/// </summary>
public static class Xtea
{
private const uint Delta = 0x9E3779B9;
private const uint BlockSize = 8;
private const uint Rounds = 32;
/// <summary>
/// Decrypts a byte array using the XTEA algorithm.
/// </summary>
/// <param name="buffer">The byte array to decrypt.</param>
/// <param name="index">Index of the underlying <see cref="NetworkMessage"/> buffer to start the decryption from.</param>
/// <returns>
/// Returns true if decryption was done on the <see cref="NetworkMessage"/>, false otherwise.
/// </returns>
public static unsafe bool Decrypt(byte[] buffer, uint length, uint[] key, uint index = 6)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (key.Length != 4 || index >= length || ((length - index) % BlockSize) > 0)
{
return false;
}
fixed (byte* bufferPtr = buffer)
{
var uintPtr = (uint*)(bufferPtr + index);
var decryptSize = length - index;
for (var i = 0; i < (decryptSize / 4); i += 2)
{
var sum = 0xC6EF3720;
var count = Rounds;
while (count-- > 0)
{
uintPtr[i + 1] -= ((uintPtr[i] << 4 ^ uintPtr[i] >> 5) + uintPtr[i]) ^ (sum + key[sum >> 11 & 3]);
sum -= Delta;
uintPtr[i] -= ((uintPtr[i + 1] << 4 ^ uintPtr[i + 1] >> 5) + uintPtr[i + 1]) ^ (sum + key[sum & 3]);
}
}
}
return true;
}
/// <summary>
/// Encrypts a <see cref="NetworkMessage"/> using the XTEA algorithm.
/// </summary>
/// <param name="message">The <see cref="NetworkMessage"/> to encrypt.</param>
/// <param name="index">Index of the underlying <see cref="NetworkMessage"/> buffer to start the encryption from.</param>
/// <returns>
/// Returns true if encryption was done on the <see cref="NetworkMessage"/>, false otherwise.
/// </returns>
public static unsafe bool Encrypt(byte[] buffer, ref uint length, uint[] key, uint index = 6)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (key.Length != 4 || index >= length)
{
return false;
}
var encryptSize = length - index;
var padding = encryptSize % BlockSize;
if (padding > 0)
{
encryptSize += BlockSize - padding;
var newSize = index + encryptSize;
if (newSize > NetworkMessage.MaxMessageSize)
{
return false;
}
length = newSize;
}
fixed (byte* bufferPtr = buffer)
{
var uintPtr = (uint*)(bufferPtr + index);
for (var i = 0; i < (encryptSize / 4); i += 2)
{
var sum = 0u;
var count = Rounds;
while (count-- > 0)
{
uintPtr[i] += ((uintPtr[i + 1] << 4 ^ uintPtr[i + 1] >> 5) + uintPtr[i + 1]) ^ (sum + key[sum & 3]);
sum += Delta;
uintPtr[i + 1] += ((uintPtr[i] << 4 ^ uintPtr[i] >> 5) + uintPtr[i]) ^ (sum + key[sum >> 11 & 3]);
}
}
}
return true;
}
}
}