/// <summary> /// Callback for the decrypt thread /// </summary> /// <param name="result"></param> /// <param name="clear"></param> private void DecryptCallback(GpgInterfaceResult result, bool clear) { if (result.Status == GpgInterfaceStatus.Success) { txtPassDetail.Text = File.ReadAllText(this.tmpfile); ShowHTML(txtPassDetail.Text); File.Delete(this.tmpfile); // copy to clipboard if (txtPassDetail.Text != "" && !txtPassDetail.Text.StartsWith("\n")) { Clipboard.SetText(new string(txtPassDetail.Text.TakeWhile(c => c != '\n').ToArray())); if (clear) { // set progressbar as notification statusPB.Maximum = 45; statusPB.Value = 0; statusPB.Step = 1; statusPB.Visible = true; statusTxt.Text = Strings.Statusbar_countdown + @" "; //Create the timer clipboardTimer = new Timer(ClearClipboard, null, 0, 1000); log.Debug("Decryption finished"); } } } else { txtPassDetail.Text = Strings.Error_weird_shit_happened; ShowHTML(txtPassDetail.Text); } }
/// <summary> /// Callback for the encrypt thread /// </summary> /// <param name="result"></param> /// <param name="tmpFile"></param> /// <param name="tmpFile2"></param> /// <param name="path"></param> public void Encrypt_Callback(GpgInterfaceResult result, string tmpFile, string tmpFile2, string path) { if (result.Status == GpgInterfaceStatus.Success) { File.Delete(tmpFile); File.Delete(path); File.Move(tmpFile2, path); // add to git using (var repo = new LibGit2Sharp.Repository(cfg["PassDirectory"])) { // Stage the file repo.Stage(path); // Commit repo.Commit("password changes", new LibGit2Sharp.Signature("pass4win", "pass4win", System.DateTimeOffset.Now), new LibGit2Sharp.Signature("pass4win", "pass4win", System.DateTimeOffset.Now)); if (cfg["UseGitRemote"] == true && GITRepoOffline == false) { toolStripOffline.Visible = false; var remote = repo.Network.Remotes["origin"]; var options = new PushOptions(); options.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = cfg["GitUser"], Password = DecryptConfig(cfg["GitPass"], "pass4win") }; var pushRefSpec = @"refs/heads/master"; repo.Network.Push(remote, pushRefSpec, options); } } } else { MessageBox.Show(Strings.Error_weird_shit_happened_encryption, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <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> /// Callback from Decrypt_Callback, ensures the tmp files are deleted and the file get's the correct name /// </summary> /// <param name="result"></param> /// <param name="tmpFile"></param> /// <param name="tmpFile2"></param> /// <param name="path"></param> public void EncryptCallback(GpgInterfaceResult result, string tmpFile, string tmpFile2, string path) { if (result.Status == GpgInterfaceStatus.Success) { File.Delete(tmpFile); File.Delete(path); File.Move(tmpFile2, path); } }
/// <summary> /// Fires a decrypt thread with a callback the encrypts it again, used to make the keys current /// </summary> /// <param name="path"></param> private void recrypt(string path) { string tmpFile = Path.GetTempFileName(); GpgDecrypt decrypt = new GpgDecrypt(path, tmpFile); { // The current thread is blocked until the decryption is finished. GpgInterfaceResult result = decrypt.Execute(); Decrypt_Callback(result, tmpFile, path); } }
/// <summary> /// Decrypt the file into a tempfile /// </summary> /// <param name="path"></param> /// <param name="clear"></param> private void decrypt_pass(string path, bool clear = true) { FileInfo f = new FileInfo(path); if (f.Length > 0) { tmpfile = Path.GetTempFileName(); GpgDecrypt decrypt = new GpgDecrypt(path, tmpfile); { // The current thread is blocked until the decryption is finished. GpgInterfaceResult result = decrypt.Execute(); Decrypt_Callback(result, clear); } } }
/// <summary> /// Callback for the encrypt thread /// </summary> /// <param name="result"></param> /// <param name="tmpFile"></param> /// <param name="tmpFile2"></param> /// <param name="path"></param> public void EncryptCallback(GpgInterfaceResult result, string tmpFile, string tmpFile2, string path) { if (result.Status == GpgInterfaceStatus.Success) { File.Delete(tmpFile); File.Delete(path); File.Move(tmpFile2, path); // add to git if (!Program.NoGit) { if (!GitRepo.Commit(path)) { MessageBox.Show( Strings.FrmMain_EncryptCallback_Commit_failed_, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } // Check if we want to sync with remote if (this.gitRepoOffline) { return; } // sync with remote if (!Program.NoGit) { if (!GitRepo.Push(_config["GitUser"], DecryptConfig(_config["GitPass"], "pass4win"))) { MessageBox.Show( Strings.FrmMain_EncryptCallback_Push_to_remote_repo_failed_, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } } log.Debug(() => "Encryption finished"); } }
/// <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; }