Beispiel #1
0
 private void MenuDisconnect_Click(object sender, RoutedEventArgs e)
 {
     NM.DC();
 }
        private void bStartupConn_Click(object sender, RoutedEventArgs e)
        {
            if (config[0] == "true")
            {
                CFM.UpdateSetting("username", Regex.Replace(tbStartupUsername.Text, @" ", ""));
            }
            if (this.lbStartupServers.SelectedItem == null)
            {
                MessageBox.Show("Error - Please select a server");
            }
            else if (this.tbStartupUsername.Text == "")
            {
                MessageBox.Show("Please enter a username");
            }
            else
            {
                ListBoxItem lbi      = this.lbStartupServers.SelectedItem as ListBoxItem;
                string      servName = lbi.Content.ToString();

                List <List <string> > data = DBM.SQLRaw("SELECT * FROM servers WHERE name='" + servName + "'", "servers");
                string servIP    = data[0][2];
                int    servPort  = Convert.ToInt16(data[0][3]);
                bool   connected = NM.Connect(servIP, servPort);

                if (connected == true)
                {
                    KeyGenerator.SecretKey = KeyGenerator.GetUniqueKey(16);

                    Debug.WriteLine(KeyGenerator.SecretKey);


                    Dictionary <string, object> message = new Dictionary <string, object>
                    {
                        { "username", "" },
                        { "channel", "" },
                        { "content", "" },
                        { "messagetype", "keyRequest" }
                    };

                    string jsonMessage = JsonConvert.SerializeObject(message);
                    NM.SendMessage(jsonMessage);

                    byte[] bytes = new byte[20480];

                    int    bytesRec = NM.sender.Receive(bytes); // Receieve "publicKey"
                    string jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec);

                    message = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonData);

                    string publicKey  = (string)message["content"];
                    string ciphertext = EMRSA.Encrypt(KeyGenerator.SecretKey, publicKey);

                    message = new Dictionary <string, object>
                    {
                        { "username", "" },
                        { "channel", "" },
                        { "content", ciphertext },
                        { "messagetype", "secretKey" }
                    };

                    jsonMessage = JsonConvert.SerializeObject(message);
                    NM.SendMessage(jsonMessage);

                    bytes = new byte[20480];

                    bytesRec = NM.sender.Receive(bytes); // Recieve "confirmed"
                    jsonData = Encoding.UTF8.GetString(bytes, 0, bytesRec);

                    message = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonData);



                    NM.RecieveMessage();

                    string username = tbStartupUsername.Text;
                    username = Regex.Replace(username, @" ", "");

                    List <string> connRequest = new List <string> {
                        username, this.tbStartupPassword.Password
                    };

                    message = new Dictionary <string, object>
                    {
                        { "username", "" },
                        { "channel", "" },
                        { "content", connRequest },
                        { "messagetype", "connRequest" }
                    };

                    List <object> encryptedMessage = EMAES.Encrypt(JsonConvert.SerializeObject(message));

                    jsonMessage = JsonConvert.SerializeObject(encryptedMessage);

                    NM.SendMessage(jsonMessage);
                }
            }
        }