Exemple #1
0
        /// <summary>
        /// Create a new instance of the client with the specified options.
        /// </summary>
        /// <param name="handler">The handler of the events.</param>
        /// <param name="encryptionKey">The key which should be used to encrypt the packets.</param>
        /// <param name="authenticationData">The data based on which the server can authenticate this client.</param>
        /// <param name="ip">The ip of the server.</param>
        /// <param name="port">The port of the server.</param>
        public DoubleClient(IDoubleClientHandler handler, byte[] encryptionKey, byte[] authenticationData, IPAddress ip, int port)
        {
            lock (this) {
                _handler = handler;
                _crypto  = new FixedKeyCrypto(encryptionKey);
                TcpHelper tcpHelper = new TcpHelper(OnTcpPacketAssembled);
                _tcp = new TcpClientSocket(OnTcpConnected, OnTcpConnectionFailed, ((buffer, size) =>
                                                                                   tcpHelper.OnTcpReceived(null, buffer, size)), OnTcpLostConnection);
                _udp = new UdpClientSocket(OnUdpReceived);

                _authenticationData = authenticationData;
                _ip   = ip;
                _port = port;
            }
        }
Exemple #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);
                }
            }
        }