Exemple #1
0
        public static void EncDec_DES(byte[] data, int index, int len, bool isSend)
        {
            int offset = 0;

            while (offset <= len - 8)
            {
                bool bEncrypt = false;
                if (isSend)
                {
                    //send cmd ==> encode
                    bEncrypt = DESEncryptorFix.IsNeedEncpypt(true);
                    DESEncryptorFix.IncrementSendData();
                }
                else
                {
                    //recv cmd ==> decode
                    bEncrypt = DESEncryptorFix.IsNeedEncpypt(false);
                    DESEncryptorFix.IncrementRecvData();
                }

                if (bEncrypt)
                {
                    UInt32[] tmp = new UInt32[2];
                    tmp[0] = ReadUInt32(data, index + offset);
                    tmp[1] = ReadUInt32(data, index + offset + 4);

                    DESEncrypt.DES_encrypt1(tmp, isSend);

                    WriteUInt32(data, index + offset, tmp[0]);
                    WriteUInt32(data, index + offset + 4, tmp[1]);
                }

                offset += 8;
            }
        }
Exemple #2
0
        public void Start(string name, string password, ushort zoneID, string[] serverIP, int[] port, uint gameVersion, IGiantFreeClient client)
        {
            DESEncryptorFix.Reset();

            this.client = client;
            this.loginModule.Account = name;
            this.loginModule.EncryptPassWord(password);
            this.loginModule.ZoneID        = zoneID;
            this.loginModule.FirServerIP   = serverIP[0];
            this.loginModule.FirServerPort = port[0];
            this.loginModule.Login();
        }
Exemple #3
0
        public byte[] CompressAndDESEncrypt()
        {
            byte[] buffer = CompressCommandAndFillZero();

            if (null == buffer)
            {
                return(null);
            }

            //if (DESEncrypt.Encrypt(buffer))
            //    return buffer;
            DESEncryptorFix.EncDec_DES(buffer, 0, buffer.Length, true);

            return(buffer);
        }
Exemple #4
0
        public void OnLoginFirSuccess(byte[] data)
        {
            Logger <IGiantFreeServer> .L("登录Fir Server成功");

            stLoginSuccessCmd cmd = (stLoginSuccessCmd)CmdSerializer.BytesToStruct(data, typeof(stLoginSuccessCmd));
            {
                this.loginModule.GateWayIP   = cmd.gameserIP;
                this.loginModule.GateWayPort = cmd.port;
                this.loginModule.UserID      = cmd.dwUserID;
                this.loginModule.UserTmpID   = cmd.loinTmpID;
                Array.Copy(cmd.key, this.loginModule.LoginKey, 256);
                byte index = this.loginModule.LoginKey[58];
                DESEncrypt.ResetDESKey(this.loginModule.LoginKey, index);
                uint desKey = (uint)this.loginModule.LoginKey[index + 2];
                DESEncryptorFix.SetDESEcryptMask(desKey);
                this.loginModule.LoginGameServer();
            }
        }
Exemple #5
0
        private void _ReceiveMessage(IByteArray bytes)
        {
            bytes.Retain();
            this.receiveByteArray.AddLast(bytes);
            this.receiveSize += bytes.Size;
            if (this.receiveSize < 8)
            {
                return;
            }

            var decSize = this.receiveSize & (~7);

            this.receiveSize -= decSize;
            var decBytes = this.storage.Alloc(decSize);
            var node     = this.receiveByteArray.First;

            while (decSize > 0 && null != node)
            {
                var current = node;
                node = node.Next;

                if (current.Value.Size <= decSize)
                {
                    decSize -= current.Value.Size;
                    decBytes.WriteByteArray(current.Value);
                }
                else
                {
                    var tmp = current.Value.ReadByteArray(decSize);
                    this.receiveByteArray.AddLast(current.Value.ReadByteArray());
                    decBytes.WriteByteArray(tmp);
                    tmp.Release();
                    decSize = 0;
                }
                this.receiveByteArray.Remove(current);
                current.Value.Release();
            }

            decBytes.Seek();
            byte[] decryptData = null;
            if (this.loginRecv)
            {
                decryptData = RC5Encrypt.Decrypt(decBytes.ReadBytes());
            }
            else
            {
                decryptData = decBytes.ReadBytes();
                DESEncryptorFix.EncDec_DES(decryptData, 0, decryptData.Length, false);
            }
            decBytes.Release();

            var array = this.storage.Alloc(decryptData.Length);

            array.WriteBytes(decryptData, 0, decryptData.Length);
            array.Seek();
            this.decodeByteArray.AddLast(array);
            this.decodeSize += array.Size;

            //解析
            node = this.decodeByteArray.First;
            while (this.decodeSize > 4 && null != node)
            {
                node = _GetHeaderNode(node);
                var byteArray = node.Value;

                //前两字节代表数据长度
                int bodyLength = byteArray.ReadInt16();
                if (bodyLength + 4 > this.decodeSize)
                {
                    byteArray.Seek(-2, SeekOrigin.CURRENT); // 回滚读取索引位置
                    break;
                }

                //第四字节代表是否压缩
                byteArray.ReadByte();
                int compressFlag = (int)byteArray.ReadByte();

                //有一个完整的数据包
                var commandData = this.storage.Alloc(bodyLength);
                while (commandData.Size != bodyLength)
                {
                    var current = node;
                    if ((current.Value.Size - current.Value.Position) <= (bodyLength - commandData.Size))
                    {
                        commandData.WriteByteArray(current.Value);
                        node = node.Next;
                    }
                    else
                    {
                        var tmp = current.Value.ReadByteArray(bodyLength - commandData.Size);
                        commandData.WriteByteArray(tmp);
                        tmp.Release();
                        node = this.decodeByteArray.AddAfter(current, current.Value.ReadByteArray());
                    }
                    current.Value.Release();
                    this.decodeByteArray.Remove(current);
                }

                this.decodeSize -= (bodyLength + 4);

                commandData.Seek();
                IByteArray msgData = null;
                if ((compressFlag & 0x40) != 0)
                {
                    msgData = this.storage.Alloc(BUFFERSIZE);   // 分配storage定义的BufferSize
                    // ZipCompress.Decompress(commandData, msgData);
                }
                else
                {
                    msgData = commandData;
                    msgData.Retain();
                }
                commandData.Release();

                byte command = msgData.ReadByte();
                byte param   = msgData.ReadByte();

                Logger <IGiantFreeServer> .L("<<<<收到返回数据 size = " + msgData.Size + ", byCmd = " + command + ", byParam = " + param);

                ushort sign = (ushort)(((int)command << 8) + param);
                msgData.Seek(6);
                Action <byte[]> callback = null;
                if (this.messageCallbacks.TryGetValue(sign, out callback))
                {
                    callback(msgData.ReadBytes());
                }
                else if (null != this.client)
                {
                    this.client.HandleMessage(command, param, msgData.ReadBytes());
                }
                msgData.Release();
            }
        }