Guest User

PB Packet.cs

a guest
May 9th, 2015
614
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Linq;
  6.  
  7.  
  8. class Packet
  9. {
  10.     private UInt16 header;
  11.     private UInt16 length;
  12.     private byte[] data;
  13.  
  14.     public Packet(byte[] data)
  15.     {
  16.         this.data = data;
  17.     }
  18.  
  19.     public void Parse()
  20.     {
  21.         Console.WriteLine("Raw Data: " + TcpEchoServer.nicePacketString(this.data));
  22.         bool encrypted = false;
  23.  
  24.         if (this.data.Length < 4)
  25.             return;
  26.  
  27.         if (BitConverter.IsLittleEndian)
  28.         {
  29.             this.length = BitConverter.ToUInt16(data, 0);
  30.         }
  31.         else
  32.             throw new NotImplementedException();
  33.  
  34.         if (this.length > 8908)
  35.         {
  36.             this.length = Convert.ToUInt16(this.length & 0x7FFF);
  37.             encrypted = true;
  38.         }
  39.  
  40.         if(this.data.Length - 2 < this.length + 2)
  41.         {
  42.             Console.WriteLine("Invalid Length:" + (this.data.Length - 2) + "/" + (this.length + 2));
  43.             return;
  44.         }
  45.  
  46.         byte[] content = this.data.Skip(2).Take(this.length).ToArray();
  47.         if (encrypted)
  48.         {
  49.             content = this.Decrypt(content);
  50.         }
  51.  
  52.         this.Read(content);
  53.  
  54.     }
  55.     public byte[] Decrypt(byte[] data)
  56.     {
  57.         //Console.WriteLine("Decrypting Data (Length: " + this.length + ")");
  58.  
  59.         byte lastElement = data[data.Length - 1];
  60.         for (int i = data.Length - 1; i > 0; i--)
  61.         {
  62.             data[i] = (byte)(((data[i - 1] & 0xFF) << (8 - GetShift())) | ((data[i] & 0xFF) >> GetShift()));
  63.         }
  64.         data[0] = (byte)((lastElement << (8 - GetShift())) | ((data[0] & 0xFF) >> GetShift()));
  65.  
  66.         return data;
  67.     }
  68.  
  69.     public void Read(byte[] data)
  70.     {
  71.         this.header = BitConverter.ToUInt16(data.Take(2).ToArray(), 0);
  72.         Console.WriteLine("ID: " + this.header + " => " + TcpEchoServer.nicePacketString(data));
  73.         TcpEchoServer.writePacketToFile(data);
  74.     }
  75.  
  76.     private int GetShift()
  77.     {
  78.         return ((29891) % 7) + 1;
  79.     }
  80. }
RAW Paste Data