示例#1
0
        private void BeginCommunication(Socket handler)
        {
            try
            {
                bool isComm = isCommunication();
                if (!isComm)
                {
                    handler.Send(Encoding.UTF8.GetBytes(Properties.NO_COMMUNICATION));
                    return;
                }


                string    privateKey = Properties.getPrivateKey();
                PublicKey publicKey  = Properties.getPublicKey();

                char[] sendKey = CriptoUtils.createSesionKey(publicKey, privateKey.ToCharArray());
                handler.Send(Encoding.UTF8.GetBytes(sendKey));

                string recvKey = SocketUtils.RecvData(handler);
                Console.WriteLine(recvKey.Length + " " + privateKey.Length);
                char[] sessionKey = CriptoUtils.createSesionKey(new PublicKey(recvKey, publicKey.mod), privateKey.ToCharArray());
                Properties.setSessionKey(new string(sessionKey));
                string encodedString = CriptoUtils.Encrypt(Properties.TEST_COMMUNICATON, Properties.getSessionKey());
                handler.Send(Encoding.UTF8.GetBytes(encodedString));
                mainWindow.write(SocketClient.SUCCESS);
                mainWindow.setEnabledSend();
            }
            catch (Exception e)
            {
                mainWindow.write("Не удалось установить соединение по причине: " + e.Message);
            }
        }
示例#2
0
        public bool SendMessageFromSocket(string message, bool isCrypto)
        {
            Socket sender = null;

            try
            {
                sender = init();
                Console.WriteLine("Сокет соединяется с {0} ", sender.RemoteEndPoint.ToString());
                byte[] msg       = Encoding.UTF8.GetBytes(isCrypto? CriptoUtils.Encrypt(message, Properties.getSessionKey()): message);
                int    bytesSent = sender.Send(msg);
                Console.WriteLine("Сокет послал с {0} ", sender.RemoteEndPoint.ToString());
                return(true);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                return(false);
            }
            finally
            {
                close(sender);
            }
        }