mhf-server/Mhf.Server/Packet/PacketFactory.cs
2019-11-24 19:43:35 +08:00

401 lines
39 KiB
C#

using System;
using System.Collections.Generic;
using Arrowgene.Services.Buffers;
using Arrowgene.Services.Logging;
using Mhf.Server.Common;
using Mhf.Server.Logging;
using Mhf.Server.Setting;
namespace Mhf.Server.Packet
{
public class PacketFactory
{
public const int PacketHeaderSize = 14;
public const int InitPacketSize = 8;
public const uint CryptoInit = 995117;
public static byte[] EncryptKey = new byte[]
{
0x90, 0x51, 0x26, 0x25, 0x04, 0xBF, 0xCF, 0x4C, 0x92, 0x02, 0x52, 0x7A, 0x70, 0x1A, 0x41, 0x88, 0x8C, 0xC2,
0xCE, 0xB8, 0xF6, 0x57, 0x7E, 0xBA, 0x83, 0x63, 0x2C, 0x24, 0x9A, 0x67, 0x86, 0x0C, 0xBE, 0x72, 0xFD, 0xB6,
0x7B, 0x79, 0xB0, 0x22, 0x5A, 0x60, 0x5C, 0x4F, 0x49, 0xE2, 0x0E, 0xF5, 0x3A, 0x81, 0xAE, 0x11, 0x6B, 0xF0,
0xA1, 0x01, 0xE8, 0x65, 0x8D, 0x5B, 0xDC, 0xCC, 0x93, 0x18, 0xB3, 0xAB, 0x77, 0xF7, 0x8E, 0xEC, 0xEF, 0x05,
0x00, 0xCA, 0x4E, 0xA7, 0xBC, 0xB5, 0x10, 0xC6, 0x6C, 0xC0, 0xC4, 0xE5, 0x87, 0x3F, 0xC1, 0x82, 0x29, 0x96,
0x45, 0x73, 0x07, 0xCB, 0x43, 0xF9, 0xF3, 0x08, 0x89, 0xD0, 0x99, 0x6A, 0x3B, 0x37, 0x19, 0xD4, 0x40, 0xEA,
0xD7, 0x85, 0x16, 0x66, 0x1E, 0x9C, 0x39, 0xBB, 0xEE, 0x4A, 0x03, 0x8A, 0x36, 0x2D, 0x13, 0x1D, 0x56, 0x48,
0xC7, 0x0D, 0x59, 0xB2, 0x44, 0xA3, 0xFE, 0x8B, 0x32, 0x1B, 0x84, 0xA0, 0x2E, 0x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59, 0x98, 0xC6, 0x55, 0xFC, 0x9F, 0xA3, 0xB7, 0x74, 0xEE, 0x31, 0x98, 0xE6, 0xB7,
0xBE, 0x26, 0xF4, 0x3C, 0x76, 0xF1, 0x23, 0x7E, 0x02, 0x4E, 0x3C, 0xD1, 0xC7, 0x28, 0x23, 0x73, 0xC4, 0xD9,
0x5E, 0x0D, 0xA1, 0x80, 0xA5, 0xAA, 0x26, 0x0A, 0xA3, 0x44, 0x82, 0x74, 0xE6, 0x3C, 0x44, 0x27, 0x51, 0x0D,
0x5F, 0xC7, 0x9C, 0xD6, 0x63, 0x67, 0xA5, 0x27, 0x97, 0x38, 0xFB, 0x2D, 0xD3, 0xD6, 0x60, 0x25, 0x83, 0x4D,
0x37, 0x5B, 0x40, 0x59, 0x11, 0x77, 0x51, 0x11, 0x14, 0x18, 0x07, 0x63, 0xB1, 0x34, 0x3D, 0xB8, 0x60, 0x13,
0xC2, 0xE8, 0x13, 0x82
};
private readonly MhfLogger _logger;
private readonly MhfSetting _setting;
private int _position;
private IBuffer _buffer;
private PacketHeader _header;
private uint _readKeyRot;
private uint _sendKeyRot;
private uint _sentPackets;
private ushort _combinedCheck;
public PacketFactory(MhfSetting setting)
{
_logger = LogProvider.Logger<MhfLogger>(this);
_setting = setting;
_readKeyRot = CryptoInit;
_sendKeyRot = CryptoInit;
_sentPackets = 0;
_header = null;
Reset();
}
public byte[] Write(MhfPacket packet)
{
if (packet.Data.Size > ushort.MaxValue)
{
_logger.Error(
$"Packet Size: {packet.Data.Size} exceeds maximum size of {ushort.MaxValue} for PacketId: {packet.Id}");
return null;
}
byte[] data = packet.Data.GetAllBytes();
byte keyRotDelta = 2;
// Update the rolling key index.
if (keyRotDelta != 0)
{
_sendKeyRot = keyRotDelta * (_sendKeyRot + 1);
}
data = Encrypt(data, _sendKeyRot,
out ushort combinedCheck, out ushort check0,
out ushort check1, out ushort check2);
packet.Header.DataSize = (ushort)data.Length;
packet.Header.Pf0 = (byte) (((packet.Header.DataSize >> 12) & 0xF3) | 3);
packet.Header.KeyRotDelta = keyRotDelta;
packet.Header.CombinedCheck = 0;
packet.Header.Check0 = check0;
packet.Header.Check1 = check1;
packet.Header.Check2 = check2;
Console.WriteLine(packet.Header.ToLogText());
IBuffer buffer = BufferProvider.Provide();
buffer.WriteByte(packet.Header.Pf0);
buffer.WriteByte(packet.Header.KeyRotDelta);
buffer.WriteInt16(packet.Header.Id, Endianness.Big);
buffer.WriteInt16(packet.Header.DataSize, Endianness.Big);
buffer.WriteInt16(packet.Header.CombinedCheck, Endianness.Big);
buffer.WriteInt16(packet.Header.Check0, Endianness.Big);
buffer.WriteInt16(packet.Header.Check1, Endianness.Big);
buffer.WriteInt16(packet.Header.Check2, Endianness.Big);
buffer.WriteBytes(data);
byte[] final = buffer.GetAllBytes();
return final;
}
public List<MhfPacket> Read(byte[] data)
{
List<MhfPacket> packets = new List<MhfPacket>();
if (_buffer == null)
{
_buffer = BufferProvider.Provide(data);
}
else
{
_buffer.SetPositionEnd();
_buffer.WriteBytes(data);
}
_buffer.Position = _position;
bool read = true;
while (read)
{
read = false;
if (_header == null && _buffer.Size - _buffer.Position >= PacketHeaderSize)
{
byte pf0 = _buffer.ReadByte();
byte keyRotDelta = _buffer.ReadByte();
ushort id = _buffer.ReadUInt16(Endianness.Big);
ushort dataSize = _buffer.ReadUInt16(Endianness.Big);
ushort combinedCheck = _buffer.ReadUInt16(Endianness.Big);
ushort check0 = _buffer.ReadUInt16(Endianness.Big);
ushort check1 = _buffer.ReadUInt16(Endianness.Big);
ushort check2 = _buffer.ReadUInt16(Endianness.Big);
_header = new PacketHeader(id, pf0, keyRotDelta, dataSize, combinedCheck, check0, check1, check2);
Console.WriteLine(_header.ToLogText());
// Update the rolling key index.
if (_header.KeyRotDelta != 0)
{
_readKeyRot = _header.KeyRotDelta * (_readKeyRot + 1);
}
}
if (_header == null && _buffer.Size - _buffer.Position == InitPacketSize)
{
byte[] payload = _buffer.ReadBytes(InitPacketSize);
_logger.Debug($"Ignoring Data: {Util.ToHexString(payload, ' ')}");
}
if (_header != null && _buffer.Size - _buffer.Position >= _header.DataSize)
{
byte[] packetData = _buffer.ReadBytes(_header.DataSize);
packetData = Decrypt(packetData, _readKeyRot,
out ushort combinedCheck, out ushort check0,
out ushort check1, out ushort check2);
IBuffer buffer = BufferProvider.Provide(packetData);
MhfPacket packet = new MhfPacket(_header, buffer);
packets.Add(packet);
_header = null;
read = _buffer.Position != _buffer.Size;
}
}
if (_buffer.Position == _buffer.Size)
{
Reset();
}
else
{
_position = _buffer.Position;
}
return packets;
}
private void Reset()
{
_header = null;
_position = 0;
_buffer = null;
}
public void Test()
{
byte[] me = Util.FromHexString(
"010201011E952C4F6D66366B414C5765553248535635755DD6002F0C75306D68662D672E6A70000C6C302E6D682D672E6A7000143130362E3138352E342E36313A3533333130002175A5E90000005DD0AE99000100010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000028B3C424F44593E3C43455445523E3C53495A455F333E3C435F3E918D93FC8DB03490E7969C8DB09290AC82C582A882BD82DF82B58FAC9482663939399687947A957A81493C423E3C424F44593E91E631343789F18E906C8DD581758F9F82BF836C83528282E782CC92A790ED8FF38176936F988DD53C42523E3C424F44593E3C42523C424F44593E3C42523E3C424F44593C4C4546543E3C435F373E3C53495A5F323E8DA189F182C58DC58CE382C6C882E98EEB906C8DD581758F9F82BF6C835282A982E782CC92A790ED8FF3768A4A8DC381493C42523E3C424F443E3C42523E3C424F44593E3C4C45463E3C435F333E3C53495A455F323E81936F985E8DD58A4A8DC38AFA8AD4813C435F373E3C42523E3C424F44593E318C8E323093FA2890852920816020318C8E323793FA2890852983818393658369839383588A4A8E6E82DC82C5435F373E3C42523E3C424F44593E3C523E3C424F44593E91E631343789F1EB906C8DD582C582CD81418354815B6F815B918D93FC8DB0909482C9899EB682C481413C42523E3C424F44593E435F353E82A882BD82DF82B58FAC9482663939399687815E8A6C93BE9166DE90943130947B81413C42523E3C4244593E8F9F82BF9167834E834783586782F08343837883938367834E834758836782C682B582C4947A904D3C43373E82C882C73C42523E3C424F445993C195CA82C8964A8FDC82F092698A934982C982B2977088D382B582C48282E882DC82B781423C42523E3C424F593E3C42523E3C424F44593E836E83835E815B82CC8A46976C81418DC58C82CC8EEB906C8DD582C982BA82D08E89C182B582C482AD82BE82B382A28100000000000000000E2A64736D63000C00003D0000008181000000000000000000816A000000000000280000006900000000000021000000814900000000002F000000815E000000000000000000817B0000000000002600000095000000000000825000003100000000000082DA0000837B0000CE00DE00D9DE00837ADE0082D9814A837A814A00814A0000000082D700008378000000DE0082D6DE008377DE0082D6814A00814A8377814A0000000082C50000660000C300DE0082C4DE008365DE00C4814A8365814AC300814A81A7814AA7DE0089B3DE0089B3814A00000000D1000083720000CB00DE0082D0DE0071DE0082D0814A8371814ACB00814A00000082C7000083680000C400DE00C6DE008367DE0082C6814A8367814A00814A84B0DE0084B0814A84A5DE00A5814A0000000082CE0000836F000000DE0082CDDE00836EDE0094AADE00CD814A836E814ACA00814A94AA814A00000082C2DE00836400008363DE00C2814A8363814AC200DE00C200814AC3000082C1DE008362DE00AF00DE00C1814A8362814AAF00814A00000000D4000083750000CC00DE008394000000DE0082A4814A82A4DE008345DE0000DE0082A3DE0082D3DE008374DE0000814A0000000082C000008361000000DE0082BFDE008360DE0082BF814A60814AC100814A90E7814A90E7DE0000000082BE0000835F0000C000DE00BDDE00835EDE00975BDE0082BD814A5E814AC000814A975B814A00000000BC0000835D0000BF00DE0082BBDE005CDE0082BB814A835C814ABF00814A93DE008393814ADD00814ADD00DE008ADE00D800DE00D800814A838A814A00000082BA0000BE00DE0082B9DE005ADE0082B9814A835A814ABE00814A5B00000000000082B800008359000000DE0082B7DE008358DE0082B7814A58814ABD00814A0000000082B60000570000BC00DE0082B5DE008356DE00B5814A8356814ABC00814A00000000B4000083550000BB00DE0082B3DE0054DE0082B3814A8354814ABB00814A00000082B2000083530000BA00DE00B1DE008352DE0082B1814A8352814A00814A0000000082B000008351000000DE0082AFDE008350DE0082AF814A50814AB900814A8396DE008396814A00000082AE0000834F0000B800DE00ADDE00834EDE0082AD814A834E814A00814A0000000082AC0000834D000000DE0082ABDE00834CDE0082AB814A4C814AB700814A0000000082AA00004B0000B600DE008395DE00834ADE00A9DE0097CDDE008395814A834A814AA9814A97CD814AB600814A00000000F0000083920000A600000000000000ED0000838F0000DC000000838E000000000082EB0000838D0000DB000000A000008CFB00000000000082EA00008C0000DA0000000000000082E900008B0000D90000000000000082E800008A0000D80000000000000082E70000890000D70000000000000082E60000880000D6000000AE00000082E500008700000000000082E4000083860000000000AD00000082E300008385000000000082E2000083840000D400000000000082E100008383000000000000E0000083820000D300000000000000DF000083810000D20000004D00450000000082DE000083800000D100000000000082DD0000837E0000D000000000000082DC0000837D0000CF00000000000082D90000837A0000CE00000000000082D6000083770000CD00000000000082D3000083740000CC00000000000082D0000083710000CB00000000000082CD0000836E0000CA000000AA00000000000082CC0000836D00000000000000000082CB0000836C00000000000000000082CA0000836B00000000000000000082C90000836A000000000093F100000000000082C80000690000C50000000000000082C60000670000C400000084B0000084A5000000000082C4000083650000C3000000A7000089B300000000000082C20000630000C200000082C10000836200000000000000000082BF00008360000000000090E700000000000082BD00005E0000C0000000975B000000000000BB0000835C0000BF00000000000000B90000835A0000BE00000000000000B7000083580000BD00000000000000B5000083560000BC00000000000000B3000083540000BB00000000000000B1000083520000BA00000000000000AF000083500000B90000008396000000000082AD0000834E0000B800000000000082AB0000834C0000B700000000000082A90000834A0000B600000095000097CD00000000000082A80000490000B5000000AB00000082A700004800000000000082A6000083470000000000AA00000082A50000834600004800000000000082A4000083450000000000A900000082A300008344000000000082A2000083430000B200000000000082A100008342000000000000000000B1000000829F000082A000004000008341000000000000815B00005C0000815D0000816000002D0000007C0000B000000088EA000000000000000000825800000000000038000000570000000000003700000082560000000000360000008255000000000000000000825400000000000034000000530000000000003300000082520000000000320000008251000000000000DB0000837C0000CE00DF0082D9DF007ADF00837A818B82D9818BCE00818B00000082D8000083790000CD00DF00D6DF008377DF008377818B82D6818B00818B0000000082D500008376000000DF0082D3DF008374DF008374818BD3818BCC00818B0000000082D20000730000CB00DF0082D0DF008371DF0071818B82D0818BCB00818B00000000CF000083700000CA00DF0082CDDF006EDF00836E818B82CD818BCA00818BAADF0094AA814B00000000827900009A00005A0000007A00000083A4000000000082780000829900005900000000000083B20000845400008485000000000082770000829800005800000000000083B4000083D40000845600007E000084870000875D000000000000760000829700005700000077000000D6000084590000848A0000848B000000000082750000829600005600000000000083CB000083D200008758000000000082740000829500005500000000000083CA000081BE000000000000730000829400005400000074000000B1000083D10000845300008484000000000024000000530000007300000090000081E700008272000082930000000000827100008292000052000000000000846000008491000000000000700000829100005100000071000000000000826F0000829000005000000000000083AF000083CF00008451000082000000000000826E0000828F00000000006F000000819B000083AD0000CD0000844F00008480000081FC00005A000030000000824F0000000000006D0000828E00004E0000006E000000AB000083C50000DD00000082F1000093000000000000826C0000828D00000000006D00000083AA0000844D00007D000000000000826B0000828C00000000006C000000879800007C000000000000826A0000828B00004B00000000000083A8000083C80000844B00007B00000000000082690000828A00000000006A0000000000000082680000890000490000006900000083A700005400000000000082670000828800000000006800000083A50000844E00007E0000000000008265000082860000000000660000000000000082660000870000470000006700000000000000640000828500004500000065000000A3000083C3000084450000844600007500008476000081B80000000000006300008284000044000000640000000000004300000082830000630000005200008483000082620000818E000000000042000000826100008282000000000083C0000083A0000084420000720000848C0000848E000081F30000000000270000008166000000000000000000826000006100000082810000BF0000400000008197000084700000F0000084400000839F00000000000000000081680000000000002500000093000000000000000000006E616D0016000002000000937AFFFF97EAFFFF00FFFF0400000082B78B0182A9A801C6660182EBFC000000FFFF0200000073FFFF8F97FFFF0000FFFF03000000A2C401826D870282DBF3010000FFFF0000008266C2024100EF02826D870269A9024100EF020000FFFF04000000C46C0182A2C401826D870282B1970100FFFF0300000082DC380182BB8301B197010000FFFF0300000082BF7901BB830182B197010000FFFF02000000ADFFFF9390FFFF0000FFFF04000000CF170282A2C40182CF1702826D870200FFFF03000000965CFFFF82A9A80163FFFF0000FFFF0400000082B98701ADA00182EBFC0082B78B010000FFFF00000082CE4D0082BD7E01815BD201A2FFFF0000FFFF0500000082DC3801E80A0182D34401A700CB0182C8620100FFFF0400000082D5050282C27201B58F01815BD2010000FFFF04000000B1970182A9A80182A2C401826D870200FFFF0400000082C46C0182A2C4016D870282DBF3010000FFFF04000000D13700815BD20182BF790182ADA00100FFFF0200000090ABFFFF977EFFFF00FFFF0300000082C8620182BF7901B78B010000FFFF0400000082EBFC00E80A0182B19701826D87020000FFFF00000082C46C0182A2C40182DE3001DBF3010000FFFF0400000082C52A00A9A80182DC380182E70E010000FFFF00000082CF170282A2C40182B8A500E80A010000FFFF0400000082DC38016D870282A9A80182B78B010000FFFF0000004100EF028263D302826C910268AE02826D87028268AE0224005C02735302827164024100EF02827353026E7902827164020000FFFF06000000A8AE018B71FFFF976CFFFF918AFFFF6BFFFF8EBAFFFF0000FFFF02000000F2FFFF95A8FFFF0000FFFF04000000D2FFFF967BFFFF8C9BFFFF8E4FFFFF00FFFF0400000092D2FFFF967BFFFFB0FFFF8D4FFFFF0000FFFF04000000ACFFFF96ECFFFF8B60FFFF93BFFFFF00FFFF0300000082716402826C91027353020000FFFF040000009099FFFF59FFFF815BD20193BFFFFF0000FFFF00000092D2FFFF967BFFFF97C7FFFF4FFFFF0000FFFF0300000093A1FFFFAAFFFF9776FFFF0000FFFF040000005BD20190A3FFFF91D7FFFF94CDFFFF00FFFF050000008FACFFFF9388FFFF54FFFF91BEFFFF9859FFFF0000FFFF00000091E5FFFF9683FFFF0000FFFF0000008A6FFFFF90C1FFFF8DDCFFFF00FFFF020000009FAFFFFF92B0FFFF00FFFF020000009683FFFF96F2FFFF00FFFF0300000092AAFFFF9081FFFFABA4010000FFFF0300000082A9A801D505029859FFFF0000FFFF0100000079FFFF0000FFFF01000000817AFFFF00FFFF020000008941FFFF8C73FFFF00FFFF020000009675FFFF8B4EFFFF00FFFF0200000089BAFFFF979FFFFF00FFFF0600000082A8AE0182A4BD01DE3001905EFFFF979DFFFF8BB3FFFF00FFFF090000008269A9024100EF026F7002826AA0028268AE02826B99026B99028264C702827164020000FFFF0000008B43FFFF88E1FFFF82A2C40100FFFF040000008B43FFFF82BF7901AAE60082A2C4010000FFFF04000000ABA40182BF790182AAE60082A2C40100FFFF030000008B43FFFF8BB6FFFFA2C4010000FFFF0400000090B8FFFF5FFFFF9496FFFF8EE3FFFF0000FFFF000000926DFFFF8C62FFFF9278FFFFEA02010000FFFF0500000082BF7901A6B50182A8AE0182ADA00182EA020100FFFF0200000096DAFFFF88C3FFFF00FFFF0200000096BEFFFF96D3FFFF00FFFF05000000A700CB0182ABA401DF2B0182ADA00182E70E010000FFFF00000082EBFC00826D870282CF1702E80A010000FFFF02000000E19AFFFF61FFFF0000FFFF0300000082E70E01A2C4019561FFFF0000FFFF0200000092FFFF9273FFFF0000FFFF01000000EAFFFF0000FFFF0300000082BF79016D870282CE4D000000FFFF01000000FBFFFF0000FFFF0200000098FBFFFF43FFFF0000FFFF02000000936AFFFF45FFFF0000FFFF020000008CEEFFFF48FFFF0000FFFF02000000E6CBFFFFAFFFFF0000FFFF02000000E271FFFFBDFFFF0000FFFF0200000094F1FFFF6CFFFF0000FFFF030000009056FFFFBDFFFF96AFFFFF0000FFFF04000000C1FFFF8EEAFFFF9594FFFF978EFFFF00FFFF0500000082CE4D0082A9A801BF790182E61201826D87020000FFFF00000082BF790182E61201826D8702F6FFFF0000FFFF0500000082C66601E9060182B197019597FFFF9843FFFF00FFFF0400000082C6660182E90601B197018FECFFFF0000FFFF0300000000CB0182C8620182E906010000FFFF00000082A4BD01826D870282B1970100FFFF0200000096D1FFFF9382FFFF00FFFF030000008D95FFFF826D8702DA18000000FFFF040000009573FFFFC2FFFF9047FFFF96AFFFFF0000FFFF0000009484FFFF8F97FFFF0000FFFF00000088FAFFFF9484FFFF0000FFFF0000008BADFFFF8AADFFFF0000FFFF00000082E2200182E80A0182DC38016D87020000FFFF0300000082DC38016D870282B197010000FFFF03000000A8AE0182DF2B0182B197010000FFFF00000082BF7901826D870282B1970100FFFF0300000082BF7901826D8702DBF3010000FFFF0300000082BF79016D870282DA18000000FFFF04000000B9870182C2720182ADA00182B78B0100FFFF0300000024005C028264C7027730020000FFFF020000009484FFFF74FFFF0000FFFF020000009483FFFF74FFFF0000FFFF0300000082EA0201A2C40182D505020000FFFF04000000B78B0182D8FC0182E9060182DC380100FFFF0500000082ADA00182E80A01C6660182E80A0182B78B010000FFFF00000082D3440182A6B50182E70E01BF790182A8AE010000FFFF03000000B4B700815BD20196CBFFFF0000FFFF00000082B4B700815BD20182DF2B016D87020000FFFF0400000082B987016D870282B8A50082E80A010000FFFF00000082A8AE0182C8620182C95D015BD2010000FFFF030000008FE1FFFF51FFFF8ED2FFFF0000FFFF0400000065BD0282744C024300D802826AA00200FFFF0400000082D34401A700CB01C2720182ADA0010000FFFF05000000BF790182E22001826D870282B19701EBFC000000FFFF050000004200E00268AE02827353024300D8028267B50200FFFF040000004300D80282744C026D8702827353020000FFFF050000006F70028264C702826D87028268AE02005C020000FFFF0300000082D8FC01C95D0182B78B010000FFFF060000007544024100EF028266C2028268AE026D87024100EF020000FFFF03000000CE4D0082ACDD0082C862010000FFFF00000082D46700A700CB0182ACDD00C862010000FFFF05000000826F7002744C0224005C0224005C028278280200FFFF0200000090B8FFFF8974FFFF00FFFF020000008E80FFFF82CB550100FFFF020000008E45FFFF82B78B0100FFFF0100000095B3FFFF0000FFFF0000008998FFFF95A8FFFF0000FFFF00000082BF7901826D870282DBF301B197010000FFFF0400000082DBF301B1970182BF7901826D87020000FFFF00000082B3930182B987018E71FFFF00FFFF020000008941FFFF96D1FFFF00FFFF020000008BCAFFFF8BE0FFFF00FFFF0400000082A9A80182D50502B19701826D87020000FFFF04000000A9A80182D5050282B1970182BB830100FFFF0400000082A9A80182D505026D870282B197010000FFFF0600000000D8024100EF02826F70024300D8026E7902826C91020000FFFF0600000000D8024100EF02826F70024300D8026E7902826D87020000FFFF0600000000D8024100EF02826F7002826AA0026E7902826C91020000FFFF0600000000D8024100EF02826F7002826AA0026E7902826D87020000FFFF060000006AA0024100EF02826F70024300D8026E7902826C91020000FFFF060000006AA0024100EF02826F70024300D8026E7902826D87020000FFFF060000006AA0024100EF02826F7002826AA0026E7902826C91020000FFFF060000006AA0024100EF02826F7002826AA0026E7902826D87020000FFFF0900000066C2024100EF0282DF2B01826C910200EF0224005C02827353028264C7027164020000FFFF0A0000008266C20200EF0282DF2B0124005C0282744C026F7002826F7002826E7902827164027353020000FFFF0600000024005C0278280224005C02827353028264C7026C91020000FFFF0400000082B58F01B78B0182C46C0182DE30010000FFFF0000008264C702827544028264C7026D8702827353020000FFFF04000000A2C40182D72100826D870282C6660100FFFF0500000024005C028273530200EF028265BD028265BD020000FFFF00000082B78B0182BD7E0182C27201D344010000FFFF08000000826E790265BD028265BD028268AE024300D80268AE024100EF02826B99020000FFFF00000082A8AE0182D3440182A2C401B58F0182E2200182E906010000FFFF00000024005C0282744C02826F70026F7002826E7902827164028273530200FFFF0400000082B3930182DBF3015BD20182C666010000FFFF0500000000EF028263D302826C91028268AE026D87020000FFFF04000000A700CB01C7400082DD3401826D87020000FFFF000000A700CB0182C7400082DD3401BB83010000FFFF04000000A700CB01C7400082DD340182C95D010000FFFF000000A700CB0182C7400082DD3401C95D010000FFFF02000000895EFFFF63FFFF0000FFFF0400000082A4BD016D870282A6B50182A2C4010000FFFF0000008AC7FFFF979DFFFF0000FFFF00000082A9A801826D870282E80A0100FFFF030000008AD6FFFF8C57FFFFD2FFFF0000FFFF030000008A4AFFFFADFFFF8ED2FFFF0000FFFF04000000A8AE01926DFFFF82E70E0182B9870100FFFF0400000082A8AE0182B58F01E70E0182B987010000FFFF0200000090FFFF926DFFFF0000FFFF03000000D4FFFF88C0FFFF9577FFFF0000FFFF00000082E2200182E80A0182BF79016D87020000FFFF0300000082E22001ADA00182B4B7000000FFFF04000000D2FFFF967BFFFF8F74FFFF8D4FFFFF00FFFF0300000096D8FFFF967BFFFF63D3020000FFFF0400000096D8FFFF7BFFFF97B4FFFF8CC8FFFF0000FFFF0000008B7BFFFF89BAFFFF4100EF026F70020000FFFF040000008B7BFFFFBAFFFF8B50FFFF8EF7FFFF0000FFFF000000826C91028267B5028265BD025BD2018266C20282BF7901815BD201DE30010000FFFF06000000826C910267B5028265BD0282BF7901815BD201DE30010000FFFF040000008B43FFFFAED40082E9060182A2C4010000FFFF00000082ABA40182AED40082E90601A2C4010000FFFF0200000090B8FFFF96FFFF0000FFFF0200000088FAFFFF90FFFF0000FFFF020000009641FFFF50FFFF0000FFFF03000000945DFFFF8CFFFF898AFFFF0000FFFF0200000054FFFF93AAFFFF0000FFFF03000000DF2B0182ADA00182E70E010000FFFF00000093F7FFFF965FFFFF0000FFFF000000956EFFFF93FBFFFF0000FFFF00000082BE800082C2720182BF7901EDF70082A2C40182D344010000FFFF0000009857FFFF0000FFFF03000000C27201826D870282DA18000000FFFF0000008B68FFFF82E80A010000FFFF0000009181FFFF9852FFFF0000FFFF00000082A2C40182DC380182E70E01BF790182A8AE010000FFFF05000000A8AE0182C8620182C95D0182B78B01C666010000FFFF0200000088A4FFFF74FFFF0000FFFF020000008AE7FFFFCBFFFF0000FFFF0200000095D0FFFF5BFFFF0000FFFF020000009790FFFFF0FFFF0000FFFF0300000082B98701DE300182B58F010000FFFF030000004FFFFF82C2720182EBFC000000FFFF000000E3E8FFFF96E5FFFF0000FFFF00000093F7FFFF92D9FFFF0000FFFF00000090ABFFFF8AB4FFFF91D1FFFF00FFFF0500000082A8AE01815BD201AAE60082B8A50082DE30010000FFFF0000008EA9FFFF88D4FFFF0000FFFF00000093FBFFFF97D6FFFF0000FFFF0000009278FFFF9852FFFF0000FFFF0000008DD7FFFF96AFFFFF0000FFFF0000008B87FFFF96AFFFFF0000FFFF00000094D8FFFF91B0FFFF0000FFFF000000966BFFFF914EFFFF0000FFFF00000093ECFFFF914EFFFF0000FFFF0000008E4FFFFF8D91FFFF906CFFFF00FFFF0200000097BDFFFF904AFFFF00FFFF0200000096B2FFFF90B8FFFF00FFFF010000008FA8FFFF0000FFFF0000008FA9FFFF9577FFFF0000FFFF0000008F97FFFF8941FFFF0000FFFF0000008EE8FFFF88FAFFFF0000FFFF0000008D67FFFF96D1FFFF906CFFFF00FFFF020000009849FFFF8F95FFFF00FFFF020000008987FFFF8CF0FFFF00FFFF0400000082ADA00182EBFC006D870282DA18000000FFFF0300000000CB0182DF2B018CF6FFFF0000FFFF00000082A2C40182BD7E018CF6FFFF00FFFF040000009553FFFF8AD1FFFFC52A0082D467000000FFFF04000000E90601826D870282D8FC01826D870200FFFF020000008941FFFF9594FFFF00FFFF020000008941FFFF904FFFFF00FFFF0200000088FAFFFF8D73FFFF00FFFF0400000082BF7901826D8702BF7901826D87020000FFFF020000006AFFFF8DAAFFFF0000FFFF05000000A8AE018F95FFFF82AF9B018EEBFFFF6CFFFF0000FFFF0300000082BF79016D870296D1FFFF0000FFFF020000004EFFFF906CFFFF0000FFFF03000000ADA001826D870282C95D010000FFFF0000009099FFFF8959FFFF826F700200FFFF0200000093B6FFFF92E5FFFF00FFFF0200000095EFFFFF8C73FFFF00FFFF020000008BE0FFFF8BCAFFFF00FFFF0400000082A8AE0182C27201CF170282A2C4010000FFFF03000000BF790182ADA00182D137000000FFFF000000A700CB0182C8620182E9060100FFFF03000000A700CB0182CA5901B78B010000FFFF0200000093FBFFFFF1FFFF0000FFFF01000000E453FFFF00FFFF0200000090C2FFFF8AADFFFF00FFFF0200000096ECFFFF8AADFFFF00FFFF0200000095D6FFFF94E9FFFF00FFFF020000008FACFFFF95D6FFFF00FFFF0200000091E5FFFF95D6FFFF00FFFF0300000082D1370082C27201BF79010000FFFF0300000082A4BD016D870282BF79010000FFFF0200000070FFFF906CFFFF0000FFFF0200000073FFFF8ABFFFFF0000FFFF04000000B9870182ADA00182CD4C0182E70E0100FFFF0400000082744C02826D87026AA002826E79020000FFFF04000000BF7901826D870282A9A80182B78B0100FFFF0400000082CF1702826D8702C46C0182A2C4010000FFFF02000000D6FFFF8AEDFFFF0000FFFF04000000ABA401826D870282BD7E0182DC380100FFFF05000000826C91024100EF026D8702826AA002826E79020000FFFF00000082B78B0182C66601815BD201A9A801815BD2010000FFFF03000000BF7901815BD20182C666010000FFFF00000082AAE600826D870282B6AE00E220010000FFFF000000006D736700080000020000009675FFFF8B4EFFFF00FFFF030000008B43FFFF88E1FFFFA2C4010000FFFF040000008B43FFFFBF790182AAE60082A2C4010000FFFF00000082ABA40182BF790182AAE600A2C4010000FFFF030000008B43FFFFB6FFFF82A2C4010000FFFF04000000B8FFFF905FFFFF9496FFFF8EE3FFFF00FFFF04000000926DFFFF8C62FFFF78FFFF82EA02010000FFFF05000000BF790182A6B50182A8AE0182ADA001EA02010000FFFF0200000096DAFFFFC3FFFF0000FFFF0200000096BEFFFFD3FFFF0000FFFF05000000A700CB01ABA40182DF2B0182ADA00182E70E0100FFFF0400000082EBFC00826D8702CF170282E80A010000FFFF020000009AFFFF9561FFFF0000FFFF03000000E70E0182A2C4019561FFFF0000FFFF0000009492FFFF9273FFFF0000FFFF000000E6EAFFFF0000FFFF03000000BF7901826D870282CE4D000000FFFF00000098FBFFFF0000FFFF02000000FBFFFF9943FFFF0000FFFF020000006AFFFF8E45FFFF0000FFFF02000000EEFFFF9048FFFF0000FFFF02000000CBFFFF96AFFFFF0000FFFF0200000071FFFF91BDFFFF0000FFFF02000000F1FFFF906CFFFF0000FFFF0300000056FFFF95BDFFFF96AFFFFF0000FFFF00000093C1FFFF8EEAFFFF9594FFFF8EFFFF0000FFFF0500000082CE4D00A9A80182BF790182E61201826D870200FFFF0400000082BF790182E612016D87028CF6FFFF0000FFFF05000000C6660182E9060182B197019597FFFF43FFFF0000FFFF0400000082C66601E9060182B197018FECFFFF0000FFFF00000096D1FFFF9382FFFF0000FFFF0000008D95FFFF826D870282DA180000FFFF040000009573FFFF89C2FFFF47FFFF96AFFFFF0000FFFF0200000084FFFF8F97FFFF0000FFFF02000000FAFFFF9484FFFF0000FFFF02000000ADFFFF8AADFFFF0000FFFF04000000E2200182E80A0182DC3801826D870200FFFF0300000082DC3801826D8702B197010000FFFF0300000082A8AE01DF2B0182B197010000FFFF03000000BF7901826D870282DBF3010000FFFF00000082BF7901826D870282DA180000FFFF0400000082B9870182C27201ADA00182B78B010000FFFF03000000005C028264C702827730020000FFFF0000009484FFFF8F74FFFF0000FFFF0000009483FFFF8F74FFFF0000FFFF00000082EA020182A2C40182D5050200FFFF0400000082B78B0182D8FC01E9060182DC38010000FFFF05000000ADA00182E80A0182C6660182E80A01B78B010000FFFF0500000082D34401A6B50182E70E0182BF790182A8AE0100FFFF0300000082B4B700815BD201CBFFFF0000FFFF0400000082B4B7005BD20182DF2B01826D87020000FFFF00000082B98701826D870282B8A500E80A010000FFFF0400000082A8AE01C8620182C95D01815BD2010000FFFF0000008265BD0282744C024300D8026AA0020000FFFF0400000082D3440100CB0182C2720182ADA0010000FFFF00000082BF790182E22001826D8702B1970182EBFC000000FFFF0500000000E0028268AE02827353024300D80267B5020000FFFF040000004300D802744C02826D8702827353020000FFFF000000826F70028264C702826D870268AE0224005C020000FFFF03000000D8FC0182C95D0182B78B010000FFFF000000827544024100EF028266C20268AE02826D87024100EF020000FFFF00000082CE4D0082ACDD0082C8620100FFFF0400000082D46700A700CB01ACDD0082C862010000FFFF050000006F700282744C0224005C0224005C027828020000FFFF0200000090B8FFFF74FFFF0000FFFF0400000082BF79016D870282DBF30182B197010000FFFF00000088FAFFFF9790FFFF0000FFFF0000009641FFFF9550FFFF0000FFFF0000008B54FFFF93AAFFFF0000FFFF00000093F7FFFF965FFFFF0000FFFF000000956EFFFF93FBFFFF0000FFFF00000082BE800082C2720182BF7901EDF70082A2C40182D344010000FFFF0000009181FFFF9852FFFF0000FFFF00000082A2C40182DC380182E70E01BF790182A8AE010000FFFF05000000A8AE0182C8620182C95D0182B78B01C666010000FFFF0200000088A4FFFF74FFFF0000FFFF020000008AE7FFFFCBFFFF0000FFFF020000009790FFFFF0FFFF0000FFFF02000000E3E8FFFFE5FFFF0000FFFF0200000093F7FFFFD9FFFF0000FFFF0300000090ABFFFFB4FFFF91D1FFFF0000FFFF05000000A8AE01815BD20182AAE60082B8A500DE30010000FFFF020000008EA9FFFFD4FFFF0000FFFF0200000093FBFFFFD6FFFF0000FFFF020000009278FFFF52FFFF0000FFFF0200000097BDFFFF4AFFFF0000FFFF0200000096B2FFFFB8FFFF0000FFFF010000008FA8FFFF00FFFF020000008FA9FFFF9577FFFF00FFFF020000008F97FFFF8941FFFF00FFFF020000008EE8FFFF88FAFFFF00FFFF020000008987FFFF8CF0FFFF00FFFF0400000082E90601826D8702D8FC01826D87020000FFFF0200000041FFFF9594FFFF0000FFFF0200000041FFFF904FFFFF0000FFFF02000000FAFFFF8D73FFFF0000FFFF020000006AFFFF8DAAFFFF0000FFFF03000000BF7901826D870296D1FFFF0000FFFF00000093B6FFFF92E5FFFF0000FFFF00000095EFFFFF8C73FFFF0000FFFF00000093FBFFFF8EF1FFFF0000FFFF000000E453FFFF0000FFFF02000000C2FFFF8AADFFFF0000FFFF02000000ECFFFF8AADFFFF0000FFFF00000000104E200031736B30302445532E537457795159485A70547677724162734E6D685661334F4F6576467733617364325A3430632D00CA1100014E200000FD8D2F000000003DDFCC945DD4AD5CDDCA90020000000A00000004080A030902080507");
byte[] packetData = Util.FromHexString(
"444C54534B45595349474E3A313030007465737400736B3030242E546676664B2E487646356E6C317436624338555843536352766538524249704D4D4E395537524C786A6B2D0000");
byte[] packetData1 = Enc(packetData, (byte) (995118 & 0xFF));
Console.WriteLine("Enc " + Util.ToHexString(packetData1, ' '));
byte[] packetData2 = Encrypt(packetData, 2,
out ushort combinedCheck, out ushort check0,
out ushort check1, out ushort check2);
Console.WriteLine("Enc " + Util.ToHexString(packetData2, ' '));
// packetData = Decrypt(packetData);
// Console.WriteLine("Dec " + Util.ToHexString(packetData, ' '));
// Console.WriteLine("Dec " + Util.ToAsciiString(packetData, true));
}
private byte[] Decrypt(byte[] input, uint key, out ushort cc, out ushort c0, out ushort c1, out ushort c2)
{
uint size = (uint) input.Length;
byte[] output = new byte[size];
byte unkCryptkeyRotArg = (byte) ((key >> 1) % 999983);
uint unkDerivedCryptkeyRot = (uint) (size * (unkCryptkeyRotArg + 1));
byte sharedBufIdx = 1;
uint accumulator0 = 0;
uint accumulator1 = 0;
uint accumulator2 = 0;
for (int i = 0; i < size; i++)
{
// Do the decryption for this iteration
byte oldSharedBufIdx = sharedBufIdx;
byte tIdx = (byte) (input[i] ^ SharedCryptKey[sharedBufIdx]);
uint decKeyByte = DecryptKey[tIdx];
sharedBufIdx = (byte) ((unkDerivedCryptkeyRot >> 10) ^ decKeyByte);
// Update the checksum accumulators.
accumulator0 = (uint) (accumulator0 + (tIdx << (i & 7)));
accumulator1 = accumulator1 + decKeyByte;
accumulator2 = (uint) (accumulator2 + (oldSharedBufIdx * sharedBufIdx));
// Append the output.
output[i] = sharedBufIdx;
// Update the key pos for next iteration.
unkDerivedCryptkeyRot = 0x4FD * (unkDerivedCryptkeyRot + 1);
}
ushort combinedCheck = (ushort) (accumulator1 + (accumulator0 >> 1) + (accumulator2 >> 2));
ushort check0 = (ushort) (accumulator0 ^ (accumulator0 >> 16));
ushort check1 = (ushort) (accumulator1 ^ (accumulator1 >> 16));
ushort check2 = (ushort) (accumulator2 ^ (accumulator2 >> 16));
cc = combinedCheck;
c0 = check0;
c1 = check1;
c2 = check2;
return output;
}
private byte[] Encrypt(byte[] input, uint key, out ushort cc, out ushort c0, out ushort c1, out ushort c2)
{
uint size = (uint) input.Length;
byte[] output = new byte[size];
byte unkCryptkeyRotArg = (byte) ((key >> 1) % 999983);
uint unkDerivedCryptkeyRot = (uint) (size * (unkCryptkeyRotArg + 1));
byte sharedBufIdx = 1;
uint accumulator0 = 0;
uint accumulator1 = 0;
uint accumulator2 = 0;
for (int i = 0; i < size; i++)
{
// Do the encryption for this iteration
byte encKeyIdx = (byte) ((unkDerivedCryptkeyRot >> 10) ^ input[i]);
unkDerivedCryptkeyRot = 0x4FD * (unkDerivedCryptkeyRot + 1);
byte encKeyByte = EncryptKey[encKeyIdx];
// Update the checksum accumulators.
accumulator2 = (uint) (accumulator2 + (sharedBufIdx * input[i]));
accumulator1 = accumulator1 + encKeyIdx;
accumulator0 = (uint) (accumulator0 + (encKeyByte << (i & 7)));
// Append the output.
output[i] = (byte) (SharedCryptKey[sharedBufIdx] ^ encKeyByte);
// Update the shared_buf_idx for the next iteration.
sharedBufIdx = input[i];
}
ushort combinedCheck = (ushort) (accumulator1 + (accumulator0 >> 1) + (accumulator2 >> 2));
ushort check0 = (ushort) (accumulator0 ^ (accumulator0 >> 16));
ushort check1 = (ushort) (accumulator1 ^ (accumulator1 >> 16));
ushort check2 = (ushort) (accumulator2 ^ (accumulator2 >> 16));
cc = combinedCheck;
c0 = check0;
c1 = check1;
c2 = check2;
return output;
}
byte[] Enc(byte[] input, byte a5)
{
byte v12;
uint v13;
uint v14;
uint inputSize = (uint) input.Length;
byte outputIndex = 0;
byte inputIndex = 0;
uint v5 = inputSize * (a5 + 1u);
uint v6 = 0;
uint v7 = 0;
uint v8 = 0;
uint v9 = 0;
byte v10 = 1;
uint v19 = 0;
uint v18 = 0;
uint v17 = 0;
byte[] output = new byte[inputSize];
if (inputSize > 0)
{
do
{
v12 = input[inputIndex];
v19 += (uint) v10 * input[inputIndex];
v13 = (byte) ((v5 >> 10) ^ input[inputIndex]);
v18 += v13;
v13 = ReplaceByte(0, v13, EncryptKey[v13]);
v14 = (uint) ((byte) v13 << ((int) v9 & 7));
v9++;
v5 = 1277 * (v5 + 1);
v17 += v14;
output[outputIndex] = (byte) (SharedCryptKey[v10] ^ v13);
inputIndex = (byte) (outputIndex + 1);
v10 = v12;
++outputIndex;
} while (v9 < inputSize);
v6 = v19;
v7 = v18;
v8 = v17;
}
uint a6 = v6 ^ (v6 >> 16);
uint a4 = v7 ^ (v7 >> 16);
uint result = (uint) (v7 + ((int) v8 >> 1) + ((int) v6 >> 2));
uint a2 = v8 ^ (v8 >> 16);
ushort check3 = (ushort) a6;
ushort check2 = (ushort) a4;
ushort check1 = (ushort) a2;
ushort res = (ushort) result;
byte resB = (byte) result;
return output;
}
uint ReplaceByte(uint index, uint value, byte replaceByte)
{
var shiftBits = 8 * index;
var mask = ~(0xff << (int) shiftBits);
return (uint) (value & mask | (replaceByte << (int) shiftBits));
}
}
}