Esempio n. 1
0
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient();

            int        port     = 11000;
            IPAddress  ip       = IPAddress.Parse("127.0.0.1");
            IPEndPoint endPoint = new IPEndPoint(ip, port);

            client.Connect(endPoint);

            NetworkStream stream = client.GetStream();

            byte[] buffer = new byte[256];

            int nBytesRead = stream.Read(buffer);

            byte[] keyBuffer = BigInteger.ModPow(keyExchange.g, b, keyExchange.n).ToByteArray();

            stream.Write(keyBuffer);

            byte[] temp = new byte[nBytesRead];

            Array.Copy(buffer, 0, temp, 0, nBytesRead);

            key = keyExchange.GenerateKey(new BigInteger(buffer), b);

            while (true)
            {
                ReceiveMessage(stream);
                Console.Write("Write your message here: ");
                string text = Console.ReadLine();

                if (text.ToLower() == "c")
                {
                    break;
                }

                buffer = Encoding.UTF8.GetBytes(text);

                stream.Write(encryption.SubstitutionEncrypt(buffer, key), 0, buffer.Length);
            }
            client.Close();
        }
Esempio n. 2
0
        static public async void AcceptClients(TcpListener listener)
        {
            bool isRunning = true;

            while (isRunning)
            {
                TcpClient client = await listener.AcceptTcpClientAsync();

                clients.Add(client);
                NetworkStream stream = client.GetStream();

                byte[] keybuffer = BigInteger.ModPow(KeyExchange.g, a, KeyExchange.n).ToByteArray();

                stream.Write(keybuffer);

                byte[] buffer = new byte[256];

                int nBytesRead = await stream.ReadAsync(buffer);

                key = KeyExchange.GenerateKey(new BigInteger(buffer), a);

                ReceiveMessages(stream);
            }
        }