private string SignZip(string zipName, RSACryptoServiceProvider csp)
        {
            string signature = null;

            byte[] ZipRawData = null;
            try
            {
                ZipRawData = File.ReadAllBytes(zipName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not read ZIP File: " + ex.ToString(), "Read Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(signature);
            }

            try
            {
                signature = RSASignatures.Sign(ZipRawData, csp);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not sign ZIP File:  " + ex.ToString(), "Signature Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(signature);
            }

            return(signature);
        }
        private void btnSign_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPrivateKey.Text))
            {
                MessageBox.Show("Please Select a Valid Private Key", "Private Key Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(txtFolderToSign.Text) ||
                !Directory.Exists(txtFolderToSign.Text))
            {
                MessageBox.Show("Please Select a Directory", "Directory Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (!txtFolderToSign.Text.EndsWith("\\"))
            {
                txtFolderToSign.Text += "\\";
            }

            SaveFileDialog dialog = new SaveFileDialog();

            dialog.Filter = "Zip Files *.zip|*.zip";
            DialogResult result = dialog.ShowDialog();

            if (DialogResult.OK == result)
            {
                RSACryptoServiceProvider csp = null;
                try
                {
                    csp = RSASignatures.ReadRSAPrivateKeyPem(txtPrivateKey.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Could not read RSA Private Key: " + ex.ToString(), "Private Key Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (CreateZip(dialog.FileName, txtFolderToSign.Text))
                {
                    string signature = SignZip(dialog.FileName, csp);

                    if (signature != null)
                    {
                        XMLUpdateManifest manifest = new XMLUpdateManifest();
                        manifest.url       = "TBD";
                        manifest.version   = "TBD";
                        manifest.changelog = "TBD";
                        manifest.signature = signature;

                        manifest.Serialize(Path.GetDirectoryName(dialog.FileName) + "\\UpdateManifest.xml");
                    }
                }
            }
        }
        private void OnDownloadComplete(object sender, AsyncCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {
                if (!File.Exists("Certificates\\UpdatesSignaturePubK.pem"))
                {
                    return;
                }

                try
                {
                    byte[] zipdata = File.ReadAllBytes(_tempPath);
                    RSACryptoServiceProvider csp = RSASignatures.ReadRSAPublicKeyPem("Certificates\\UpdatesSignaturePubK.pem");
                    bool hashValid = RSASignatures.Verify(zipdata, _signature, csp);

                    if (!hashValid)
                    {
                        Close();
                        var    resources = new System.ComponentModel.ComponentResourceManager(this.GetType());
                        string title     = resources.GetString("SignatureError", CultureInfo.CurrentCulture);
                        string msg       = resources.GetString("SignatureErrorDesc", CultureInfo.CurrentCulture);
                        MessageBox.Show(msg, title,
                                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                        return;
                    }
                }
                catch (Exception)
                {
                    return;
                }

                var processStartInfo = new ProcessStartInfo {
                    FileName = _tempPath, UseShellExecute = true
                };
                var extension = Path.GetExtension(_tempPath);
                if (extension != null && extension.ToLower().Equals(".zip"))
                {
                    string installerPath = Path.Combine(Path.GetTempPath(), "ZipExtractor.exe");
                    File.WriteAllBytes(installerPath, Properties.Resources.ZipExtractor);
                    processStartInfo = new ProcessStartInfo
                    {
                        UseShellExecute = true,
                        FileName        = installerPath,
                        Arguments       = string.Format("\"{0}\" \"{1}\"", _tempPath, Assembly.GetEntryAssembly().Location)
                    };
                }
                try
                {
                    Process.Start(processStartInfo);
                }
                catch (Win32Exception exception)
                {
                    if (exception.NativeErrorCode != 1223)
                    {
                        throw;
                    }
                }

                var currentProcess = Process.GetCurrentProcess();
                foreach (var process in Process.GetProcessesByName(currentProcess.ProcessName))
                {
                    if (process.Id != currentProcess.Id)
                    {
                        process.Kill();
                    }
                }

                if (AutoUpdater.IsWinFormsApplication)
                {
                    Application.Exit();
                }
                else
                {
                    Environment.Exit(0);
                }
            }
        }