Beispiel #1
0
        /// <summary>
        /// Sends the specified payload over TCP.
        /// </summary>
        /// <param name="payloadWriter">The action which writes the payload to a buffer.</param>
        public void SendTcp(Action <BitBuffer> payloadWriter)
        {
            lock (this) {
                if (CurrentState == State.Disconnected)
                {
                    return;
                }

                byte[] encrypted;
                using (_sendBuffer) {
                    _sendBuffer.Write(_sendSequenceId);
                    if (++_sendSequenceId == _sequenceIdBound)
                    {
                        _sendSequenceId = 0;
                    }
                    payloadWriter(_sendBuffer);
                    encrypted = _crypto.Encrypt(_sendBuffer.Array, 0, _sendBuffer.Size);
                }
                using (_sendBuffer) {
                    _sendBuffer.Write((ushort)encrypted.Length);
                    _sendBuffer.Write(encrypted);
                    _tcp.Send(_sendBuffer.Array, 0, _sendBuffer.Size);
                }
            }
        }
Beispiel #2
0
        public void TestFixedKeyCrypto()
        {
            Random random = new Random();

            byte[] key = new byte[KeyLength];

            for (int length = 0; length < MaxDataLength; length++)
            {
                random.NextBytes(key);
                FixedKeyCrypto crypto   = new FixedKeyCrypto(key);
                byte[]         original = new byte[length];

                for (int i = 0; i < DataPerKeyCount; i++)
                {
                    random.NextBytes(original);
                    byte[] encrypted = crypto.Encrypt(original, 0, original.Length);
                    byte[] decrypted = crypto.Decrypt(encrypted, 0, encrypted.Length);
                    CollectionAssert.AreEqual(original, decrypted);
                }
            }
        }