Ejemplo n.º 1
0
        public PacketOut Crypt(PacketOut packet)
        {
            if (m_crypts.Count <= 0)
            {
                return(packet);
            }

            byte[] Packet = packet.ToArray();

            Log.Tcp("SendTCP", Packet, 0, (int)Packet.Length);

            int Hpos = 0;

            Hpos += PacketOut.SizeLen;
            if (PacketOut.OpcodeInLen)
            {
                Hpos += packet.OpcodeLen;
            }

            byte[] Header  = new byte[Hpos];
            byte[] ToCrypt = new byte[(packet.Length - Hpos)];

            for (int i = 0; i < Hpos; ++i)
            {
                Header[i] = Packet[i];
            }

            for (int i = Hpos; i < Packet.Length; ++i)
            {
                ToCrypt[i - Hpos] = Packet[i];
            }

            try
            {
                foreach (KeyValuePair <ICryptHandler, CryptKey[]> Entry in m_crypts)
                {
                    ToCrypt = Entry.Key.Crypt(Entry.Value[0], ToCrypt);
                }
            }
            catch (Exception e)
            {
                Log.Error("BaseClient", "Crypt Error : " + e.ToString());
                return(packet);
            }

            PacketOut Out = new PacketOut((byte)0);

            Out.Opcode    = packet.Opcode;
            Out.OpcodeLen = packet.OpcodeLen;
            Out.Position  = 0;
            Out.SetLength(0);

            byte[] Total = new byte[Header.Length + ToCrypt.Length];

            for (int i = 0; i < Total.Length; ++i)
            {
                if (i < Header.Length)
                {
                    Total[i] = Header[i];
                }
                else
                {
                    Total[i] = ToCrypt[i - Header.Length];
                }
            }

            Out.Write(Total, 0, Total.Length);

            return(Out);
        }