public void sendCommand(string dataToSend, string howToEncrypt) { server = client.GetStream(); dataToSend = encryptionClass.Encrypt(dataToSend, howToEncrypt); byte[] outStream = System.Text.Encoding.ASCII.GetBytes(dataToSend + "$"); server.Write(outStream, 0, (int)outStream.Length); server.Flush(); }
private void checkClient() { canConnect = false; isConnected = false; closeCommand = false; ipToBlock.Clear(); malwareToBlock.Clear(); ipWhichTriedToConnect.Clear(); mainWindowClass.statusControl.Dispatcher.Invoke(new Action(() => mainWindowClass.statusControl.Content = "Status : waiting for user")); while (!canConnect) { client = server.AcceptTcpClient(); bool okIp = true; string clientIp = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); foreach (string ip in malwareToBlock) { if (ip == clientIp) { okIp = false; client.Close(); } } foreach (string ip in ipToBlock) { if (ip == clientIp) { networkStream = client.GetStream(); string serverResponse = encryptionClass.Encrypt("Max reached", "1s 1t 0k %!1,5") + "$"; Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse); networkStream.Write(sendBytes, 0, sendBytes.Length); networkStream.Flush(); okIp = false; client.Close(); } } if (okIp) { try { networkStream = client.GetStream(); string serverResponse = encryptionClass.Encrypt("NothingWrong", "1s 1t 0k %!1,5") + "$"; Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse); networkStream.Write(sendBytes, 0, sendBytes.Length); networkStream.Flush(); networkStream = client.GetStream(); byte[] byteFrom = new byte[100250]; networkStream.Read(byteFrom, 0, (int)client.ReceiveBufferSize); string dataFromClient = System.Text.Encoding.ASCII.GetString(byteFrom); dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$")); dataFromClient = encryptionClass.Decrypt(dataFromClient, "Th15 p@55w0r4 w111 b3 r3c31v3d %!1"); if (dataFromClient == "FailedToDecrypt") { malwareToBlock.Add(clientIp); mainWindowClass.failedToDecryptAtPassCheckServer(); } else if (dataFromClient == mainWindowClass.serverPassword) { isConnected = true; canConnect = true; for (int i = 0; i < ipWhichTriedToConnect.Count; i++) { if (ipWhichTriedToConnect[i] == clientIp) { passwordTriedPerIpTries[i] = 0; } } mainWindowClass.notification("Somebody has connected to your computer !"); networkStream = client.GetStream(); serverResponse = encryptionClass.Encrypt("YouShallPassqwerty", "Th15 1s th3 r3sp0ns3 %!2") + "$"; sendBytes = Encoding.ASCII.GetBytes(serverResponse); networkStream.Write(sendBytes, 0, sendBytes.Length); networkStream.Flush(); mainWindowClass.statusControl.Dispatcher.Invoke(new Action(() => mainWindowClass.statusControl.Content = "Status : online")); mainWindowClass.chatAvalible = true; mainWindowClass.activateChat(); } else { bool found = false; int whichOne = 0; for (int i = 0; i < ipWhichTriedToConnect.Count; i++) { if (ipWhichTriedToConnect[i] == clientIp) { found = true; passwordTriedPerIpTries[i]++; whichOne = passwordTriedPerIpTries[i]; if (passwordTriedPerIpTries[i] == 3) { ipToBlock.Add(ipWhichTriedToConnect[i]); } } } if (!found) { ipWhichTriedToConnect.Add(clientIp); passwordTriedPerIpTries.Add(1); whichOne = passwordTriedPerIpTries[passwordTriedPerIpTries.Count - 1]; } networkStream = client.GetStream(); serverResponse = encryptionClass.Encrypt("YouShallNotPass" + (4 - whichOne).ToString(), "Th15 1s th3 r3sp0ns3 %!2") + "$"; sendBytes = Encoding.ASCII.GetBytes(serverResponse); networkStream.Write(sendBytes, 0, sendBytes.Length); networkStream.Flush(); } if (!canConnect) { client.Close(); } } catch (Exception) { //MessageBox.Show(ex.ToString()); } } } }