57 lines
1.7 KiB
C#
57 lines
1.7 KiB
C#
using System;
|
|
using System.Text;
|
|
|
|
namespace xaskel_decoder
|
|
{
|
|
public static class Tea
|
|
{
|
|
private const uint DELTA = 0x9E3779B9;
|
|
|
|
public static string Decode(string data, string key)
|
|
{
|
|
if (string.IsNullOrEmpty(data))
|
|
return string.Empty;
|
|
|
|
byte[] dataBytes = Convert.FromBase64String(data),
|
|
keyBytes = Encoding.UTF8.GetBytes(key);
|
|
|
|
uint[] key32 = new uint[4];
|
|
Buffer.BlockCopy(keyBytes, 0, key32, 0, Math.Min(keyBytes.Length, 16));
|
|
|
|
int dataLength = dataBytes.Length,
|
|
dataBlocks = dataLength / 4,
|
|
numPasses = dataBlocks - 1;
|
|
|
|
if (numPasses <= 0)
|
|
return string.Empty;
|
|
|
|
uint[] data32 = new uint[dataBlocks];
|
|
Buffer.BlockCopy(dataBytes, 0, data32, 0, dataBytes.Length);
|
|
|
|
uint previous = data32[numPasses];
|
|
for (int offset = numPasses - 1; offset >= 0; offset--)
|
|
{
|
|
uint v = data32[offset],
|
|
sum = 0xC6EF3720;
|
|
|
|
for (int i = 0; i < 32; i++)
|
|
{
|
|
previous -= (((v << 4) ^ (v >> 5)) + v) ^ (sum + key32[(sum >> 11) & 3]);
|
|
sum -= DELTA;
|
|
v -= (((previous << 4) ^ (previous >> 5)) + previous) ^ (sum + key32[sum & 3]);
|
|
}
|
|
|
|
data32[offset] = v;
|
|
}
|
|
|
|
int byteLength = dataBytes.Length - 5;
|
|
for (; byteLength >= 0 && dataBytes[byteLength] == 0; byteLength--) ;
|
|
|
|
byte[] resultBytes = new byte[byteLength + 1];
|
|
Buffer.BlockCopy(data32, 0, resultBytes, 0, resultBytes.Length);
|
|
|
|
return Encoding.UTF8.GetString(resultBytes);
|
|
}
|
|
}
|
|
}
|