private void NegotiateKey(ConnectedClient selectedClient) { var synMessage = new EncMessage() { Sender = new ConnectedClient() { Id = _userId, Username = _username }, Recipient = selectedClient, UsedForDHExchange = true, Payload = new byte[1] { 0x34 } }; _serverChannel.SendMessage(synMessage); byte[] rndBytes = new byte[32]; RNGCryptoServiceProvider.Create().GetBytes(rndBytes); privateKey = Curve25519.ClampPrivateKey(rndBytes); byte[] publicKey = Curve25519.GetPublicKey(privateKey); synMessage.Payload = publicKey; _serverChannel.SendMessage(synMessage); }
private void DisplayNewMessage(EncMessage message) { if (!message.UsedForDHExchange) { // TODO: Here we will decrypt the message and show it using (Aes aes = Aes.Create()) { this.messagesListBox.Items.Add($"{message.Sender.Username}> " + $"{AesEncryption.DecryptStringFromBytes(message.Payload, _clientsPrivateKeys[message.Sender.Username], new byte[16])}"); } } else { if (message.Payload[0] == 0x34 && message.Payload.Length == 1) { byte[] rndBytes = new byte[32]; RNGCryptoServiceProvider.Create().GetBytes(rndBytes); privateKey = Curve25519.ClampPrivateKey(rndBytes); byte[] publicKey = Curve25519.GetPublicKey(privateKey); var pubKeyMessage = new EncMessage() { Recipient = message.Sender, Sender = message.Recipient, UsedForDHExchange = true, Payload = publicKey }; _serverChannel.SendMessage(pubKeyMessage); } else { if (!_clientsPrivateKeys.ContainsKey(message.Sender.Username)) { _clientsPrivateKeys[message.Sender.Username] = Curve25519.GetSharedSecret(privateKey, message.Payload); } } } }
private void sendButton_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(messageTextBox.Text) || (messageTextBox.Text == "Write a message")) { MessageBox.Show("Please enter a message first.", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } var selectedClientRecipient = clientsListBox.SelectedItem as ConnectedClient; if (selectedClientRecipient == null) { MessageBox.Show("Please select a recipient first.", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } // TODO: Here we will encrypt the message using (Aes aes = Aes.Create()) { var encryptedMessage = new EncMessage() { UsedForDHExchange = false, Payload = AesEncryption.EncryptStringToBytes(messageTextBox.Text, _clientsPrivateKeys[selectedClientRecipient.Username], new byte[16]), Recipient = selectedClientRecipient, Sender = new ConnectedClient() { Id = _userId, Username = _username } }; _serverChannel.SendMessage(encryptedMessage); } messagesListBox.Items.Add($"Me> {messageTextBox.Text}"); messageTextBox.Clear(); }