private void ProcessClient() { ExchangeAgent = new DiffieHellmanKeyExchange(ppk, pbn, pk); client.Send(Encoding.ASCII.GetBytes("100 " + ExchangeAgent.midkey.ToString("X"))); Thread.Sleep(100); client.Send(Encoding.ASCII.GetBytes("120 " + ExchangeAgent.n.ToString("X"))); Thread.Sleep(100); client.Send(Encoding.ASCII.GetBytes("130 " + ExchangeAgent.g.ToString("X"))); client.Send(Encoding.ASCII.GetBytes("")); try { while (true) { byte[] data = new byte[1024]; int size = client.Receive(data); if (size == 0) { PrintLine("Connection Closed...\n", ok_color); break; } var incomingdata = ""; for (int i = 0; i < size; i++) { incomingdata += Convert.ToChar(data[i]).ToString(); } if (EnableEncryption) { incomingdata = ExchangeAgent.Decrypt(incomingdata); PrintLine("Client : " + incomingdata + "\n", re_message); playNewMessage(); } else { if (incomingdata.Substring(0, 3) == "100") { ExchangeAgent.CalculatePrivateKey( BigInteger.Parse( incomingdata.Substring(4, incomingdata.Length - 4), System.Globalization.NumberStyles.HexNumber )); EnableEncryption = true; PrintLine("Secure Connection Ready...\n\n", ok_color); playSuccess(); } else { PrintLine("Unsecure : Client : " + incomingdata + "\n", er_color); playNewMessage(); } } } } catch (System.Net.Sockets.SocketException e) { PrintLine("Closed Connection...\n", er_color); } }
public void Connect() { var client = new TcpClient(); client.Connect(servername, port); // Connect to the server networkStream = client.GetStream(); var reader = new StreamReader(networkStream, Encoding.UTF8); string g = null; string n = null; string midkey = null; PrintLine("Connecting to server...\n", color: ok_color); while (true) { byte[] data = new byte[1024]; int size = networkStream.Read(data, 0, 1024); var incomingdata = ""; for (int i = 0; i < size; i++) // Convert the ByteStream into a string... or something... { incomingdata += Convert.ToChar(data[i]).ToString(); } if (EnableEncryption) // if the encryption is enabled, then decrypt it before processeding... { incomingdata = ExchangeAgent.Decrypt(incomingdata); PrintLine("Server : " + incomingdata + "\n", color: re_message); playNewMessage(); } else { if (String.IsNullOrWhiteSpace(incomingdata)) { continue; } var substr = ""; var inc_data = ""; try { substr = incomingdata.Substring(0, 3); inc_data = incomingdata.Substring(4, incomingdata.Length - 4); } catch { } if (substr == "100") // if the header is 100 its a public key { midkey = inc_data; PrintLine("Received Public Key...\n", color: ok_color); } else if (substr == "120") // if its 120 its the public large key { n = inc_data; PrintLine("Recieved Large Key...\n", color: ok_color); } else if (substr == "130") // if its 130 its the public prime key { g = inc_data; PrintLine("Recieved Public Prime Key...\n", color: ok_color); } else // otherwise, just print the data { PrintLine("Unsecure : Server : " + incomingdata, color: er_color); } if ((g != null) && (n != null) && (midkey != null)) // if all needed components are collected, Enable encryption... { PrintLine("Sending Public Key...\n", color: ok_color); ExchangeAgent = new DiffieHellmanKeyExchange( Int32.Parse(g, System.Globalization.NumberStyles.HexNumber), BigInteger.Parse(n, System.Globalization.NumberStyles.HexNumber), private_key_p ); Send("100 " + ExchangeAgent.midkey.ToString("X"), false); // and send local public key ExchangeAgent.CalculatePrivateKey(BigInteger.Parse(midkey, System.Globalization.NumberStyles.HexNumber)); EnableEncryption = true; PrintLine("Connection successful...\n\n", color: ok_color); playSuccess(); } } } }