private void MenuItemWithRadioButtons_Click(object sender, RoutedEventArgs e) { Fluent.MenuItem oMenu = (Fluent.MenuItem)sender; User oUser = (User)oMenu.DataContext; bool bIsInList = UserListSelected.Contains(oUser); if (bIsInList) { UserListSelected.Remove(oUser); } else { UserListSelected.Add(oUser); } oMenu.IsChecked = !bIsInList; }
private void oSaveItemButton_Click(object sender, RoutedEventArgs e) { // perform data validation if in text mode and option is set if (ThisItem.ItemType.Equals("text") && !String.IsNullOrWhiteSpace(Properties.Settings.Default.ItemTextExpressionFilter)) { if (!Regex.Match(oItemData.Text, Properties.Settings.Default.ItemTextExpressionFilter).Success) { // note to the user that the data was invalid MessageBox.Show(this, "The item text provided does not satifsy the content filter.", "Invalid Item Text", MessageBoxButton.OK, MessageBoxImage.Error); return; } } // update the entity using the local copy we have using (CryptureEntities oContent = new CryptureEntities()) { oContent.Entry(ThisItem).State = (ThisItem.CreatedDate == DateTime.MinValue) ? EntityState.Added : EntityState.Modified; // verify the selected users foreach (User oUser in UserListSelected.ToArray()) { using (X509Certificate2 oCert = new X509Certificate2(oUser.Certificate)) { if (CertificateOperations.CheckCertificateStatus(oCert) == false && MessageBox.Show(this, "The certificate for '" + oUser.Name + "' cannot be verified. " + "Should this certificate be removed from the list?", "Cannot Verify Certificate", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { // remove from list and force refresh UserListSelected.Remove(oUser); oAddCertDropDown.Items.Refresh(); } } } // error if there are no selected users if (UserListSelected.Count == 0) { MessageBox.Show(this, "This certificate share list is empty and cannot be saved.", "Empty Certificates List", MessageBoxButton.OK, MessageBoxImage.Question); return; } using (Aes oCng = AesCng.Create()) { // create new cipher object and associate it with this id ThisItem.Cipher = new Cipher(); ThisItem.Cipher.Item = ThisItem; using (MemoryStream oMemory = new MemoryStream()) using (CryptoStream oCrypto = new CryptoStream( oMemory, oCng.CreateEncryptor(), CryptoStreamMode.Write)) { byte[] oPlainByte = ThisItem.ItemType.Equals("text") ? Encoding.Unicode.GetBytes(oItemData.Text) : BinaryItemData; oCrypto.Write(oPlainByte, 0, oPlainByte.Length); oCrypto.FlushFinalBlock(); ThisItem.Cipher.CipherText = oMemory.ToArray(); } ThisItem.Cipher.CipherVector = oCng.IV; ThisItem.CreatedDate = DateTime.Now; ThisItem.ModifiedDate = DateTime.Now; // clear out any existing instances oContent.Instances.RemoveRange(ThisItem.Instances); // encode each instance foreach (User oUser in UserListSelected) { Instance oInstance = new Instance(); oInstance.Signature = new byte[] { }; oInstance.UserId = oUser.UserId; oInstance.ItemId = ThisItem.ItemId; byte[] oCipherByte = null; using (X509Certificate2 oCert = new X509Certificate2(oUser.Certificate)) { // always attempt to use next generation classes first before // resorting to using legacy crytographic classes try { using (RSA oRSA = oCert.GetRSAPublicKey()) { oCipherByte = oRSA.Encrypt(oCng.Key, RSAEncryptionPadding.Pkcs1); } } catch (CryptographicException) { using (RSACryptoServiceProvider oRSA = oCert.PublicKey.Key as RSACryptoServiceProvider) { oCipherByte = oRSA.Encrypt(oCng.Key, false); } } } oInstance.CipherKey = oCipherByte; ThisItem.Instances.Add(oInstance); } } // commit changes to database oContent.SaveChanges(); } // close and return to calling dialog Close(); }