/// <summary> /// Handles socket client request - encrypts or decrypts text /// </summary> /// <param name="client">client to handle</param> private static void HandleClient(Socket client) { Console.WriteLine(String.Format("Client connected. Task: {0}", Task.CurrentId.HasValue ? Task.CurrentId.Value.ToString() : "-")); //recieve data byte[] data = new byte[1024 * 5000]; int receivedBytesLen = client.Receive(data); byte[] receivedData = new byte[receivedBytesLen]; Array.Copy(data, receivedData, receivedBytesLen); //deserialize data SDO sdoEncoder = new SDO(); Message receivedMessage = (Message)sdoEncoder.Deserialize(receivedData, typeof(Message)); Console.WriteLine(String.Format("Original message text: {0}. Task: {1}", receivedMessage.Text, Task.CurrentId.HasValue ? Task.CurrentId.Value.ToString() : "-")); //select crypto algorithm SymmetricAlgorithm cryptAlgorithm; switch (receivedMessage.Alg) { case "Rijndael": cryptAlgorithm = Rijndael.Create(); break; case "TripleDES": cryptAlgorithm = TripleDES.Create(); break; default: goto case "Rijndael"; } //parse key and initialization vecor byte[] key = new byte[16]; byte[] iv = new byte[16]; byte[] receivedKey = Encoding.UTF8.GetBytes(receivedMessage.Key); byte[] receivedIV = Encoding.UTF8.GetBytes(receivedMessage.IV); Array.Copy(receivedKey, key, receivedKey.Length); Array.Copy(receivedIV, iv, receivedIV.Length); //encrypt or decrypt text string proccessedText; if (receivedMessage.Encrypt) proccessedText = Encrypt(receivedMessage.Text, key, iv, cryptAlgorithm); else proccessedText = Decrypt(receivedMessage.Text, key, iv, cryptAlgorithm); Console.WriteLine(String.Format("Proccessed message text: {0}. Task: {1}", proccessedText, Task.CurrentId.HasValue ? Task.CurrentId.Value.ToString() : "-")); //serialize and send proccessed text back to client byte[] serializedText = sdoEncoder.Serialize(proccessedText); client.Send(serializedText); //close connection client.Close(); Console.WriteLine(String.Format("Client disconnected. Task: {0}", Task.CurrentId.HasValue ? Task.CurrentId.Value.ToString() : "-")); }
static void Main(string[] args) { bool correctUserInput = false; string text = ""; string fileName = ""; while (!correctUserInput) { //get file name Console.WriteLine("Įveskite duomenų failo pavadinimą:"); fileName = Console.ReadLine(); //read file try { FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None); byte[] bytesRead = new byte[fs.Length]; int numBytesToRead = (int)fs.Length; int numBytesRead = 0; while (numBytesToRead > 0) { // read may return anything from 0 to numBytesToRead. int n = fs.Read(bytesRead, numBytesRead, numBytesToRead); // break when the end of the file is reached. if (n == 0) break; numBytesRead += n; numBytesToRead -= n; } text = Encoding.UTF8.GetString(bytesRead); correctUserInput = true; } catch (Exception e) { Console.WriteLine(String.Format("Nepavyko nuskaityti failo: {0}. Bandykite dar kartą.", e.Message)); correctUserInput = false; }; } //encrypt or decrypt correctUserInput = false; bool encrypt = false; while (!correctUserInput) { Console.WriteLine("Tekstas turi būti užšifruotas ar iššifruotas? \n 1 - Užšifruotas \n 2 - Iššifruotas"); try { int chosenAlg = int.Parse(Console.ReadLine()); switch (chosenAlg) { case 1: encrypt = true; correctUserInput = true; break; case 2: encrypt = false; correctUserInput = true; break; } } catch (Exception e) { Console.WriteLine("Neteisingai pasirinktas variantas. Bandykite dar kartą."); } } //choose Crypto algorithm correctUserInput = false; string algorithmName = ""; while (!correctUserInput) { Console.WriteLine("Pasirinkite šifravimo algoritmą: \n 1 - Rijndael \n 2 - TripleDES"); try { int chosenAlg = int.Parse(Console.ReadLine()); switch (chosenAlg) { case 1: algorithmName = "Rijndael"; correctUserInput = true; break; case 2: algorithmName = "TripleDES"; correctUserInput = true; break; } } catch (Exception e){ Console.WriteLine("Neteisingai pasirinktas šiframvimo algoritmas. Bandykite dar kartą."); } } //get Key correctUserInput = false; string key = ""; while(!correctUserInput) { Console.WriteLine("Įveskite šifravmimo raktą:"); key = Console.ReadLine(); if (key.Length <= 16) correctUserInput = true; else Console.WriteLine("Raktas negali būti ilgesnis nei 16 simbolių. Bandykite dar kartą."); } //get initializacion vector string iv; if (encrypt) { iv = RandomString(5); Console.WriteLine(String.Format("Jums sugeneruotas inicializacijos vektorius:{0}", iv)); } else { Console.WriteLine("Įveskite inicializacijos vektorių:"); iv = Console.ReadLine(); } Message msg = new Message() { Alg = algorithmName, IV = iv, Key = key, Text = text, Encrypt = encrypt }; SDO sdoEncoder = new SDO(); byte[] serializedMessage = sdoEncoder.Serialize(msg); //connect to server IPAddress ipAddress = IPAddress.Loopback;//Dns.GetHostAddresses("localhost")[0]; IPEndPoint ipEnd = new IPEndPoint(ipAddress, 5656); Socket clientSock = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.IP); clientSock.Connect(ipEnd); //send serialized message clientSock.Send(serializedMessage); //get response byte[] data = new byte[1024 * 5000]; int receivedBytesLen = clientSock.Receive(data); byte[] receivedData = new byte[receivedBytesLen]; Array.Copy(data, receivedData, receivedBytesLen); //close connection clientSock.Close(); string receivedText = (string)sdoEncoder.Deserialize(receivedData, typeof(string)); //save encrypted/decrypted info to file fileName = fileName.Substring(0,fileName.IndexOf('.')) + "Response.txt"; File.WriteAllText(fileName, receivedText); Console.WriteLine(String.Format("Užšifruotas/iššifruotas tekstas buvo išsaugotas į failą: {0}", fileName)); Console.ReadKey(); }