/// <summary> /// metoda obs³uguj¹ca asynchroniczne przy³¹czanie klientów /// </summary> /// <param name="result"></param> private void HandleAsyncConnection(IAsyncResult result) { try { // Akceptuj po³¹czenia TcpListener listener = (TcpListener)result.AsyncState; TcpClient client = listener.EndAcceptTcpClient(result); connectionWaitHandle.Set(); Cryptography crypt = new Cryptography(); AesManaged myAes = new AesManaged(); String AESKey = "SuperTajnyKlucz1"; //128 bit myAes.Key = Encoding.UTF8.GetBytes(AESKey); String AESIV = "SuperTajnyWektor"; //128 bit myAes.IV = Encoding.UTF8.GetBytes(AESIV); // Po³¹czenie zaakceptowane Guid clientId = Guid.NewGuid(); Trace.WriteLine("Accepted connection with ID " + clientId.ToString(), "Information"); // Stwórz strumienie NetworkStream netStream = client.GetStream(); StreamReader reader = new StreamReader(netStream); StreamWriter writer = new StreamWriter(netStream); writer.AutoFlush = true; //writer.WriteLine("Who are you?"); String clientType = reader.ReadLine(); // Gdy pod³¹czono loggera if (clientType == "logger") { LoggerReader = reader; LoggerWriter = writer; LoggerWriter.WriteLine("Hello, Logger!\n\rPlease wait for incoming connections"); if (loggerList.Count != 0) { LoggerWriter.WriteLine("There are some incoming logs"); foreach (string s in loggerList) { LoggerWriter.WriteLine(s); } loggerList = new List<string>(); } while (true) { try { String msg = LoggerReader.ReadLine(); if (msg == "quit") { LoggerReader = null; LoggerWriter = null; client.Close(); break; } } catch { client.Close(); } } } // Gdy pod³¹czono klienta else if (clientType == "client") { // losuj klucz szyfru cezara Random rnd = new Random(); String key = string.Empty; String decryptedKey = string.Empty; String plainText = string.Empty; // writer.WriteLine("Key: "); // writer.WriteLine(ckey); // writer.WriteLine("Welcome to RC4 Encrypting Server!"); while (true) { try { // writer.WriteLine("If you want to encrypt text, enter \"run\", if you want to quit, enter \"quit\""); String command = reader.ReadLine(); if (command == "run") { // writer.WriteLine("Please Enter Key:"); key = reader.ReadLine(); //byte[] keyAESBytes = Encoding.UTF8.GetBytes(key); byte[] keyAESBytes = crypt.StringToByteArray(key); // odszyfruj klucz decryptedKey = crypt.DecryptStringFromBytes_Aes(keyAESBytes, myAes.Key, myAes.IV); // writer.WriteLine("Please Enter Plain Text:"); string isHexStr = reader.ReadLine(); bool isHex = (isHexStr == "hex") ? true : false; plainText = reader.ReadLine(); Console.WriteLine("Key received : {0}", decryptedKey); Console.WriteLine("Plain Text received : {0}", plainText); byte[] keyBytes = Encoding.ASCII.GetBytes(decryptedKey); byte[] textBytes; if (!isHex) textBytes = Encoding.ASCII.GetBytes(plainText); else textBytes = crypt.StringToByteArray(plainText); Byte[] output = crypt.runRC4(keyBytes, textBytes); // wynik szyfrowania w postaci heksadecymalnej String outputString = crypt.ByteArrayToString(output); // wynik szyfrowania w postaci stringu utf8 String outputStringUTF8 = Encoding.UTF8.GetString(output); Byte[] decrypt = crypt.runRC4(keyBytes, output); String decryptString = Encoding.ASCII.GetString(decrypt); String decryptStringHex = crypt.ByteArrayToString(decrypt); // wyœlij wynik do loggera String msg; if (!isHex) msg = "From Client : " + clientId.ToString() + "\n\rKey : " + decryptedKey + "\n\rPlain Text : " + plainText + "\n\rResult as hex string : \n\r" + outputString + "\n\rDecrypt result : " + decryptString + "\n\r"; else msg = "From Client : " + clientId.ToString() + "\n\rKey : " + decryptedKey + "\n\rHex string : " + plainText + "\n\rResult as utf8 string : \n\r" + outputStringUTF8 + "\n\rDecrypt hex result : " + decryptStringHex + "\n\r"; if (LoggerWriter != null) LoggerWriter.Write(msg); else loggerList.Add(msg); } if (command == "quit") { client.Close(); break; } } catch { Console.WriteLine("Error while connecting to client"); client.Close(); } } } else { client.Close(); } } catch { Console.WriteLine("Something went wrong"); } }