public override bool Encrypt(NetOutgoingMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            int unEncLenBits = message.BitLength;

            var ms = new MemoryStream();

            using (var encryptor = Algorithm.CreateEncryptor())
                using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write, true))
                    cs.Write(message.GetBuffer().AsSpan(0, message.ByteLength));

            int length = (int)ms.Length;

            message.BitPosition = 0;
            message.EnsureByteCapacity(length + 4);
            message.Write((uint)unEncLenBits);
            message.Write(ms.GetBuffer().AsSpan(0, length));
            message.ByteLength = length + 4;

            return(true);
        }
        public override bool Encrypt(NetOutgoingMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            byte[]      data  = message.GetBuffer();
            Span <byte> slice = data.AsSpan(0, message.ByteLength);

            for (int i = 0; i < slice.Length; i++)
            {
                int offset = i % _key.Length;
                slice[i] = (byte)(slice[i] ^ _key[offset]);
            }
            return(true);
        }
        /// <summary>
        /// Encrypt am outgoing message with this algorithm;
        /// no writing can be done to the message after encryption,
        /// or message will be corrupted.
        /// </summary>
        public override bool Encrypt(NetOutgoingMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            int payloadBitLength = message.BitLength;
            int numBytes         = message.ByteLength;
            int blockSize        = BlockSize;
            int numBlocks        = (int)Math.Ceiling(numBytes / (double)blockSize);
            int dstSize          = numBlocks * blockSize;

            message.EnsureBitCapacity((dstSize + 4) * 8); // add 4 bytes for payload length
            message.BitPosition = 0;

            Span <byte> buffer        = _buffer.AsSpan();
            Span <byte> messageBuffer = message.GetBuffer().AsSpan();

            for (int i = 0; i < numBlocks; i++)
            {
                Span <byte> messageSlice = messageBuffer[(i * blockSize)..];
        // on user thread
        // the message must not be sent already
        private NetSendResult SendFragmentedMessage(
            NetOutgoingMessage message,
            IEnumerable <NetConnection?> recipients,
            NetDeliveryMethod method,
            int sequenceChannel)
        {
            // determine minimum mtu for all recipients
            int mtu = GetMTU(recipients, out int recipientCount);

            if (recipientCount == 0)
            {
                Recycle(message);
                return(NetSendResult.NoRecipients);
            }

            int group = GetNextFragmentGroup();

            // do not send msg; but set fragmentgroup in case user tries to recycle it immediately
            message._fragmentGroup = group << 2 | 1;

            // create fragmentation specifics
            int totalBytes = message.ByteLength;

            int bytesPerChunk = NetFragmentationHelper.GetBestChunkSize(group, totalBytes, mtu);

            int numChunks = totalBytes / bytesPerChunk;

            if (numChunks * bytesPerChunk < totalBytes)
            {
                numChunks++;
            }

            var retval = NetSendResult.Sent;

            int bitsPerChunk = bytesPerChunk * 8;
            int bitsLeft     = message.BitLength;

            byte[] buffer = message.GetBuffer();

            for (int i = 0; i < numChunks; i++)
            {
                NetOutgoingMessage chunk = CreateMessage();
                chunk.SetBuffer(buffer, false);
                chunk.BitLength = Math.Min(bitsLeft, bitsPerChunk);

                chunk._fragmentGroup          = group << 2 | 1;
                chunk._fragmentGroupTotalBits = totalBytes * 8;
                chunk._fragmentChunkByteSize  = bytesPerChunk;
                chunk._fragmentChunkNumber    = i;

                LidgrenException.Assert(chunk.BitLength != 0);
                LidgrenException.Assert(chunk.GetEncodedSize() <= mtu);

                Interlocked.Add(ref chunk._recyclingCount, recipientCount);

                foreach (NetConnection?recipient in recipients.AsListEnumerator())
                {
                    if (recipient == null)
                    {
                        continue;
                    }

                    NetSendResult result = recipient.EnqueueMessage(chunk, method, sequenceChannel).Result;
                    if (result > retval)
                    {
                        retval = result; // return "worst" result
                    }
                }

                bitsLeft -= bitsPerChunk;
            }
            return(retval);
        }