virtual protected void OnChatPromtCheck(ImReceivedMessageEventArgs e) { if (ChatPromtCheck != null) { ChatPromtCheck(this, e); } }
void HandleReceive() { ImReceivedMessageEventArgs args = null; while (ConnectionToServer.TcpClient.Connected) { byte responseFromServer = ConnectionToServer.BinaryReader.ReadByte(); switch (responseFromServer) { case IMCommands.Buddies: byte action = ConnectionToServer.BinaryReader.ReadByte(); switch (action) { case IMCommands.AddBuddy: string usernameToAdd = ConnectionToServer.BinaryReader.ReadString(); if (!_generatedClientList.ToList().Single(cli => cli.UserName == _clientInfo.UserName).Buddies.Contains(usernameToAdd)) { _generatedClientList.ToList().Single(cli => cli.UserName == _clientInfo.UserName).Buddies.Add(usernameToAdd); } break; case IMCommands.RemoveBuddy: string usernameToRemove = ConnectionToServer.BinaryReader.ReadString(); if (_generatedClientList.ToList().Single(cli => cli.UserName == _clientInfo.UserName).Buddies.Contains(usernameToRemove)) { _generatedClientList.ToList().Single(cli => cli.UserName == _clientInfo.UserName).Buddies.Remove(usernameToRemove); } break; } OnBuddyUpdate(new EventArgs()); break; case IMCommands.IM_RECEIVE: try { string from = ConnectionToServer.BinaryReader.ReadString(); string msg = ConnectionToServer.BinaryReader.ReadString(); int encryptionStandardFlag = ConnectionToServer.BinaryReader.ReadInt32(); int encryptionMethodFlag = ConnectionToServer.BinaryReader.ReadInt32(); bool syncFlag = ConnectionToServer.BinaryReader.ReadBoolean(); args = new ImReceivedMessageEventArgs(new Message(msg, "", from, encryptionStandardFlag, encryptionMethodFlag, syncFlag)); _messagesRecieved.Add(msg); if (GeneratedClientList.Count(cli => cli.UserName == from) == 0) { ConnectionToServer.BinaryWriter.Write(IMCommands.IM_CLIENT_LIST); // ask for list, although if we've made an attempt before they dont need to know what we're after } else { OnChatPromtCheck(args); args = null; //nullify them } } catch (Exception e) { e.GetType(); } break; case IMCommands.IM_CLIENT_LIST: try { int sizeOfListBeingSent = ConnectionToServer.BinaryReader.ReadInt32(); //read list count int lengthOfBytes = ConnectionToServer.BinaryReader.ReadInt32(); //read length of bytes incomming byte[] incommingList = ConnectionToServer.SslStream.ReadStreamTillEnd(lengthOfBytes); //read them into byte array _unbindedClientList = (List <IMLibrary.Client>)incommingList.ByteStreamToObject(typeof(List <IMLibrary.Client>)); //this event populates the the list view with the new clients OnNewList(new ImListReceivedFromServerArgs(_unbindedClientList)); //ConnectionToServer.BinaryWriter.Write(sizeOfListBeingSent == unbindedClientList.Count ? IMCommands.IM_CORRECT : IMCommands.ERROR_INCORRECT); //thise event invokes new negotiations with the new clients, serperate events so no network streams are crossed. OnNegotiation(new InitiateNegotiationEventArgs(_unbindedClientList)); if (args != null) { OnChatPromtCheck(args); //if when we are receiving, if the client sender wasnt on the list we get a new list an then use the args args = null; //nullify args } } catch (Exception e) { e.GetType(); } break; case IMCommands.NegotiateKeys: try { int tupleLength = ConnectionToServer.BinaryReader.ReadInt32(); byte[] tupleBytes = ConnectionToServer.BinaryReader.ReadBytes(tupleLength); Tuple <byte[], byte[]> encrypttedKeyVectorCombo = (Tuple <byte[], byte[]>)tupleBytes.ByteStreamToObject(typeof(Tuple <byte[], byte[]>)); string sender = ConnectionToServer.BinaryReader.ReadString(); OnKeyNegotiation(new KeyNegotiationEventArgs(encrypttedKeyVectorCombo, sender)); ConnectionToServer.BinaryWriter.Write(IMCommands.IM_CLIENT_LIST); // ask for list, although if we've made an attempt before they dont need to know what we're after } catch (Exception e) { e.GetType(); } break; } } }
private void MessageReceiveHandler(object obj, ImReceivedMessageEventArgs e) { Application.Current.Dispatcher.Invoke(new Action(() => { if (this._clientPromtList.Find(cli => cli.Item1.UserName == e.Message.From) == null) { //if we hit here, a client that is not on the list has sent us a messsage, we could resync with the list. } string decryptedMessage = ""; if (!gridBuddies.Children.Contains(_clientPromtList.Find(cli => cli.Item1.UserName == e.Message.From).Item2)) { GeneratedClientList.ToList().Find(cli => cli.UserName == e.Message.From).NewMessages = true; GeneratedClientList.ToList().Find(cli => cli.UserName == e.Message.From).Received++; } try { if (_clientKeyIvDictionary.ContainsKey(e.Message.From)) { int selectedEncryptionMethod = e.Message.SyncFlag ? e.Message.EncryptionMethodFlag : _clientPromtList.Find(cli => cli.Item1.UserName == e.Message.From).Item2.EncryptionMethod.SelectedIndex; _clientPromtList.Find(cli => cli.Item1.UserName == e.Message.From).Item2.EncryptionMethod.SelectedIndex = selectedEncryptionMethod; switch (selectedEncryptionMethod) { case 0: //symmetrical decryption int selectedStandard = e.Message.SyncFlag ? e.Message.EncryptionStandardFlag : _clientPromtList.Find(cli => cli.Item1.UserName == e.Message.From).Item2.EncryptionStandard.SelectedIndex; _clientPromtList.Find(cli => cli.Item1.UserName == e.Message.From).Item2.EncryptionStandard.SelectedIndex = selectedStandard; switch (selectedStandard) { case 0: decryptedMessage = Cryptography.Decrypt <AesManaged>(e.Message.MessageSent, _clientKeyIvDictionary[e.Message.From].Item1, Encoding.Unicode.GetString(_clientKeyIvDictionary[e.Message.From].Item2)); break; case 1: decryptedMessage = Cryptography.Decrypt <TripleDESCryptoServiceProvider>(e.Message.MessageSent, _clientKeyIvDictionary[e.Message.From].Item1, Encoding.Unicode.GetString(_clientKeyIvDictionary[e.Message.From].Item2)); break; //case 2: // decryptedMessage = Cryptography.Decrypt<RC2CryptoServiceProvider>(e.Message.MessageSent, _clientKeyIvDictionary[e.Message.From].Item1, Encoding.Unicode.GetString(_clientKeyIvDictionary[e.Message.From].Item2)); // break; } break; case 1: //one-time-pad decryption if (_clientPromtList.Find(cli => cli.Item1.UserName == e.Message.From).Item2.txtOneTimePad.Text.Length >= e.Message.MessageSent.Length) { decryptedMessage = Cryptography.DecryptMessageWithPad(_clientPromtList.Find(cli => cli.Item1.UserName == e.Message.From).Item2.txtOneTimePad.Text, e.Message.MessageSent); } break; } } else { decryptedMessage = Cryptography.DecryptData(e.Message.MessageSent); } _clientPromtList.Find(cli => cli.Item1.UserName == e.Message.From).Item2.RecieveText.AppendNewLine(String.Format("[{0}] >> {1}", DateTime.Now.ToString("HH:mm:ss"), decryptedMessage)); } catch (Exception ex) { _clientPromtList.Find(cli => cli.Item1.UserName == e.Message.From).Item2.RecieveText.AppendNewLine(String.Format("[{0}] >> {1}", DateTime.Now.ToString("HH:mm:ss"), "Could not decrypt incomming message.")); } })); }