internal void SendOnly(Message message) { var rsa = new EncryptionRSA(); rsa.SetPublicKey(PublicKey); message.WriteTo(_socket.GetStream(), rsa); }
internal void ReadOnly(Message message) { var rsa = new EncryptionRSA(); rsa.SetPrivateKey(PrivateKey); message.ReadFrom(_socket.GetStream(), rsa); }
public TCPServer(string hOST, int pORT) { this.hOST = hOST; this.pORT = pORT; RSA = new EncryptionRSA(); PublicKeyToSend = RSA.GetPublicKey(); PrivateKey = RSA.GetPrivateKey(); }
public void Start() { //start the server IPAddress iPAddress = Dns.GetHostEntry(hOST).AddressList[0]; TcpListener = new TcpListener(iPAddress, pORT); try { TcpListener.Start(); Console.WriteLine("Server started..."); } catch (Exception e) { Console.WriteLine(e.Message); } while (true) { TcpClient tcpClient = TcpListener.AcceptTcpClient(); //hand shake //Server send public key to client SendPublicKey(tcpClient); //Server read public key from client string publicKeyFromClient = ReadPublicKey(tcpClient); //handle login //Read username and password from user Message request = new Message(); var rsa = new EncryptionRSA(); rsa.SetPrivateKey(PrivateKey); request.ReadFrom(tcpClient.GetStream(), rsa); Message response = handlers[request.Header].Invoke(request); if (response.Header.Equals("OK")) { Console.WriteLine("Client authenticated is connected..."); Console.WriteLine("server sends head: " + response.Header + " body " + response.Body); var rsa2 = new EncryptionRSA(); rsa2.SetPublicKey(publicKeyFromClient); response.WriteTo(tcpClient.GetStream(), rsa2); RegisterClient(response.Body, publicKeyFromClient, tcpClient); Thread thread = new Thread(() => HandleRequest(tcpClient)); thread.Start(); } else { var rsa2 = new EncryptionRSA(); rsa2.SetPublicKey(publicKeyFromClient); response.WriteTo(tcpClient.GetStream(), rsa2); Console.WriteLine("Client failed to connect"); } } }
private void broadcast(Message request) { foreach (Tuple <string, string, TcpClient> client in ActiveClients) { string publicKey = client.Item2; var rsa = new EncryptionRSA(); rsa.SetPublicKey(publicKey); request.WriteTo(client.Item3.GetStream(), rsa); } }
public TCPClient(string host, int port) { this._host = host; this._port = port; this.RSA = new EncryptionRSA(); PublicKeyToSend = RSA.GetPublicKey(); PrivateKey = RSA.GetPrivateKey(); _socket = new TcpClient(_host, _port); GetKeyFromServer(_socket); SendPublicKeyToServer(PublicKeyToSend); }
private void HandleRequest(TcpClient tcpClient) { while (true) { Thread.Sleep(10); Message message = new Message(); var rsa = new EncryptionRSA(); rsa.SetPrivateKey(PrivateKey); try { message.ReadFrom(tcpClient.GetStream(), rsa); Console.WriteLine("Client send: body" + message.Body + "header" + message.Header); handlers[message.Header].Invoke(message); broadcast(message); } catch (Exception e) { Console.WriteLine("Thread stop listening"); break; } } }
public bool Send(Message request) { var rsa = new EncryptionRSA(); rsa.SetPublicKey(PublicKey); request.WriteTo(_socket.GetStream(), rsa); Console.WriteLine("client -send : " + request.Body); Message response = new Message(); rsa = new EncryptionRSA(); rsa.SetPrivateKey(PrivateKey); response.ReadFrom(_socket.GetStream(), rsa); Console.WriteLine("Client receive: " + response.Body); if (response.Header.Equals("OK")) { return(true); } return(false); }
/// <summary> /// Initiate new client to connect /// </summary> /// <param name="args"></param> private async void ProcessClientConnection(object args) { TcpClient client = (TcpClient)args; StreamReader sr; StreamWriter sw; try { sr = new StreamReader(client.GetStream(), Encoding.UTF8, true, BUFFER_SIZE); sw = new StreamWriter(client.GetStream(), Encoding.UTF8, BUFFER_SIZE); sw.AutoFlush = true; while (true) { bool testDecrypted = false; Console.Write("Enter AES encryption key: "); string tempKey = "/gUFEqNJ17CV52nJTs1ruB8BsZ8kNBpyDVMJH26AGIE="; //string tempKey = Console.ReadLine(); Console.Write("Enter AES IV: "); string tempIV = "UaTwfDMLvZb2uh96KTKmvQ=="; //string tempIV = Console.ReadLine(); Console.Write("Enter Password: "******"doyouknowdawae"; //string password = Console.ReadLine(); //Load key and IV into constructor EncryptionAES.password = tempKey; EncryptionAES.iv = tempIV; //Create(password); //Response r = Login(password); //while (r.Success == false) //{ // Console.Write("Enter AES encryption key: "); // tempKey = Console.ReadLine(); // Console.Write("Enter AES IV: "); // tempIV = Console.ReadLine(); // Console.Write("Enter Password: "******"true"); //Sending database public key to webserver in plain await sw.FlushAsync(); break; } else { CustomAPI.logging("Incorrect encryption key or IV entered"); } } if (!auth_bypass) { bool wsPublicKeyRetrieved = false; string password = "******"; int rsaKeySize = 1024; string dbPublicKey = null; string dbPrivateKey = null; string wsPublicKey = null; //bool proceed = true; //if (!proceed) //{ // return; //} while (true) { //TODO: Authentication between servers if (!wsPublicKeyRetrieved) { try { Console.WriteLine("Generating a {0} size key ...", rsaKeySize); EncryptionRSA.GenerateKeys(rsaKeySize, out dbPublicKey, out dbPrivateKey); await sw.WriteLineAsync(dbPublicKey); //Sending database public key to webserver in plain await sw.FlushAsync(); wsPublicKey = sr.ReadLine(); Console.WriteLine("Webserver public key received"); wsPublicKeyRetrieved = true; } catch (Exception e) { CustomAPI.logging(e.Message); wsPublicKeyRetrieved = false; } } else if (!keyExchanged) { using (ECDiffieHellmanCng ecd = new ECDiffieHellmanCng()) { ecd.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; ecd.KeySize = 256; ecd.HashAlgorithm = CngAlgorithm.Sha256; tempKey = ecd.PublicKey.ToByteArray(); //Generate shared secret key string encryptTempKey = EncryptionRSA.Encrypt(Convert.ToBase64String(tempKey), rsaKeySize, wsPublicKey); await sw.WriteLineAsync(encryptTempKey); //Sending database temp key to webserver server await sw.FlushAsync(); string encryptedWsTempKey = sr.ReadLine(); //Retrieve webserver temp key byte[] wsTempKey = Convert.FromBase64String(EncryptionRSA.Decrypt(encryptedWsTempKey, rsaKeySize, dbPrivateKey)); masterKey = ecd.DeriveKeyMaterial(CngKey.Import(wsTempKey, CngKeyBlobFormat.EccPublicBlob)); CustomAPI.readContent(masterKey); Console.Write("Master Key Generated"); iv = Convert.FromBase64String(sr.ReadLine()); //IV Generated from webserver set to here keyExchanged = true; } } else { break; //Configuration between servers have been completed } } } if (keyExchanged) { ListeningToClient(sr, sw); } } catch (Exception e) { Console.WriteLine(e); if (client != null) { client.Close(); } } }
/// <summary> /// Webserver attempts to connect to the database /// </summary> public static void RegisterDataAccess() { //TODO: Key exchange between database server and webserver try { client = new TcpClient("127.0.0.1", 3351); sr = new StreamReader(client.GetStream(), Encoding.UTF8, true, BUFFER_SIZE); sw = new StreamWriter(client.GetStream(), Encoding.UTF8, BUFFER_SIZE); sw.AutoFlush = true; Debug.WriteLine("Waiting for database server"); bool proceed = false; while (!proceed) { proceed = Convert.ToBoolean(sr.ReadLine()); Debug.WriteLine("WAITING"); } if (!auth_bypass) { bool dbPublicKeyRetrieved = false; bool keyExchanged = false; string password = "******"; //Key in from user int rsaKeySize = 1024; string wsPublicKey = null; string wsPrivateKey = null; string dbPublicKey = null; while (true) { //TODO: Authentication between servers if (!dbPublicKeyRetrieved) { Debug.WriteLine("Generating a {0} size key", rsaKeySize); EncryptionRSA.GenerateKeys(rsaKeySize, out wsPublicKey, out wsPrivateKey); sw.WriteLine(wsPublicKey); //Sending database public key to webserver in plain sw.Flush(); dbPublicKey = sr.ReadLine(); dbPublicKeyRetrieved = true; } else if (!keyExchanged) { using (ECDiffieHellmanCng ecd = new ECDiffieHellmanCng()) { ecd.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; ecd.KeySize = 256; ecd.HashAlgorithm = CngAlgorithm.Sha256; tempKey = ecd.PublicKey.ToByteArray(); //Generate shared secret key string encryptTempKey = EncryptionRSA.Encrypt(Convert.ToBase64String(tempKey), rsaKeySize, dbPublicKey); sw.WriteLine(encryptTempKey); //Sending database temp key to webserver server sw.Flush(); string encryptDbTempKey = sr.ReadLine(); //Retrieve webserver temp key byte[] dbTempKey = Convert.FromBase64String(EncryptionRSA.Decrypt(encryptDbTempKey, rsaKeySize, wsPrivateKey)); masterKey = ecd.DeriveKeyMaterial(CngKey.Import(dbTempKey, CngKeyBlobFormat.EccPublicBlob)); CustomAPI.readContent(masterKey); Debug.WriteLine("Master Key Generated"); //Generate an new IV using (Aes aes = new AesCryptoServiceProvider()) { iv = aes.IV; } sw.WriteLine(Convert.ToBase64String(iv)); sw.Flush(); keyExchanged = true; } } else { break; } } } sw.AutoFlush = false; } catch (Exception e) { CustomAPI.logging(e.Message); } }