private void btnExportPubKey_Click(object sender, EventArgs e)
        {
            if (lwKeys.SelectedItems.Count < 1)
                return;

            string privKeyFilename = tbSettingsPrivateKeyFolder.Text + Path.DirectorySeparatorChar + lwKeys.SelectedItems[0].Text + ".privkey";
            if (!File.Exists(privKeyFilename))
            {
                LogLine("Private key " + privKeyFilename + " does not exist");
                return;
            }            

            FormPass form = new FormPass();
            if (form.ShowDialog() != DialogResult.OK)
                return;

            string password = form.Password;
            DigitalSignature sig = new DigitalSignature();
            try
            {
                sig.LoadKeys(privKeyFilename, password);
            }
            catch (DigitalSignatureException ex)
            {
                LogLine(ex.Message);
            }
            catch (Exception ex)
            {
                LogLine("LoadKeys: An error has occurred. " + ex.Message);
                return;
            }            

            SaveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            SaveFileDialog.Filter = "Public keys | *.pubkey";
            if (SaveFileDialog.ShowDialog() == DialogResult.OK)            
                sig.SavePublicKey(SaveFileDialog.FileName);            
        }
        private void MenuItemSignFile_Click(object sender, EventArgs e)
        {            
            if (lwSign.SelectedItems.Count < 1)
            {
                LogLine("You must select a key");
                return;
            }

            string privKeyFilename = tbSettingsPrivateKeyFolder.Text + Path.DirectorySeparatorChar + lwSign.SelectedItems[0].Text + ".privkey";
            string pubKeyFilename = tbSettingsPublicKeyFolder.Text + Path.DirectorySeparatorChar + lwSign.SelectedItems[0].Text + ".pubkey";

            if (!File.Exists(privKeyFilename))
            {
                LogLine("Private key " + privKeyFilename + " does not exist");
                return;
            }

            if (!File.Exists(pubKeyFilename))
            {
                LogLine("Public key " + pubKeyFilename + " does not exist");
                return;
            }

            FormPass form = new FormPass();
            if (form.ShowDialog() != DialogResult.OK)
                return;

            string password = form.Password;
            DigitalSignature sig = new DigitalSignature();
            try
            {
                sig.LoadKeys(privKeyFilename, password);
            }            
            catch(Exception ex)
            {                
                LogLine("Invalid password");                
                return;
            }            

            FileDialog.Multiselect = true;
            FileDialog.Title = "Select files for signing";
            if (FileDialog.ShowDialog() != DialogResult.OK)
                return;

            Enabled = false;            
            LogLine("Signing files. Please wait...");
            Application.DoEvents();

            foreach (string filename in FileDialog.FileNames)
            {                
                byte[] bytes = File.ReadAllBytes(filename);
                var sha256 = SHA256.Create();
                var hashedData = sha256.ComputeHash(bytes);

                var signature = sig.SignData(hashedData);
                string signatureFilename = filename + ".signature";
                sig.SaveSignature(signatureFilename, signature);                
            }
            
            string dirName = "";
            if(FileDialog.FileNames.Length > 0)
                dirName = Path.GetDirectoryName(FileDialog.FileNames[0]);
            LogLine("Signatures created in directory " + dirName);

            Enabled = true;            
        }
        private void MenuItemVerifyFile_Click(object sender, EventArgs e)
        {                        
            if (lwVerify.SelectedItems.Count < 1)
            {
                LogLine("You must select a key");
                return;
            }

            string pubKeyFilename = tbSettingsPublicKeyFolder.Text + Path.DirectorySeparatorChar + lwVerify.SelectedItems[0].Text + ".pubkey";
            if (!File.Exists(pubKeyFilename))
            {
                LogLine("Public key " + pubKeyFilename + " does not exist");
                return;
            }

            DigitalSignature sig = new DigitalSignature();
            sig.LoadPublicKey(pubKeyFilename);

            FileDialog.Multiselect = false;
            FileDialog.Title = "Select file for verification";
            if (FileDialog.ShowDialog() != DialogResult.OK)
                return;

            string dataFilename = FileDialog.FileName;
            string sigFilename = dataFilename + ".signature";
            if (!File.Exists(sigFilename))
            {
                LogLine("Signature file " + sigFilename  + " does not exist");
                return;
            }

            Enabled = false;
            LogLine("Verifying file " + dataFilename + ". Please wait...");
            Application.DoEvents();

            byte[] signature = null;
            bool verified = false;

            try
            {
                signature = sig.LoadSignature(sigFilename);

                byte[] bytes = File.ReadAllBytes(dataFilename);
                var sha256 = SHA256.Create();

                var hashedData = sha256.ComputeHash(bytes);
                verified = sig.VerifySignature(hashedData, signature);
            }
            catch (DigitalSignatureException ex)
            {
                LogLine(ex.Message);                
                return;
            }
            catch (Exception ex)
            {
                LogLine("Verify error: " + ex.Message);                
                return;
            }
            finally
            {
                Enabled = true;
            }                        

            if (verified)
                LogLine("Verify ok");
            else
                LogLine("Verify failed");                                    
        }
        private void MenuItemNewKey_Click(object sender, EventArgs e)
        {            
            FormName form = new FormName();
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string basePrivKeyName = tbSettingsPrivateKeyFolder.Text + Path.DirectorySeparatorChar + form.NewName + ".privkey";
                string basePubKeyName = tbSettingsPublicKeyFolder.Text + Path.DirectorySeparatorChar + form.NewName + ".pubkey";
                if (File.Exists(basePrivKeyName))
                {
                    LogLine("A private key named " + form.NewName + " already exists");
                    return;
                }

                if (File.Exists(basePubKeyName))
                {
                    LogLine("A public key named " + form.NewName + " already exists");
                    return;
                }

                Enabled = false;            
                LogLine("Generating new key. Please wait...");                
                Application.DoEvents();

                DigitalSignature sig = new DigitalSignature();
                sig.CreateNewKey();
                LogLine("Storing new key as files...");
                sig.SaveKeys(basePrivKeyName, basePubKeyName, form.NewPassword);

                PopulatePrivateKeyList(ref lwKeys);
                LogLine("New key created");                
                Enabled = true;            
            }
        }