private void okButton_Click(object sender, EventArgs e)
        {
            if (nameTextBox.Text.Length < 1)
            {
                MessageBox.Show("Name cannot be blank.", "Missing Credential Name");
            }
            else if (usernameTextBox.Text.Length < 1)
            {
                MessageBox.Show("Username cannot be blank.", "Missing Credential Username");
            }
            else if (passwordTextBox.Text.Length < 1)
            {
                MessageBox.Show("Password cannot be blank.", "Missing Credential Password");
            }
            else
            {
                if (MessageBox.Show("Save changes to this credential?", "Save Credential Changes", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    database = new SQLiteDatabase();

                    internetCredential.name        = nameTextBox.Text;
                    internetCredential.username    = CredentialEncryption.EncryptCredential(usernameTextBox.Text);
                    internetCredential.password    = CredentialEncryption.EncryptCredential(passwordTextBox.Text);
                    internetCredential.url         = urlTextBox.Text;
                    internetCredential.description = descriptionTextBox.Text;

                    database.UpdateInternetCredential(internetCredential, internetCredential.id);

                    MessageBox.Show("New credential information saved.", "Changes Saved");
                    // Bypass the form closing event since the user already pressed the ok button
                    userAlreadyClosing = true;
                    this.Close();
                }
            }
        }
        private void MainForm_Activated(object sender, EventArgs e)
        {
            Application.Idle += new EventHandler(Application_Idle);

            // Place the generated password into the password field when the users confirms copying it in the password generator
            if (SharedObject.generatedPassword.Length > 0)
            {
                passwordTextBox.Text           = SharedObject.generatedPassword;
                SharedObject.generatedPassword = "";

                internetCredential.id          = Convert.ToInt32(idLabel.Text);
                internetCredential.name        = nameTextBox.Text;
                internetCredential.username    = CredentialEncryption.EncryptCredential(usernameTextBox.Text);
                internetCredential.password    = CredentialEncryption.EncryptCredential(passwordTextBox.Text);
                internetCredential.url         = urlTextBox.Text;
                internetCredential.description = descriptionTextBox.Text;

                database.UpdateInternetCredential(internetCredential, internetCredential.id);
            }
            else if (SharedObject.newCredentialAdded)
            {
                SharedObject.newCredentialAdded = false;

                // Depopulate treeview and then repopuate it with the new record
                categoryTreeView.Nodes[0].Nodes.Clear();
                allNames = database.SelectCredentialNames();
                if (allNames != null)
                {
                    foreach (string name in allNames)
                    {
                        categoryTreeView.Nodes[0].Nodes.Add(name);
                    }
                    categoryTreeView.ExpandAll();
                }

                // Show the recently added record to the user
                internetCredential = database.SelectInternetCredential(database.CountCredentials());
                if (internetCredential != null)
                {
                    if (internetCredential.name != null)
                    {
                        idLabel.Text            = internetCredential.id.ToString();
                        nameTextBox.Text        = internetCredential.name;
                        usernameTextBox.Text    = CredentialEncryption.DecryptCredential(internetCredential.username);
                        passwordTextBox.Text    = CredentialEncryption.DecryptCredential(internetCredential.password);
                        urlTextBox.Text         = internetCredential.url;
                        descriptionTextBox.Text = internetCredential.description;
                    }
                }
            }
        }
        private void okButton_Click(object sender, EventArgs e)
        {
            if (nameTextBox.Text.Length < 1)
            {
                MessageBox.Show("Name cannot be blank.", "Missing Credential Name");
            }
            else if (usernameTextBox.Text.Length < 1)
            {
                MessageBox.Show("Username cannot be blank.", "Missing Credential Username");
            }
            else if (passwordTextBox.Text.Length < 1)
            {
                MessageBox.Show("Password cannot be blank.", "Missing Credential Password");
            }
            else
            {
                if (MessageBox.Show("Add this credential to the database?", "Add New Credential", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    internetCredential = new Credential();
                    database           = new SQLiteDatabase();
                    encryptCredential  = new CredentialEncryption();

                    internetCredential.name        = nameTextBox.Text;
                    internetCredential.username    = CredentialEncryption.EncryptCredential(usernameTextBox.Text);
                    internetCredential.password    = CredentialEncryption.EncryptCredential(passwordTextBox.Text);
                    internetCredential.url         = urlTextBox.Text;
                    internetCredential.description = descriptionTextBox.Text;

                    database.InsertInternetCredential(internetCredential);

                    MessageBox.Show("Credential added to the database.", "Credential Added");
                    SharedObject.newCredentialAdded = true;
                    // Bypass the form closing event since the user already pressed the ok button
                    userAlreadyClosing = true;
                    this.Close();
                }
            }
        }
        private void okButton_Click(object sender, EventArgs e)
        {
            if (masterEntryTextBox.Text.Trim().Equals("") || masterConfirmTextBox.Text.Trim().Equals(""))
            {
                MessageBox.Show("Password fields cannot be blank.", "Missing Password");
            }
            else
            {
                if (masterEntryTextBox.Text.Equals(masterConfirmTextBox.Text))
                {
                    database = new SQLiteDatabase();

                    credentials = database.SelectAllSecureCredentials();

                    // Decrypt all the usernames and passwords using the old key
                    for (int i = 0; i < credentials.Count; i++)
                    {
                        credentials[i].username = CredentialEncryption.DecryptCredential(credentials[i].username);
                        credentials[i].password = CredentialEncryption.DecryptCredential(credentials[i].password);
                    }

                    string hashString;
                    if (Properties.Settings.Default.UseSHA3Hashing)
                    {
                        IHash      hash       = HashFactory.Crypto.SHA3.CreateKeccak512();
                        HashResult hashResult = hash.ComputeString(masterEntryTextBox.Text);

                        hashString = hashResult.ToString();
                    }
                    else
                    {
                        hashString = PasswordHash.CreateHash(masterEntryTextBox.Text);
                    }

                    database.UpdateHash(hashString);

                    // Update salt?

                    // Put user's password in a secure string for later use
                    SharedObject.encryptedPassword.Clear();
                    foreach (char c in masterEntryTextBox.Text)
                    {
                        SharedObject.encryptedPassword.AppendChar(c);
                    }

                    // Re-encrypt all the usernames and password using the new key
                    CredentialEncryption.DeriveKey();
                    for (int i = 0; i < credentials.Count; i++)
                    {
                        credentials[i].username = CredentialEncryption.EncryptCredential(credentials[i].username);
                        credentials[i].password = CredentialEncryption.EncryptCredential(credentials[i].password);

                        database.UpdateSecureCredential(credentials[i], credentials[i].id);
                    }

                    // SharedObject.passwordGood = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Passwords do not match.", "Mismatched Passwords");
                }
            }
        }