示例#1
0
 private void AdvanceIV()
 {
     Iv = Rand32.CrtRand(Iv);
 }
示例#2
0
        public MaplePacket Read(DateTime pTransmitted, bool bMaple2)
        {
            if (mCursor < _expectedDataSize)
            {
                return(null);
            }
            if (bMaple2)
            {
                mRawSeq = BitConverter.ToUInt16(mBuffer, 0);
                if (mOutbound && DecodeSeqBase(IV) != Build || !mOutbound && DecodeSeqBase(Rand32.CrtRand(IV)) != Build)
                {
                    throw new Exception("Failed to confirm packet header");
                }
            }
            else
            {
                if (!mAES.ConfirmHeader(mBuffer, 0))
                {
                    throw new Exception("Failed to confirm packet header");
                }
            }

            int headerLength = bMaple2 ? 6 : MapleAES.GetHeaderLength(mBuffer, mCursor, _usesOldHeader);

            _expectedDataSize = headerLength;
            if (mCursor < headerLength)
            {
                return(null);
            }

            int packetSize = MapleAES.GetPacketLength(mBuffer, mCursor, _usesOldHeader, bMaple2);

            _expectedDataSize = packetSize + headerLength;
            if (mCursor < (packetSize + headerLength))
            {
                return(null);
            }

            byte[] packetBuffer = new byte[packetSize];
            Buffer.BlockCopy(mBuffer, headerLength, packetBuffer, 0, packetSize);

            uint preDecodeIV;

            if (bMaple2)
            {
                preDecodeIV = IV;

                mCrypt.Decrypt(packetBuffer, packetSize, BlockIV, IV);
                ShiftIV();
            }
            else
            {
                preDecodeIV = BitConverter.ToUInt32(mAES.mIV, 0);
                Decrypt(packetBuffer, _transformMethod);
            }

            var postDecodeIV = bMaple2 ? IV : BitConverter.ToUInt32(mAES.mIV, 0);

            mCursor -= _expectedDataSize;
            if (mCursor > 0)
            {
                Buffer.BlockCopy(mBuffer, _expectedDataSize, mBuffer, 0, mCursor);
            }
            ushort opcode;

            if (_usesByteHeader)
            {
                opcode = (ushort)(packetBuffer[0]);
                Buffer.BlockCopy(packetBuffer, 1, packetBuffer, 0, packetSize - 1);
                Array.Resize(ref packetBuffer, packetSize - 1);
            }
            else
            {
                opcode = (ushort)(packetBuffer[0] | (packetBuffer[1] << 8));
                Buffer.BlockCopy(packetBuffer, 2, packetBuffer, 0, packetSize - 2);
                Array.Resize(ref packetBuffer, packetSize - 2);
            }

            _expectedDataSize = bMaple2 ? 6 : 4;

            Definition definition = Config.Instance.GetDefinition(Build, Locale, mOutbound, opcode);

            return(new MaplePacket(pTransmitted, mOutbound, Build, Locale, opcode, definition == null ? "" : definition.Name, packetBuffer, preDecodeIV, postDecodeIV));
        }