예제 #1
0
 /// <summary>
 /// Decrypts all messages shown again
 /// </summary>
 public void Decrypt()
 {
     ObservableCollection<ListBoxItem> msgs = _viewModel.ChatMsgs;
     int i = 0;
     foreach (var row in msgs)
     {
         var panel = row.Content as StackPanel;
         if (panel != null)
             foreach (object child in panel.Children)
             {
                 if (child is TextBlock)
                 {
                     var temp = child as TextBlock;
                     if (_messages[i].Length > 13 && _messages[i].Substring(0, 13) == "------------ ")
                     {
                         continue;
                     }
                     switch (_viewModel.SelectedCypher)
                     {
                         case (int)Cyphers.SimpleEncryption:
                             var cypher1 = new SimpleEncryption();
                             temp.Text = _msgIds[i] + " : " + cypher1.DecryptMessage(_messages[i], _viewModel.Key);
                             break;
                         case (int)Cyphers.Rijndael:
                             var cypher2 = new Rijndael();
                             temp.Text = _msgIds[i] + " : " + cypher2.DecryptMessage(_messages[i], _viewModel.Key);
                             break;
                     }
                 }
             }
         i++;
     }
 }
예제 #2
0
        /// <summary>
        /// Handles message receiving and decryption of incoming messages.
        /// </summary>
        /// <param name="msg"></param>
        public void Receive(Message msg)
        {
            foreach (SharedComponents.Client c in _onlineClients.Values)
            {
                if (c.Name == msg.Sender)
                {
                    string output = "";
                    switch (_viewModel.SelectedCypher)
                    {
                        case (int)Cyphers.SimpleEncryption:
                            var cypher1 = new SimpleEncryption();
                            output = msg.Sender + " : " + cypher1.DecryptMessage(msg.Content, _viewModel.Key);
                            break;
                        case (int)Cyphers.Rijndael:
                            var cypher2 = new Rijndael();
                            output = msg.Sender + " : " + cypher2.DecryptMessage(msg.Content, _viewModel.Key);
                            break;
                    }
                    ListBoxItem item = MakeItem(c.AvatarID, output);
                    _viewModel.ChatMsgs.Add(item);
                    _messages.Add(msg.Content);
                    using (var terr = new ChatDataContainer())
                    {
                        var sender = terr.ChatUsersSet.Where(s => s.Username == msg.Sender).ToList();
                        var curUser = terr.ChatUsersSet.Where(s => s.Username == _viewModel.Name).ToList();
                        ChatData temp = new ChatData
                        {
                            Id = curUser[0].Id,
                            Sender = sender[0].Id,
                            Message = msg.Content,
                            RecTime = msg.Time,
                            ChatUsers = curUser[0]
                        };
                        _msgIds.Add(msg.Sender);
                        try
                        {
                            terr.ChatDataSet.Add(temp);
                            terr.SaveChanges();
                        }
                        catch (Exception e)
                        {
                            _viewModel.Name = e.ToString();
                        }

                    }
                }
            }
            _viewModel.ScrollDown();
        }
예제 #3
0
 /// <summary>
 /// Receives a whisper and decrypts it
 /// </summary>
 /// <param name="msg"></param>
 /// <param name="receiver"></param>
 public void ReceiveWhisper(Message msg, SharedComponents.Client receiver)
 {
     foreach (SharedComponents.Client c in _onlineClients.Values)
     {
         if (c.Name == msg.Sender)
         {
             string output = "";
             switch (_viewModel.SelectedCypher)
             {
                 case (int)Cyphers.SimpleEncryption:
                     var cypher1 = new SimpleEncryption();
                     output = cypher1.DecryptMessage(msg.Content, _viewModel.Key);
                     break;
                 case (int)Cyphers.Rijndael:
                     var cypher2 = new Rijndael();
                     output = cypher2.DecryptMessage(msg.Content, _viewModel.Key);
                     break;
             }
             ListBoxItem item = MakeItem(c.AvatarID,
                 msg.Sender + " whispers " + receiver.Name + " : " + output);
             _viewModel.ChatMsgs.Add(item);
             _messages.Add(msg.Content);
             _msgIds.Add(msg.Sender + " whispers " + receiver.Name);
         }
     }
     _viewModel.ScrollDown();
 }
예제 #4
0
 /// <summary>
 /// This method encrypts the message we want to send
 /// and sends it either to one recipient (whisper)
 /// or all online chat clients
 /// </summary>
 public void Send()
 {
     if (_channel != null && _viewModel.ChatInputText != "")
     {
         if (_channel.State == CommunicationState.Faulted)
         {
             HandleProxy();
         }
         else
         {
             //Create message, assign its properties
             //encrypt message
             string temp = "";
             switch (_viewModel.SelectedCypher)
             {
                 case (int)Cyphers.SimpleEncryption:
                     var cypher1 = new SimpleEncryption();
                     temp = cypher1.EncryptMessage(_viewModel.ChatInputText, _viewModel.Key);
                     break;
                 case (int)Cyphers.Rijndael:
                     var cypher2 = new Rijndael();
                     temp = cypher2.EncryptMessage(_viewModel.ChatInputText, _viewModel.Key);
                     break;
             }
             Message msg = new Message
             {
                 Sender = _localClient.Name,
                 Time = DateTime.Now,
                 Content = temp
             };
             //If whisper mode is checked and an item is
             //selected in the list box of clients, it will
             //arrange a client object called receiver
             //to whisper
             if (_viewModel.WhisperBox)
             {
                 if (_receiver != null)
                 {
                     _channel.WhisperAsync(msg, _receiver);
                     _viewModel.ChatInputText = "";
                 }
             }
             else
             {
                 _channel.SayAsync(msg);
                 _viewModel.ChatInputText = "";
             }
             //Tell the service to tell back all clients that this client
             //has just finished typing..
             _channel.IsWritingAsync(null);
         }
     }
 }