コード例 #1
0
        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);            
        }
コード例 #2
0
        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;            
        }