Ejemplo n.º 1
0
 public static void Initialize(byte[] key)
 {
     _instance = new PacketEncrypt(key);
 }
Ejemplo n.º 2
0
        public int Decode(MemoryStream rawPacket, out short serial, List <object> messages)
        {
            using (var decPacket = new MemoryStream())
                using (var posPacket = new MemoryStream())
                {
                    //var type = rawPacket[0];
                    var    type = (byte)rawPacket.ReadByte();
                    ushort size = 0;
                    serial = -1;

                    if (type == 0xC1 || type == 0xC3)
                    {
                        size = (ushort)rawPacket.ReadByte();
                    }
                    else if (type == 0xC2 || type == 0xC4)
                    {
                        size = (ushort)((rawPacket.ReadByte() << 8) | rawPacket.ReadByte());
                    }
                    else
                    {
                        throw new InvalidProgramException($"Invalid packet type {type:X2}");
                    }

                    // Packet Encrypted
                    byte[] tmp;
                    byte[] dec;
                    switch (type)
                    {
                    case 0xC1:
                        decPacket.WriteByte(type);
                        decPacket.WriteByte((byte)size);
                        tmp = new byte[size - 2];
                        rawPacket.Read(tmp, 0, tmp.Length);
                        decPacket.Write(tmp, 0, tmp.Length);
                        break;

                    case 0xC2:
                        decPacket.WriteByte(type);
                        decPacket.WriteByte((byte)((size >> 8) & 0xff));
                        decPacket.WriteByte((byte)(size & 0xff));
                        tmp = new byte[size - 3];
                        rawPacket.Read(tmp, 0, tmp.Length);
                        decPacket.Write(tmp, 0, tmp.Length);
                        break;

                    case 0xC3:
                        tmp = new byte[size - 2];
                        rawPacket.Read(tmp, 0, tmp.Length);

                        if (_packetRijndael)
                        {
                            dec = PacketEncrypt.Decrypt(tmp);
                            if (dec.Length == 0)
                            {
                                return(size);
                            }

                            decPacket.WriteByte(0xC1);
                            decPacket.WriteByte((byte)(dec.Length + 2));
                            decPacket.Write(dec, 0, dec.Length);
                        }
                        else
                        {
                            dec    = SimpleModulus.Decoder(tmp);
                            serial = dec[0];
                            decPacket.WriteByte(0xC1);
                            decPacket.WriteByte((byte)(dec.Length + 1));
                            decPacket.Write(dec, 1, dec.Length - 1);
                        }
                        break;

                    case 0xC4:
                        tmp = new byte[size - 3];
                        rawPacket.Read(tmp, 0, tmp.Length);

                        if (_packetRijndael)
                        {
                            dec = PacketEncrypt.Decrypt(tmp);
                            if (dec.Length == 0)
                            {
                                return(size);
                            }

                            decPacket.WriteByte(0xC2);
                            decPacket.WriteByte((byte)((dec.Length + 3) >> 8));
                            decPacket.WriteByte((byte)((dec.Length + 3) & 255));
                            decPacket.Write(dec, 0, dec.Length);
                        }
                        else
                        {
                            dec    = SimpleModulus.Decoder(tmp);
                            serial = dec[0];
                            decPacket.WriteByte(0xC2);
                            decPacket.WriteByte((byte)((dec.Length + 2) >> 8));
                            decPacket.WriteByte((byte)((dec.Length + 2) & 255));
                            decPacket.Write(dec, 1, dec.Length - 1);
                        }
                        break;
                    }

                    using (var spe = new StreamPacketEngine(_packetRijndael))
                    {
                        spe.AddData(decPacket.ToArray());
                        var posProcess = spe.ExtractPacket();
                        posPacket.Write(posProcess, 0, posProcess.Length);
                    }

                    posPacket.Seek(0, SeekOrigin.Begin);

                    ushort opCode;
                    ushort pkSize;

                    if (type == 0xC1 || type == 0xC3)
                    {
                        if (posPacket.Length == 3)
                        {
                            posPacket.Seek(0, SeekOrigin.End);
                            posPacket.WriteByte(0);
                            posPacket.Seek(0, SeekOrigin.Begin);
                        }
                        else if (posPacket.Length < 3)
                        {
                            throw new Exception("Invalid Packet " + type.ToString("X2") + " size " + posPacket.Length + " -");
                        }

                        var tmph = Serializer.Deserialize <WZBPacket>(posPacket);
                        opCode = tmph.Operation;
                        pkSize = tmph.Size;
                    }
                    else
                    {
                        if (posPacket.Length == 4)
                        {
                            posPacket.Seek(0, SeekOrigin.End);
                            posPacket.WriteByte(0);
                            posPacket.Seek(0, SeekOrigin.Begin);
                        }
                        else if (posPacket.Length < 4)
                        {
                            throw new Exception("Invalid Packet " + type.ToString("X2") + " size " + posPacket.Length + " -");
                        }

                        var tmph = Serializer.Deserialize <WZWPacket>(posPacket);
                        opCode = tmph.Operation;
                        pkSize = tmph.Size;
                    }

                    //posPacket.Seek(0, SeekOrigin.Begin);

                    var factory = _factories.FirstOrDefault(f => f.ContainsOpCode(opCode));
                    try
                    {
                        if (factory != null)
                        {
                            messages.Add(factory.GetMessage(opCode, posPacket));
                        }
                        else
                        {
                            var orgOpCode = opCode;
                            opCode |= 0xFF00;
                            factory = _factories.FirstOrDefault(f => f.ContainsOpCode(opCode));
                            if (factory != null)
                            {
                                posPacket.Position--;
                                messages.Add(factory.GetMessage(opCode, posPacket));
                            }
                            else
                            {
                                Logger.Error("Invalid OpCoder {opCodea:X4}|{opCode:X2}", orgOpCode, opCode & 0xff);
                            }
                        }
                    }catch (Exception ex)
                    {
                        throw new Exception("Factory: " + factory, ex);
                    }

                    return(size);
                }
        }
Ejemplo n.º 3
0
        public byte[] Encode(object message, ref short serial)
        {
            var factory = _factories.First(f => f.ContainsType(message.GetType()));

            ushort opCode = factory.GetOpCode(message.GetType());

            var att = message
                      .GetType()
                      .GetCustomAttributes(false)
                      .Where(x => x.GetType() == typeof(WZContractAttribute))
                      .FirstOrDefault() as WZContractAttribute;

            if (att == null)
            {
                throw new InvalidOperationException("Invalid message format");
            }

            byte[] res;

            using (var data = new MemoryStream())
            {
                var opCodeSize = (opCode & 0xFF00) == 0xFF00 ? 1 : 2;
                if (att.LongMessage)
                {
                    Serializer.Serialize(data, new WZWPacket(0xC2, (ushort)data.Length, opCode));
                }
                else
                {
                    Serializer.Serialize(data, new WZBPacket(0xC1, (byte)data.Length, opCode));
                }

                try
                {
                    data.Position = (att.LongMessage ? 3 : 2) + opCodeSize;
                    Serializer.Serialize(data, message);
                }
                catch (Exception e)
                {
                    Logger.Error(e, "");
                }

                if (att.Serialized)
                {
                    data.Position = (att.LongMessage ? 3 : 2);
                    if (_packetRijndael == true)
                    {
                        PacketEncrypt.Encrypt(data, data);
                    }
                    else
                    {
                        SimpleModulus.Encrypt(data, (byte)serial, data);
                    }

                    serial++;
                    data.Position = 0;
                    data.WriteByte((byte)(att.LongMessage ? 0xC4 : 0xC3));
                }

                data.Position = 1;
                if (att.LongMessage)
                {
                    data.Write(BitConverter.GetBytes(((ushort)data.Length).ShufleEnding()), 0, 2);
                }
                else
                {
                    data.Write(BitConverter.GetBytes((byte)data.Length), 0, 1);
                }
                res = data.ToArray();
                //if(!att.Serialized)
                return(res);
            }

            /*byte[] result;
            *  using (var h = new MemoryStream())
            *  using (var b = new MemoryStream())
            *  {
            *   try
            *   {
            *       Serializer.Serialize(b, message);
            *   }
            *   catch (Exception e)
            *   {
            *       Logger.Error(e, "");
            *   }
            *   var body = b.ToArray();
            *   var length = body.Length;
            *   var sizeFix = (((opCode & 0xFF00) == 0xFF00) ? 1 : 0);
            *   var sizeFix2 = 0;
            *
            *   if (att.LongMessage)
            *   {
            *       var header = new WZWPacket
            *       {
            *           Type = (byte)0xC2,
            *           Size = (ushort)(length + 5 - sizeFix),
            *           Operation = opCode
            *       };
            *       Serializer.Serialize(h, header);
            *
            *       sizeFix2 = 3;
            *   }
            *   else
            *   {
            *       var header = new WZBPacket
            *       {
            *           Type = (byte)0xC1,
            *           Size = (byte)(length + 4 - sizeFix),
            *           Operation = opCode
            *       };
            *       Serializer.Serialize(h, header);
            *
            *       sizeFix2 = 2;
            *   }
            *
            *   var head = h.ToArray();
            *   var headLen = head.Length - sizeFix;
            *   result = new byte[headLen + body.Length];
            *   Array.Copy(head, result, headLen);
            *   Array.Copy(body, 0, result, headLen, body.Length);
            *
            *   if (att.Serialized)
            *   {
            *       result[0] += 2;
            *
            *       var temp = new byte[result.Length - sizeFix2];
            *       Array.Copy(result, sizeFix2, temp, 0, temp.Length);
            *
            *       //temp[0] = (byte)serial;
            *
            *       byte[] enc;// = SimpleModulus.Encoder(temp);
            *       using (var ms = new MemoryStream())
            *       {
            *           if (_packetRijndael == true)
            *               PacketEncrypt.Encrypt(ms, new MemoryStream(temp));
            *           else
            *               SimpleModulus.Encrypt(ms, (byte)serial, new MemoryStream(temp));
            *
            *           enc = new byte[ms.Length];
            *           ms.Seek(0, SeekOrigin.Begin);
            *           ms.Read(enc, 0, (int)ms.Length);
            *       }
            *
            *       var resultTemp = new byte[sizeFix2 + enc.Length + 1];
            *       Array.Copy(result, resultTemp, sizeFix2 + 1);
            *       if (resultTemp[0] == 0xC3)
            *       {
            *           resultTemp[1] = (byte)(resultTemp.Length);
            *       }
            *       else
            *       {
            *           resultTemp[1] = (byte)(resultTemp.Length >> 8);
            *           resultTemp[2] = (byte)(resultTemp.Length & 0xff);
            *       }
            *       Array.Copy(enc, 0, resultTemp, sizeFix2 + 1, enc.Length);
            *       serial++;
            *
            *       Logger.Debug("\nA:{0}\nB{1}", result.GetHex(), res.GetHex());
            *       return res;
            *   }
            *  }
            *
            *  return result;*/
        }
Ejemplo n.º 4
0
        public byte[] Encode(object message, ref short serial)
        {
            MessageFactory factory = null;

            try
            {
                factory = _factories.First(f => f.ContainsType(message.GetType()));
            }catch (Exception ex)
            {
                throw new Exception("Unregisted message " + message.GetType().ToString(), ex);
            }

            ushort opCode = factory.GetOpCode(message.GetType());

            var att = message
                      .GetType()
                      .GetCustomAttributes(false)
                      .Where(x => x.GetType() == typeof(WZContractAttribute))
                      .FirstOrDefault() as WZContractAttribute;

            if (att == null)
            {
                throw new InvalidOperationException("Invalid message format");
            }

            byte[] res;

            using (var data = new MemoryStream())
            {
                var opCodeSize = (opCode & 0xFF00) == 0xFF00 ? 1 : 2;
                if (att.LongMessage)
                {
                    Serializer.Serialize(data, new WZWPacket(0xC2, (ushort)data.Length, opCode));
                }
                else
                {
                    Serializer.Serialize(data, new WZBPacket(0xC1, (byte)data.Length, opCode));
                }

                try
                {
                    data.Position = (att.LongMessage ? 3 : 2) + opCodeSize;
                    Serializer.Serialize(data, message);
                }
                catch (Exception e)
                {
                    Logger.Error(e, "");
                }

                if (att.Serialized)
                {
                    data.Position = (att.LongMessage ? 3 : 2);
                    if (_packetRijndael == true)
                    {
                        PacketEncrypt.Encrypt(data, data);
                        //PacketPrint(data);
                    }
                    else
                    {
                        SimpleModulus.Encrypt(data, (byte)serial, data);
                    }

                    serial++;
                    data.Position = 0;
                    data.WriteByte((byte)(att.LongMessage ? 0xC4 : 0xC3));
                }

                data.Position = 1;
                if (att.LongMessage)
                {
                    data.Write(BitConverter.GetBytes(((ushort)data.Length).ShufleEnding()), 0, 2);
                }
                else
                {
                    data.Write(BitConverter.GetBytes((byte)data.Length), 0, 1);
                }
                res = data.ToArray();

                //if(!att.Serialized)
                return(res);
            }
        }