Пример #1
0
        /// <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)
            {
                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 = Cfg["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(' '));
                    }
                }
                // 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 (KeyUserInfo 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);
                }

                var encrypt = new GpgEncrypt(tmpFile, tmpFile2, false, false, null, recipients, CipherAlgorithm.None);
                var encResult = encrypt.Execute();
                this.EncryptCallback(encResult, tmpFile, tmpFile2, listFileView.SelectedItem.ToString());
            }
        }
Пример #2
0
        private void txtPassDetail_Leave(object sender, EventArgs e)
        {
            if (txtPassDetail.ReadOnly == false)
            {
                txtPassDetail.ReadOnly = false;
                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 = Properties.Settings.Default.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);
                    }
                }
                // match keyid
                List<GpgApi.KeyId> recipients = new List<KeyId>() { };
                foreach (var line in GPGRec)
                {
                    GpgListSecretKeys publicKeys = new GpgListSecretKeys();
                    publicKeys.Execute();
                    foreach (Key key in publicKeys.Keys)
                    {
                        if (key.UserInfos[0].Email == line.ToString())
                        {
                            recipients.Add(key.Id);
                        }
                    }
                }
                // 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;
        }
Пример #3
0
        /// <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 DecryptCallback(GpgInterfaceResult result, string tmpFile, string path)
        {
            if (result.Status == GpgInterfaceStatus.Success)
            {
                List<KeyId> recipients = new List<KeyId>();
                foreach (var line in listBox1.Items)
                {
                    GpgListSecretKeys publicKeys = new GpgListSecretKeys();
                    publicKeys.Execute();
                    recipients.AddRange(from key in publicKeys.Keys where key.UserInfos[0].Email == line.ToString() select key.Id);
                }

                string tmpFile2 = Path.GetTempFileName();
                GpgEncrypt encrypt = new GpgEncrypt(tmpFile, tmpFile2, false, false, null, recipients, CipherAlgorithm.None);
                GpgInterfaceResult encResult = encrypt.Execute();
                this.EncryptCallback(encResult, tmpFile, tmpFile2, path);
            }
            else
            {
                // shit happened
            }
        }
Пример #4
0
        public string encryptString(string toEncrypt, string target, string sign = null, CipherAlgorithm algorithm = CipherAlgorithm.Aes256, bool armour = true, bool hideuserid = false)
        {
            List<KeyId> recipients = new List<KeyId>();
            recipients.Add(new KeyId(target));

            KeyId signkey = new KeyId(defaultsign);

            GpgInterface.ExePath = ExePath;

            string path = Directory.GetCurrentDirectory() + "\\" + GetUniqueKey() + ".txt";
            string pathout = path + ".out";

            System.IO.File.WriteAllText(path, toEncrypt);

            GpgEncrypt encrypt = new GpgEncrypt(path, pathout, armour, hideuserid, signkey, recipients, algorithm);

            encrypt.AskPassphrase = GetPassword;

            GpgInterfaceResult result = encrypt.Execute();

            System.IO.File.Delete(path);

            if (result.Status == GpgInterfaceStatus.Success)
            {
                string toReturn = System.IO.File.ReadAllText(pathout);
                System.IO.File.Delete(pathout);
                return toReturn;
            }
            else
            {
                throw new Exception("Encryption Failed");
            }
        }
Пример #5
0
        /// <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;
        }