/// <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(); }
/// <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++; } }
/// <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(); }