Gwupe.Communication.P2P.RUDP.Packet.AesCryptoPacketUtil.DecryptData C# (CSharp) Method

DecryptData() public method

public DecryptData ( byte data, int length ) : byte[]
data byte
length int
return byte[]
        public byte[] DecryptData(byte[] data, int length)
        {
            #if DEBUG
            //Logger.Debug("Decrypting " + length + " bytes");
            #endif
            if (_pendingBytes > 0)
            {
                // We have stuff left in our buffer
                // What is left here will start with our count
                var newData = new byte[_pendingBytes + length];
                Array.Copy(_overflowBuffer, newData, _pendingBytes);
                Array.Copy(data, 0, newData, _pendingBytes, length);
                data = newData;
                length = length + _pendingBytes;
            #if DEBUG
                //Logger.Debug("Added " + _pendingBytes + " bytes, now Decrypting " + length + " bytes");
            #endif
                _pendingBytes = 0;
            }
            int encLength = data[0] * 16 + 32;
            #if DEBUG
            //Logger.Debug("Embedded packet is of length " + encLength);
            #endif
            int offset = 1;
            int consumed = 0;
            // try avoiding using memory stream
            MemoryStream fullBytes = null;
            byte[] packetBytes = null;
            // while we can pull a full packet from the data
            while (encLength <= length - offset)// && (consumed + encLength <= MaxBufferSize))
            {
            #if DEBUG
                //Logger.Debug("Full packet is available");
            #endif
                // if we have been through once before, store that data in memory stream
                if (packetBytes != null)
                {
                    if (fullBytes == null)
                    {
                        fullBytes = new MemoryStream();
                    }
                    fullBytes.Write(packetBytes, 0, packetBytes.Length);
                }
                // Decrypt the full packet
                packetBytes = Util.getSingleton().AesDecryptBytes(data, offset, encLength, _key);
                consumed = offset + encLength;
                // if the amount we read was less
                if (encLength < length - offset)
                {
                    offset = consumed + 1;
                    encLength = data[consumed] * 16 + 32;
            #if DEBUG
                   // Logger.Debug("Embedded packet is of length " + encLength);
            #endif
                }
                else
                {
                    break;
                }
            }
            if (consumed < length)
            {
            #if DEBUG
                //Logger.Debug("Full packet is not available, need " + encLength + ", only have " + (length - offset) + " bytes, saving " + (length - consumed) + " bytes (incl length byte)");
            #endif
                _pendingBytes = length - consumed;
                Array.Copy(data, consumed, _overflowBuffer, 0, length - consumed);
            }
            if (fullBytes != null)
            {
                fullBytes.Write(packetBytes, 0, packetBytes.Length);
                packetBytes = fullBytes.ToArray();
            }
            #if DEBUG
            /*
            if (packetBytes != null)
            {
                Logger.Debug("Successfully decrypted " + packetBytes.Length + " bytes");
            }
            else
            {
                Logger.Debug("Didn't decrypt any packet this round, not enough data");
            }
             */
            #endif
            return packetBytes;
        }