//public Packet(byte[] data) // : this() //{ // ParseData(ref data); //} public Packet(BinaryReader reader, PacketEncryptor packetEncryptor) : this() { var header = reader.ReadBytes(HEADER_SIZE); var packetBody = default(byte[]); var xorKey = new byte[4]; Array.Copy(header, xorKey, xorKey.Length); XorBytes(xorKey, ref header); var encrypted = false; var packetType = PacketType.Request; using (var headerStream = new MemoryStream(header)) using (var headerReader = new BinaryReader(headerStream)) { // Move to the encryption flags headerReader.BaseStream.Seek(ENC_LENGTH, SeekOrigin.Begin); var encFlags = headerReader.ReadDword(); var bytesToRead = headerReader.ReadDword() - 8; packetType = headerReader.ReadPacketType(); packetBody = reader.ReadBytes((int)bytesToRead); encrypted = encFlags == PacketEncryptor.ENC_AES256; } XorBytes(xorKey, ref packetBody); if (encrypted) { packetBody = packetEncryptor.AesDecrypt(packetBody); } ParseData(packetType, ref packetBody); }
public byte[] ToRaw(byte[] sessionGuid, PacketEncryptor packetEncryptor = null) { var packetData = default(byte[]); packetEncryptor = packetEncryptor ?? PacketEncryptor.Blank; using (var packetStream = new MemoryStream()) using (var writer = new BinaryWriter(packetStream)) { var tlvData = default(byte[]); using (var tlvStream = new MemoryStream()) using (var tlvWriter = new BinaryWriter(tlvStream)) { foreach (var tlv in this.Tlvs.Values.AsEnumerable().Flatten()) { tlv.ToRaw(tlvWriter); } tlvData = packetEncryptor.Encrypt(tlvStream.ToArray()); } // Write a zero XOR key, which gets filled in later. writer.WriteDword(0u); writer.Write(sessionGuid); writer.WriteDword(packetEncryptor.Flags); writer.WriteDword((UInt32)tlvData.Length + 8u); writer.WritePacketType(this.type); writer.Write(tlvData); packetData = packetStream.ToArray(); } var xorKey = GenerateXorKey(); XorBytes(xorKey, ref packetData); return(packetData); }
public Packet(BinaryReader reader, PacketEncryptor packetEncryptor = null) : this() { packetEncryptor = packetEncryptor ?? PacketEncryptor.Blank; var header = reader.ReadBytes(HEADER_SIZE); var packetBody = default(byte[]); var xorKey = new byte[4]; Array.Copy(header, xorKey, xorKey.Length); header.Xor(xorKey); var encrypted = false; var packetType = PacketType.Request; using (var headerStream = new MemoryStream(header)) using (var headerReader = new BinaryReader(headerStream)) { // Move to the encryption flags headerReader.BaseStream.Seek(ENC_LENGTH, SeekOrigin.Begin); var encFlags = headerReader.ReadDword(); var bytesToRead = headerReader.ReadDword() - 8; packetType = headerReader.ReadPacketType(); packetBody = reader.ReadBytes((int)bytesToRead); encrypted = encFlags == PacketEncryptor.ENC_AES256; } packetBody.Xor(xorKey); if (encrypted) { // TODO: if we don't have a packet encryptor, then we should probably // bail out. packetBody = packetEncryptor.AesDecrypt(packetBody); } ParseData(packetType, ref packetBody); }
public byte[] ToRaw(Guid sessionGuid, PacketEncryptor packetEncryptor = null) { return(this.ToRaw(sessionGuid.ToByteArray(), packetEncryptor)); }