private UserConnection ProcessClient(Socket s) { var ss = Encoding.ASCII.GetBytes("aaaa can you see this??"); s.Send(ss); UserConnection conn = new UserConnection(); conn.Sock = s; conn.Aes = new AesCryptoServiceProvider(); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { using (RSACng rsa = new RSACng(3072)) { conn.Sock.Send(rsa.ExportRSAPublicKey()); var aesKey = new byte[384]; conn.Sock.Receive(aesKey); conn.Aes.Key = rsa.Decrypt(aesKey, RSAEncryptionPadding.Pkcs1); } } else { using (RSAOpenSsl rsa = new RSAOpenSsl(3072)) { conn.Sock.Send(rsa.ExportRSAPublicKey()); var aesKey = new byte[384]; conn.Sock.Receive(aesKey); conn.Aes.Key = rsa.Decrypt(aesKey, RSAEncryptionPadding.Pkcs1); } } byte[] encryptedMsg; var header = new byte[20]; conn.Aes.IV.CopyTo(header, 0); using (MemoryStream mem = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(mem, conn.Aes.CreateEncryptor(), CryptoStreamMode.Write)) { using (StreamWriter sw = new StreamWriter(cs)) sw.Write("OK!"); encryptedMsg = mem.ToArray(); } } BitConverter.GetBytes(encryptedMsg.Length).CopyTo(header, 16); conn.Sock.Send(header); conn.Sock.Send(encryptedMsg); return(conn); }