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); } }
public void HashResultIsAccurate() { const uint key = 0x100; var result = IGCipher.InnoHash(key, 4, 0); Assert.Equal((uint)0x2B1F7777, result); Assert.NotEqual(key, 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); } }
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(); } }