Exemple #1
0
        protected override void Encode(IChannelHandlerContext context, IPacket message, IByteBuffer output)
        {
            var socket  = context.Channel.GetAttribute(AbstractSocket.SocketKey).Get();
            var dataLen = (short)message.Length;
            var buffer  = new byte[dataLen];

            Array.Copy(message.Buffer, buffer, dataLen);

            if (socket != null)
            {
                var seqSend = socket.SeqSend;
                var rawSeq  = (short)((seqSend >> 16) ^ -(95 + 1));

                if (socket.EncryptData)
                {
                    dataLen ^= rawSeq;

                    ShandaCipher.EncryptTransform(buffer);
                    AESCipher.Transform(buffer, seqSend);
                }

                output.WriteShortLE(rawSeq);
                output.WriteShortLE(dataLen);
                output.WriteBytes(buffer);

                socket.SeqSend = IGCipher.InnoHash(seqSend, 4, 0);
            }
            else
            {
                output.WriteShortLE(dataLen);
                output.WriteBytes(buffer);
            }
        }
Exemple #2
0
        protected override void Encode(IChannelHandlerContext context, IPacket message, IByteBuffer output)
        {
            var socket = context.Channel.GetAttribute(NettyAttributes.SocketKey).Get();
            var dataLen = (short)message.Buffer.Length;
            var buffer = message.Buffer.ToArray();

            if (socket != null)
            {
                var seqSend = socket.SeqSend;
                var rawSeq = (short)((seqSend >> 16) ^ -(_transport.Version + 1));

                if (socket.EncryptData)
                {
                    dataLen ^= rawSeq;

                    ShandaCipher.EncryptTransform(buffer);
                    _aesCipher.Transform(buffer, seqSend);
                }

                output.WriteShortLE(rawSeq);
                output.WriteShortLE(dataLen);
                output.WriteBytes(buffer);

                socket.SeqSend = _igCipher.Hash(seqSend, 4, 0);
            }
            else
            {
                output.WriteShortLE(dataLen);
                output.WriteBytes(buffer);
            }
        }
        public void TransformResultIsAccurate()
        {
            const int  data = 0xABCDEF;
            const uint key  = 0x100;

            var result = AESCipher.Transform(BitConverter.GetBytes(data), key);

            Assert.Equal(0x5883F451, BitConverter.ToInt32(result));
            Assert.NotEqual(data, BitConverter.ToInt32(result));
        }
        protected override void Encode(IChannelHandlerContext context, Packet message, IByteBuffer output)
        {
            var socket = context.Channel.GetAttribute(Socket.SocketKey).Get();

            if (socket != null)
            {
                lock (socket.LockSend)
                {
                    var seqSend = socket.SeqSend;
                    var rawSeq  = (short)((seqSend >> 16) ^ -(AESCipher.Version + 1));
                    var dataLen = (short)message.Length;
                    var buffer  = new byte[dataLen];

                    Unpooled.CopiedBuffer(message.Buffer).ReadBytes(buffer);

                    if (socket.EncryptData)
                    {
                        dataLen ^= rawSeq;

                        buffer = ShandaCipher.EncryptTransform(buffer);
                        buffer = AESCipher.Transform(buffer, seqSend);
                    }

                    output.WriteShortLE(rawSeq);
                    output.WriteShortLE(dataLen);
                    output.WriteBytes(buffer);

                    socket.SeqSend = IGCipher.InnoHash(seqSend, 4, 0);
                }
            }
            else
            {
                var length = message.Length;
                var buffer = new byte[length];

                message.Buffer.ReadBytes(buffer);
                output.WriteShortLE(length);
                output.WriteBytes(buffer);
            }
        }
Exemple #5
0
        protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output)
        {
            var socket = context.Channel.GetAttribute(Socket.SocketKey).Get();

            try
            {
                switch (State)
                {
                case PacketState.Header:
                    if (socket != null)
                    {
                        var sequence = input.ReadShortLE();
                        var length   = input.ReadShortLE();

                        if (socket.EncryptData)
                        {
                            length ^= sequence;
                        }

                        _sequence = sequence;
                        _length   = length;
                    }
                    else
                    {
                        _length = input.ReadShortLE();
                    }

                    Checkpoint(PacketState.Payload);
                    return;

                case PacketState.Payload:
                    var buffer = new byte[_length];

                    input.ReadBytes(buffer);
                    Checkpoint(PacketState.Header);

                    if (_length < 0x2)
                    {
                        return;
                    }
                    if (_length > 0x10000)
                    {
                        return;
                    }

                    if (socket != null)
                    {
                        lock (socket.LockRecv)
                        {
                            var seqRecv = socket.SeqRecv;
                            var version = (short)(seqRecv >> 16) ^ _sequence;

                            if (!(version == -(AESCipher.Version + 1) ||
                                  version == AESCipher.Version))
                            {
                                return;
                            }

                            if (socket.EncryptData)
                            {
                                buffer = AESCipher.Transform(buffer, seqRecv);
                                buffer = ShandaCipher.DecryptTransform(buffer);
                            }

                            socket.SeqRecv = IGCipher.InnoHash(seqRecv, 4, 0);
                        }
                    }

                    output.Add(new InPacket(Unpooled.CopiedBuffer(buffer)));
                    return;
                }
            }
            catch (IndexOutOfRangeException)
            {
                RequestReplay();
            }
        }
Exemple #6
0
        protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output)
        {
            var socket = context.Channel.GetAttribute(NettyAttributes.SocketKey).Get();

            switch (State)
            {
            case NettyPacketState.DecodingHeader:
                if (socket != null)
                {
                    if (input.ReadableBytes < 4)
                    {
                        RequestReplay();
                        return;
                    }

                    var sequence = input.ReadShortLE();
                    var length   = input.ReadShortLE();

                    if (socket.EncryptData)
                    {
                        length ^= sequence;
                    }

                    _sequence = sequence;
                    _length   = length;
                }
                else
                {
                    if (input.ReadableBytes < 2)
                    {
                        RequestReplay();
                        return;
                    }

                    _length = input.ReadShortLE();
                }

                Checkpoint(NettyPacketState.DecodingPayload);
                return;

            case NettyPacketState.DecodingPayload:
                if (input.ReadableBytes < _length)
                {
                    RequestReplay();
                    return;
                }

                var buffer = new byte[_length];

                input.ReadBytes(buffer);
                Checkpoint(NettyPacketState.DecodingHeader);

                if (_length < 0x2)
                {
                    return;
                }

                if (socket != null)
                {
                    var seqRecv = socket.SeqRecv;
                    var version = (short)(seqRecv >> 16) ^ _sequence;

                    if (!(version == -(_transport.Version + 1) ||
                          version == _transport.Version))
                    {
                        return;
                    }

                    if (socket.EncryptData)
                    {
                        _aesCipher.Transform(buffer, seqRecv);
                        ShandaCipher.DecryptTransform(buffer);
                    }

                    socket.SeqRecv = _igCipher.Hash(seqRecv, 4, 0);
                }

                output.Add(new UnstructuredIncomingPacket(buffer));
                return;
            }
        }