void OnPostMsgButtonClick(object sender, RoutedEventArgs e) { User toUser; bool queryResult = User.AllUsers.TryGetValue(toTextBox.Text, out toUser); if (!queryResult) { ExclamationMsgBox("User does not exist", "User does not exist"); return; } using (X509Certificate2 toUserCert = CryptoService.FindCertificate(toUser)) { if (toUserCert == null) { ExclamationMsgBox($"Can not find certificate for user: \"{toUser.FullName}\"", "Can not find certificate"); return; } using (RSA toUserPublicKey = toUserCert.GetRSAPublicKey()) { // No rsa public key or cert not valid if (toUserPublicKey == null || !CryptoService.ValidateCertificate(toUserCert)) { ExclamationMsgBox($"Certificate not valid for user: \"{toUser.FullName}\"", "Certificate not valid"); return; } byte[] signature; byte[] data; try { using (RSA userPrivateKey = PrivateKeyDialog()) { //Null means we cancel if (userPrivateKey == null) { return; } string formattedMsg = Message.MakeXml(currentUser, msgTextBox.Text); data = Encoding.UTF8.GetBytes(formattedMsg); signature = CryptoService.SignData(data, userPrivateKey); } } catch (FileFormatException) { ExclamationMsgBox("Invalid pem file", "Invalid pem file"); return; } byte[] symmKey = CryptoService.GenerateSymmetricKey(); byte[] iV = CryptoService.GenerateIV(); byte[] envelope = CryptoService.EncryptSymmetricData(symmKey, iV, toUserPublicKey); byte[] dataAndSign = Utility.CombineByteArrays(data, signature); byte[] encData = CryptoService.EncryptData(dataAndSign, symmKey, iV); // Zero out the symmetric key Array.Clear(symmKey, 0, symmKey.Length); byte[] payload = Utility.CombineByteArrays(envelope, encData); string imagePath = imageTextBox.Text; try { Steganography.Embed(imagePath, imagePath, payload); } //TODO: Implement separate exception for small image catch (FileFormatException) { MessageBox.Show("Image is too small for given message", "Small image", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } string hash = CryptoService.HashFile(imagePath); UnreadList list = new UnreadList(toUser.UnreadFile); list.Add(imagePath, hash); list.Write(toUser.UnreadFile); MessageBox.Show("Message post succesful", "Success", MessageBoxButton.OK); } } }