public override bool Encrypt(NetOutgoingMessage msg)
        {
            int unEncLenBits = msg.LengthBits;

            var ms = new MemoryStream();
            var cs = GetEncryptStream(ms);
            cs.Write(msg.m_data, 0, msg.LengthBytes);
            cs.Close();

            // get results
            var arr = ms.ToArray();
            ms.Close();

            msg.EnsureBufferSize((arr.Length + 4) * 8);
            msg.LengthBits = 0; // reset write pointer
            msg.Write((uint)unEncLenBits);
            msg.Write(arr);
            msg.LengthBits = (arr.Length + 4) * 8;

            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 msg)
        {
            int payloadBitLength = msg.LengthBits;
            int numBytes = msg.LengthBytes;
            int blockSize = BlockSize;
            int numBlocks = (int)Math.Ceiling((double)numBytes / (double)blockSize);
            int dstSize = numBlocks * blockSize;

            msg.EnsureBufferSize(dstSize * 8 + (4 * 8)); // add 4 bytes for payload length at end
            msg.LengthBits = dstSize * 8; // length will automatically adjust +4 bytes when payload length is written

            for(int i=0;i<numBlocks;i++)
            {
                EncryptBlock(msg.m_data, (i * blockSize), m_tmp);
                Buffer.BlockCopy(m_tmp, 0, msg.m_data, (i * blockSize), m_tmp.Length);
            }

            // add true payload length last
            msg.Write((UInt32)payloadBitLength);

            return true;
        }
 private void WriteLocalHail(NetOutgoingMessage om)
 {
     if (m_localHailMessage != null)
     {
         byte[] hi = m_localHailMessage.Data;
         if (hi != null && hi.Length >= m_localHailMessage.LengthBytes)
         {
             if (om.LengthBytes + m_localHailMessage.LengthBytes > m_peerConfiguration.m_maximumTransmissionUnit - 10)
                 m_peer.ThrowOrLog("Hail message too large; can maximally be " + (m_peerConfiguration.m_maximumTransmissionUnit - 10 - om.LengthBytes));
             om.Write(m_localHailMessage.Data, 0, m_localHailMessage.LengthBytes);
         }
     }
 }