Ejemplo n.º 1
0
        private async void SendPublicKeyForSecureTransfer(string sessionId)
        {
            // We need to generate a public/private key pair.
            KeyValuePair <string, string> cryptoKeyPair = CryptoProvider.GenerateKeys();

            // Store the Private Key, using its Public Key as the ID.
            SessionManager.Instance.sessions[cryptoKeyPair.Key] = cryptoKeyPair.Value;

            // Send the public key up to the server.
            // We need to include the sessionId so that the server knows
            // which message to encrypt and send down.
            log.Info("Sending generated public key, including sessionId");
            var cypherTextMessage = await _chatHubProxy.Invoke <Message>("GetMessageSecurely", new KeyValuePair <string, string>(sessionId, cryptoKeyPair.Key));

            log.Info("Recieved encrypted message");

            var message = CryptoProvider.Decrypt(cypherTextMessage, SessionManager.Instance.sessions[cryptoKeyPair.Key]);

            log.Info("Decrypted message");

            //Console.WriteLine("{0} {1}: {2}", DateTime.Now.ToShortTimeString(), message.Sender, message.MessageContent);
            printMessageUnobtrusively(message);

            // Remove the session from the session store
            SessionManager.Instance.sessions.Remove(cryptoKeyPair.Key);
        }
Ejemplo n.º 2
0
        public string GetPublicKeyForSecureTransfer()
        {
            KeyValuePair <string, string> cryptoKeyPair = CryptoProvider.GenerateKeys();

            // Store the Private Key, using its Public Key as the ID.
            SessionManager.Instance.sessions.Add(cryptoKeyPair.Key, cryptoKeyPair.Value);

            // Return the public key, so the requesting client can send
            // an encrypted message.
            return(cryptoKeyPair.Key);
        }
Ejemplo n.º 3
0
        public bool Connect(string addr, int port = 0)
        {
            ErrorMessage = null;
            if (port == 0)
            {
                port = DefaultPort;
            }
            try
            {
                IsLogined = false;
                if (Client != null)
                {
                    Client.Dispose();
                }
            }
            catch
            { }

            try
            {
                //Loger.Log("Client Connect1");
                // Generate open-close keys  KClose-KOpen
                //Генерим рандомную пару КЗакр-КОткр
                var crypto = new CryptoProvider();
                if (UseCryptoKeys)
                {
                    crypto.GenerateKeys();
                }

                //Loger.Log("Client Connect2");
                Client = new ConnectClient(addr, port);

                //Loger.Log("Client Connect3");//Строго первый пакет: Передаем серверу КОткр
                if (UseCryptoKeys)
                {
                    Client.SendMessage(Encoding.UTF8.GetBytes(crypto.OpenKey));
                }
                else
                {
                    Client.SendMessage(new byte[1] {
                        0x00
                    });
                }

                //Loger.Log("Client Connect4");
                //Строго первый ответ: Передаем клиенту КОткр(Сессия)
                var rc = Client.ReceiveBytes();
                if (UseCryptoKeys)
                {
                    Key = crypto.Decrypt(rc);
                }
                else
                {
                    Key = rc;
                }

                //Loger.Log("Client Connect5");
                //Обмен по протоколу ниже: Передаем серверу Сессия(Логин - Пароль или запрос на создание)

                //Запускаем таймер фоново поддерживающий открытое соединение при простое
                ConnectSaver.AddClient(Client, (cl) =>
                {
                    lock (LockObj)
                    {
                        cl.SendMessage(new byte[1] {
                            0x00
                        });
                        cl.ReceiveBytes();
                    }
                });

                return(true);
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message
                               + (e.InnerException == null ? "" : " -> " + e.InnerException.Message);
                ExceptionUtil.ExceptionLog(e, "Client");
                return(false);
            }
        }