/// <summary> /// Callback from recrypt, ensures the file is encrypted again with the current keys /// </summary> /// <param name="result"></param> /// <param name="tmpFile"></param> /// <param name="path"></param> private void Decrypt_Callback(GpgInterfaceResult result, string tmpFile, string path) { if (result.Status == GpgInterfaceStatus.Success) { List <GpgApi.KeyId> recipients = new List <KeyId>() { }; foreach (var line in listBox1.Items) { GpgListSecretKeys publicKeys = new GpgListSecretKeys(); publicKeys.Execute(); foreach (Key key in publicKeys.Keys) { if (key.UserInfos[0].Email == line.ToString()) { recipients.Add(key.Id); } } } string tmpFile2 = Path.GetTempFileName(); GpgEncrypt encrypt = new GpgEncrypt(tmpFile, tmpFile2, false, false, null, recipients, GpgApi.CipherAlgorithm.None); GpgInterfaceResult enc_result = encrypt.Execute(); Encrypt_Callback(enc_result, tmpFile, tmpFile2, path); } else { // shit happened } }
/// <summary> /// Cleans up and reencrypts after an edit /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TxtPassDetailLeave(object sender, EventArgs e) { if (txtPassDetail.ReadOnly == false) { HTMLShowDetails.Visible = false; txtPassDetail.ReadOnly = true; txtPassDetail.Visible = false; btnMakeVisible.Visible = true; txtPassDetail.BackColor = Color.LightGray; // read .gpg-id var gpgfile = Path.GetDirectoryName(listFileView.SelectedItem.ToString()); gpgfile += "\\.gpg-id"; // check if .gpg-id exists otherwise get the root .gpg-id if (!File.Exists(gpgfile)) { gpgfile = _config["PassDirectory"]; gpgfile += "\\.gpg-id"; } var gpgRec = new List <string>(); using (var r = new StreamReader(gpgfile)) { string line; while ((line = r.ReadLine()) != null) { gpgRec.Add(line.TrimEnd(' ')); } } log.Debug("Matching GPG Keys"); // match keyid var recipients = new List <KeyId>(); foreach (var line in gpgRec) { var gotTheKey = false; var email = new MailAddress(line); var publicKeys = new GpgListPublicKeys(); publicKeys.Execute(); foreach (var key in publicKeys.Keys) { foreach (var t in key.UserInfos) { if (t.Email == email.Address) { recipients.Add(key.Id); gotTheKey = true; } } } if (!gotTheKey) { MessageBox.Show( Strings.Error_key_missing_part1 + line + @" " + Strings.Error_key_missing_part2, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // encrypt var tmpFile = Path.GetTempFileName(); var tmpFile2 = Path.GetTempFileName(); using (var w = new StreamWriter(tmpFile)) { w.Write(txtPassDetail.Text); } log.Debug("Begin encryption of: " + dirTreeView.SelectedNode.Tag + "\\" + listFileView.SelectedItem); var encrypt = new GpgEncrypt(tmpFile, tmpFile2, false, false, null, recipients, CipherAlgorithm.None); var encResult = encrypt.Execute(); this.EncryptCallback(encResult, tmpFile, tmpFile2, dirTreeView.SelectedNode.Tag + "\\" + listFileView.SelectedItem + ".gpg"); } }
/// <summary> /// Cleans up and reencrypts after an edit /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtPassDetail_Leave(object sender, EventArgs e) { if (txtPassDetail.ReadOnly == false) { txtPassDetail.ReadOnly = true; txtPassDetail.Visible = false; btnMakeVisible.Visible = true; txtPassDetail.BackColor = Color.LightGray; // read .gpg-id string gpgfile = Path.GetDirectoryName(dataPass.Rows[dataPass.CurrentCell.RowIndex].Cells[0].Value.ToString()); gpgfile += "\\.gpg-id"; // check if .gpg-id exists otherwise get the root .gpg-id if (!File.Exists(gpgfile)) { gpgfile = cfg["PassDirectory"]; gpgfile += "\\.gpg-id"; } List <string> GPGRec = new List <string>() { }; using (StreamReader r = new StreamReader(gpgfile)) { string line; while ((line = r.ReadLine()) != null) { GPGRec.Add(line.TrimEnd(' ')); } } // match keyid List <GpgApi.KeyId> recipients = new List <KeyId>() { }; foreach (var line in GPGRec) { bool GotTheKey = false; MailAddress email = new MailAddress(line.ToString()); GpgListPublicKeys publicKeys = new GpgListPublicKeys(); publicKeys.Execute(); foreach (Key key in publicKeys.Keys) { for (int i = 0; i < key.UserInfos.Count; i++) { if (key.UserInfos[i].Email == email.Address) { recipients.Add(key.Id); GotTheKey = true; } } } if (!GotTheKey) { MessageBox.Show(Strings.Error_key_missing_part1 + line.ToString() + " " + Strings.Error_key_missing_part2, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // encrypt string tmpFile = Path.GetTempFileName(); string tmpFile2 = Path.GetTempFileName(); using (StreamWriter w = new StreamWriter(tmpFile)) { w.Write(txtPassDetail.Text); } GpgEncrypt encrypt = new GpgEncrypt(tmpFile, tmpFile2, false, false, null, recipients, GpgApi.CipherAlgorithm.None); GpgInterfaceResult enc_result = encrypt.Execute(); Encrypt_Callback(enc_result, tmpFile, tmpFile2, dataPass.Rows[dataPass.CurrentCell.RowIndex].Cells[0].Value.ToString()); } dataPass.Enabled = true; }