Exemple #1
0
        private void ClientConnected(object sender, ClientAcceptEventArgs e)
        {
            var clientConnection = e.AcceptedConnection;
            var serverSocket     = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            serverSocket.Connect(this.txtOtherServer.Text, (int)this.numRealGSPort.Value);
            var socketConnection = SocketConnection.Create(serverSocket);
            var encryptor        = new PipelinedXor32Encryptor(new PipelinedSimpleModulusEncryptor(socketConnection.Output, PipelinedSimpleModulusEncryptor.DefaultClientKey).Writer);
            var decryptor        = new PipelinedSimpleModulusDecryptor(socketConnection.Input, PipelinedSimpleModulusDecryptor.DefaultClientKey)
            {
                AcceptWrongBlockChecksum = true
            };
            var serverConnection = new Connection(socketConnection, decryptor, encryptor);
            var proxy            = new Proxy(clientConnection, serverConnection, this.InvokeByProxy);

            this.InvokeByProxy(new Action(() =>
            {
                this.proxiedConnections.Add(proxy);
                if (this.proxiedConnections.Count == 1)
                {
                    this.connectedClientsListBox.SelectedItem = proxy;
                    this.ConnectionSelected(this, EventArgs.Empty);
                }
            }));
        }
Exemple #2
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The arguments. The target address can be specified as an argument in the format [IP]:[Port].</param>
        internal static async Task Main(string[] args)
        {
            var address          = args.Length > 0 ? args[0] : "127.0.0.1:55901";
            var socketConnection = await SocketConnection.ConnectAsync(IPEndPoint.Parse(address));

            var encryptor  = new PipelinedXor32Encryptor(new PipelinedSimpleModulusEncryptor(socketConnection.Output, PipelinedSimpleModulusEncryptor.DefaultClientKey).Writer);
            var decryptor  = new PipelinedSimpleModulusDecryptor(socketConnection.Input, PipelinedSimpleModulusDecryptor.DefaultClientKey);
            var connection = new Connection(socketConnection, decryptor, encryptor);

            _ = new TestClient(connection);

            await connection.BeginReceive();

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
        /// <summary>
        /// Tests the encryption-decryption cycle for the packet. The specified packet must be the same after the packet has passed this cycle.
        /// </summary>
        /// <param name="packet">The packet.</param>
        /// <returns>The async task.</returns>
        private async Task EncryptDecryptFromClientToServer(byte[] packet)
        {
            // this pipe connects the encryptor with the decryptor. You can imagine this as the client-to-server pipe of a network socket, for example.
            var pipe = new Pipe();

            var encryptor = new PipelinedXor32Encryptor(new PipelinedSimpleModulusEncryptor(pipe.Writer, PipelinedSimpleModulusEncryptor.DefaultClientKey).Writer);
            var decryptor = new PipelinedXor32Decryptor(new PipelinedSimpleModulusDecryptor(pipe.Reader).Reader);

            encryptor.Writer.Write(packet);
            await encryptor.Writer.FlushAsync();

            var readResult = await decryptor.Reader.ReadAsync().ConfigureAwait(false);

            var result = readResult.Buffer.ToArray();

            Assert.That(result, Is.EquivalentTo(packet));
        }
        public async ValueTask Xor32Encryption()
        {
            var pipe = new Pipe();
            var pipelinedEncryptor = new PipelinedXor32Encryptor(pipe.Writer);
            var readBuffer         = new byte[256];

            for (int i = 0; i < PacketCount; i++)
            {
                await pipelinedEncryptor.Writer.WriteAsync(this.c1Packet);

                await pipelinedEncryptor.Writer.FlushAsync();

                var readResult = await pipe.Reader.ReadAsync();

                readResult.Buffer.CopyTo(readBuffer);
                //// In the client/server, I would process the readBuffer here
                pipe.Reader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End);
            }

            pipelinedEncryptor.Writer.Complete();
        }