//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 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); }