533 lines
23 KiB
C#
533 lines
23 KiB
C#
|
/*
|
||
|
* SecureBlackbox 2024 .NET Edition - Sample Project
|
||
|
*
|
||
|
* This sample project demonstrates the usage of SecureBlackbox in a
|
||
|
* simple, straightforward way. It is not intended to be a complete
|
||
|
* application. Error handling and other checks are simplified for clarity.
|
||
|
*
|
||
|
* www.nsoftware.com/secureblackbox
|
||
|
*
|
||
|
* This code is subject to the terms and conditions specified in the
|
||
|
* corresponding product license agreement which outlines the authorized
|
||
|
* usage and restrictions.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
using System;
|
||
|
using System.Windows.Forms;
|
||
|
using nsoftware.SecureBlackbox;
|
||
|
|
||
|
namespace PDFEncryptorDemo
|
||
|
{
|
||
|
public class MainForm : System.Windows.Forms.Form
|
||
|
{
|
||
|
private System.Windows.Forms.Button btnBrowseSource;
|
||
|
private System.Windows.Forms.Button btnBrowseDest;
|
||
|
private System.Windows.Forms.Label lSource;
|
||
|
private System.Windows.Forms.Label lDest;
|
||
|
private System.Windows.Forms.TextBox tbSource;
|
||
|
private System.Windows.Forms.TextBox tbDest;
|
||
|
private System.Windows.Forms.GroupBox gbEncProps;
|
||
|
private System.Windows.Forms.Label lEncryptionAlgorithm;
|
||
|
private System.Windows.Forms.ComboBox cbEncryptionAlgorithm;
|
||
|
private System.Windows.Forms.CheckBox cbEncryptMetadata;
|
||
|
private System.Windows.Forms.RadioButton rbPasswordEncryption;
|
||
|
private System.Windows.Forms.RadioButton rbPublicKeyEncryption;
|
||
|
private System.Windows.Forms.TextBox tbPassword;
|
||
|
private System.Windows.Forms.Label lCertificate;
|
||
|
private System.Windows.Forms.Button btnBrowseCert;
|
||
|
private System.Windows.Forms.TextBox tbCert;
|
||
|
private System.Windows.Forms.Label lCertPassword;
|
||
|
private System.Windows.Forms.TextBox tbCertPassword;
|
||
|
private System.Windows.Forms.Button btnEncrypt;
|
||
|
private System.Windows.Forms.Button btnCancel;
|
||
|
private System.Windows.Forms.OpenFileDialog openPDFDialog;
|
||
|
private System.Windows.Forms.SaveFileDialog savePDFDialog;
|
||
|
private System.Windows.Forms.OpenFileDialog openCertDialog;
|
||
|
private System.Windows.Forms.Label lInfo;
|
||
|
private System.Windows.Forms.Label lPassword;
|
||
|
|
||
|
/// <summary>
|
||
|
/// Required designer variable.
|
||
|
/// </summary>
|
||
|
private System.ComponentModel.Container components = null;
|
||
|
|
||
|
public MainForm()
|
||
|
{
|
||
|
//
|
||
|
// Required for Windows Form Designer support
|
||
|
//
|
||
|
InitializeComponent();
|
||
|
|
||
|
//
|
||
|
// TODO: Add any constructor code after InitializeComponent call
|
||
|
//
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Clean up any resources being used.
|
||
|
/// </summary>
|
||
|
protected override void Dispose(bool disposing)
|
||
|
{
|
||
|
if (disposing)
|
||
|
{
|
||
|
if (components != null)
|
||
|
{
|
||
|
components.Dispose();
|
||
|
}
|
||
|
}
|
||
|
base.Dispose(disposing);
|
||
|
}
|
||
|
|
||
|
#region Windows Form Designer generated code
|
||
|
/// <summary>
|
||
|
/// Required method for Designer support - do not modify
|
||
|
/// the contents of this method with the code editor.
|
||
|
/// </summary>
|
||
|
private void InitializeComponent()
|
||
|
{
|
||
|
this.tbSource = new System.Windows.Forms.TextBox();
|
||
|
this.tbDest = new System.Windows.Forms.TextBox();
|
||
|
this.btnBrowseSource = new System.Windows.Forms.Button();
|
||
|
this.btnBrowseDest = new System.Windows.Forms.Button();
|
||
|
this.lSource = new System.Windows.Forms.Label();
|
||
|
this.lDest = new System.Windows.Forms.Label();
|
||
|
this.gbEncProps = new System.Windows.Forms.GroupBox();
|
||
|
this.lPassword = new System.Windows.Forms.Label();
|
||
|
this.tbCertPassword = new System.Windows.Forms.TextBox();
|
||
|
this.btnBrowseCert = new System.Windows.Forms.Button();
|
||
|
this.lCertPassword = new System.Windows.Forms.Label();
|
||
|
this.lCertificate = new System.Windows.Forms.Label();
|
||
|
this.tbCert = new System.Windows.Forms.TextBox();
|
||
|
this.tbPassword = new System.Windows.Forms.TextBox();
|
||
|
this.rbPublicKeyEncryption = new System.Windows.Forms.RadioButton();
|
||
|
this.rbPasswordEncryption = new System.Windows.Forms.RadioButton();
|
||
|
this.cbEncryptMetadata = new System.Windows.Forms.CheckBox();
|
||
|
this.cbEncryptionAlgorithm = new System.Windows.Forms.ComboBox();
|
||
|
this.lEncryptionAlgorithm = new System.Windows.Forms.Label();
|
||
|
this.btnEncrypt = new System.Windows.Forms.Button();
|
||
|
this.btnCancel = new System.Windows.Forms.Button();
|
||
|
this.openPDFDialog = new System.Windows.Forms.OpenFileDialog();
|
||
|
this.savePDFDialog = new System.Windows.Forms.SaveFileDialog();
|
||
|
this.openCertDialog = new System.Windows.Forms.OpenFileDialog();
|
||
|
this.lInfo = new System.Windows.Forms.Label();
|
||
|
this.gbEncProps.SuspendLayout();
|
||
|
this.SuspendLayout();
|
||
|
//
|
||
|
// tbSource
|
||
|
//
|
||
|
this.tbSource.Location = new System.Drawing.Point(8, 75);
|
||
|
this.tbSource.Name = "tbSource";
|
||
|
this.tbSource.Size = new System.Drawing.Size(288, 21);
|
||
|
this.tbSource.TabIndex = 1;
|
||
|
//
|
||
|
// tbDest
|
||
|
//
|
||
|
this.tbDest.Location = new System.Drawing.Point(8, 123);
|
||
|
this.tbDest.Name = "tbDest";
|
||
|
this.tbDest.Size = new System.Drawing.Size(288, 21);
|
||
|
this.tbDest.TabIndex = 4;
|
||
|
//
|
||
|
// btnBrowseSource
|
||
|
//
|
||
|
this.btnBrowseSource.Location = new System.Drawing.Point(304, 72);
|
||
|
this.btnBrowseSource.Name = "btnBrowseSource";
|
||
|
this.btnBrowseSource.Size = new System.Drawing.Size(75, 25);
|
||
|
this.btnBrowseSource.TabIndex = 2;
|
||
|
this.btnBrowseSource.Text = "Browse...";
|
||
|
this.btnBrowseSource.Click += new System.EventHandler(this.btnBrowseSource_Click);
|
||
|
//
|
||
|
// btnBrowseDest
|
||
|
//
|
||
|
this.btnBrowseDest.Location = new System.Drawing.Point(304, 120);
|
||
|
this.btnBrowseDest.Name = "btnBrowseDest";
|
||
|
this.btnBrowseDest.Size = new System.Drawing.Size(75, 25);
|
||
|
this.btnBrowseDest.TabIndex = 5;
|
||
|
this.btnBrowseDest.Text = "Browse...";
|
||
|
this.btnBrowseDest.Click += new System.EventHandler(this.btnBrowseDest_Click);
|
||
|
//
|
||
|
// lSource
|
||
|
//
|
||
|
this.lSource.AutoSize = true;
|
||
|
this.lSource.Location = new System.Drawing.Point(8, 57);
|
||
|
this.lSource.Name = "lSource";
|
||
|
this.lSource.Size = new System.Drawing.Size(83, 13);
|
||
|
this.lSource.TabIndex = 0;
|
||
|
this.lSource.Text = "Source PDF file:";
|
||
|
//
|
||
|
// lDest
|
||
|
//
|
||
|
this.lDest.AutoSize = true;
|
||
|
this.lDest.Location = new System.Drawing.Point(8, 105);
|
||
|
this.lDest.Name = "lDest";
|
||
|
this.lDest.Size = new System.Drawing.Size(104, 13);
|
||
|
this.lDest.TabIndex = 3;
|
||
|
this.lDest.Text = "Destination PDF file:";
|
||
|
//
|
||
|
// gbEncProps
|
||
|
//
|
||
|
this.gbEncProps.Controls.Add(this.lPassword);
|
||
|
this.gbEncProps.Controls.Add(this.tbCertPassword);
|
||
|
this.gbEncProps.Controls.Add(this.btnBrowseCert);
|
||
|
this.gbEncProps.Controls.Add(this.lCertPassword);
|
||
|
this.gbEncProps.Controls.Add(this.lCertificate);
|
||
|
this.gbEncProps.Controls.Add(this.tbCert);
|
||
|
this.gbEncProps.Controls.Add(this.tbPassword);
|
||
|
this.gbEncProps.Controls.Add(this.rbPublicKeyEncryption);
|
||
|
this.gbEncProps.Controls.Add(this.rbPasswordEncryption);
|
||
|
this.gbEncProps.Controls.Add(this.cbEncryptMetadata);
|
||
|
this.gbEncProps.Controls.Add(this.cbEncryptionAlgorithm);
|
||
|
this.gbEncProps.Controls.Add(this.lEncryptionAlgorithm);
|
||
|
this.gbEncProps.Location = new System.Drawing.Point(8, 153);
|
||
|
this.gbEncProps.Name = "gbEncProps";
|
||
|
this.gbEncProps.Size = new System.Drawing.Size(368, 273);
|
||
|
this.gbEncProps.TabIndex = 6;
|
||
|
this.gbEncProps.TabStop = false;
|
||
|
this.gbEncProps.Text = "Encryption properties";
|
||
|
//
|
||
|
// lPassword
|
||
|
//
|
||
|
this.lPassword.AutoSize = true;
|
||
|
this.lPassword.Enabled = false;
|
||
|
this.lPassword.Location = new System.Drawing.Point(40, 99);
|
||
|
this.lPassword.Name = "lPassword";
|
||
|
this.lPassword.Size = new System.Drawing.Size(57, 13);
|
||
|
this.lPassword.TabIndex = 4;
|
||
|
this.lPassword.Text = "Password:";
|
||
|
//
|
||
|
// tbCertPassword
|
||
|
//
|
||
|
this.tbCertPassword.Enabled = false;
|
||
|
this.tbCertPassword.Location = new System.Drawing.Point(40, 243);
|
||
|
this.tbCertPassword.Name = "tbCertPassword";
|
||
|
this.tbCertPassword.PasswordChar = '*';
|
||
|
this.tbCertPassword.Size = new System.Drawing.Size(128, 21);
|
||
|
this.tbCertPassword.TabIndex = 11;
|
||
|
//
|
||
|
// btnBrowseCert
|
||
|
//
|
||
|
this.btnBrowseCert.Enabled = false;
|
||
|
this.btnBrowseCert.Location = new System.Drawing.Point(278, 194);
|
||
|
this.btnBrowseCert.Name = "btnBrowseCert";
|
||
|
this.btnBrowseCert.Size = new System.Drawing.Size(75, 25);
|
||
|
this.btnBrowseCert.TabIndex = 9;
|
||
|
this.btnBrowseCert.Text = "Browse...";
|
||
|
this.btnBrowseCert.Click += new System.EventHandler(this.btnBrowseCert_Click);
|
||
|
//
|
||
|
// lCertPassword
|
||
|
//
|
||
|
this.lCertPassword.AutoSize = true;
|
||
|
this.lCertPassword.Enabled = false;
|
||
|
this.lCertPassword.Location = new System.Drawing.Point(40, 227);
|
||
|
this.lCertPassword.Name = "lCertPassword";
|
||
|
this.lCertPassword.Size = new System.Drawing.Size(110, 13);
|
||
|
this.lCertPassword.TabIndex = 10;
|
||
|
this.lCertPassword.Text = "Certificate password:";
|
||
|
//
|
||
|
// lCertificate
|
||
|
//
|
||
|
this.lCertificate.AutoSize = true;
|
||
|
this.lCertificate.Enabled = false;
|
||
|
this.lCertificate.Location = new System.Drawing.Point(40, 179);
|
||
|
this.lCertificate.Name = "lCertificate";
|
||
|
this.lCertificate.Size = new System.Drawing.Size(113, 13);
|
||
|
this.lCertificate.TabIndex = 7;
|
||
|
this.lCertificate.Text = "Encryption certificate:";
|
||
|
//
|
||
|
// tbCert
|
||
|
//
|
||
|
this.tbCert.Enabled = false;
|
||
|
this.tbCert.Location = new System.Drawing.Point(40, 197);
|
||
|
this.tbCert.Name = "tbCert";
|
||
|
this.tbCert.Size = new System.Drawing.Size(232, 21);
|
||
|
this.tbCert.TabIndex = 8;
|
||
|
//
|
||
|
// tbPassword
|
||
|
//
|
||
|
this.tbPassword.Location = new System.Drawing.Point(40, 118);
|
||
|
this.tbPassword.Name = "tbPassword";
|
||
|
this.tbPassword.PasswordChar = '*';
|
||
|
this.tbPassword.Size = new System.Drawing.Size(128, 21);
|
||
|
this.tbPassword.TabIndex = 5;
|
||
|
//
|
||
|
// rbPublicKeyEncryption
|
||
|
//
|
||
|
this.rbPublicKeyEncryption.Location = new System.Drawing.Point(16, 151);
|
||
|
this.rbPublicKeyEncryption.Name = "rbPublicKeyEncryption";
|
||
|
this.rbPublicKeyEncryption.Size = new System.Drawing.Size(304, 24);
|
||
|
this.rbPublicKeyEncryption.TabIndex = 6;
|
||
|
this.rbPublicKeyEncryption.Text = "Public key encryption";
|
||
|
this.rbPublicKeyEncryption.CheckedChanged += new System.EventHandler(this.rbPasswordEncryption_CheckedChanged);
|
||
|
//
|
||
|
// rbPasswordEncryption
|
||
|
//
|
||
|
this.rbPasswordEncryption.Checked = true;
|
||
|
this.rbPasswordEncryption.Location = new System.Drawing.Point(16, 72);
|
||
|
this.rbPasswordEncryption.Name = "rbPasswordEncryption";
|
||
|
this.rbPasswordEncryption.Size = new System.Drawing.Size(272, 24);
|
||
|
this.rbPasswordEncryption.TabIndex = 3;
|
||
|
this.rbPasswordEncryption.TabStop = true;
|
||
|
this.rbPasswordEncryption.Text = "Password encryption";
|
||
|
this.rbPasswordEncryption.CheckedChanged += new System.EventHandler(this.rbPasswordEncryption_CheckedChanged);
|
||
|
//
|
||
|
// cbEncryptMetadata
|
||
|
//
|
||
|
this.cbEncryptMetadata.AutoSize = true;
|
||
|
this.cbEncryptMetadata.Checked = true;
|
||
|
this.cbEncryptMetadata.CheckState = System.Windows.Forms.CheckState.Checked;
|
||
|
this.cbEncryptMetadata.Location = new System.Drawing.Point(184, 40);
|
||
|
this.cbEncryptMetadata.Name = "cbEncryptMetadata";
|
||
|
this.cbEncryptMetadata.Size = new System.Drawing.Size(162, 17);
|
||
|
this.cbEncryptMetadata.TabIndex = 2;
|
||
|
this.cbEncryptMetadata.Text = "Encrypt document metadata";
|
||
|
//
|
||
|
// cbEncryptionAlgorithm
|
||
|
//
|
||
|
this.cbEncryptionAlgorithm.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||
|
this.cbEncryptionAlgorithm.Items.AddRange(new object[] {
|
||
|
"RC4/40 bits (Acrobat 4)",
|
||
|
"RC4/128 bits (Acrobat 5)",
|
||
|
"AES/128 bits (Acrobat 6, 7)",
|
||
|
"AES/256 bits (Acrobat 9)",
|
||
|
"AES/256 bits (Acrobat X)"});
|
||
|
this.cbEncryptionAlgorithm.Location = new System.Drawing.Point(16, 40);
|
||
|
this.cbEncryptionAlgorithm.Name = "cbEncryptionAlgorithm";
|
||
|
this.cbEncryptionAlgorithm.Size = new System.Drawing.Size(152, 21);
|
||
|
this.cbEncryptionAlgorithm.TabIndex = 1;
|
||
|
//
|
||
|
// lEncryptionAlgorithm
|
||
|
//
|
||
|
this.lEncryptionAlgorithm.AutoSize = true;
|
||
|
this.lEncryptionAlgorithm.Location = new System.Drawing.Point(16, 24);
|
||
|
this.lEncryptionAlgorithm.Name = "lEncryptionAlgorithm";
|
||
|
this.lEncryptionAlgorithm.Size = new System.Drawing.Size(109, 13);
|
||
|
this.lEncryptionAlgorithm.TabIndex = 0;
|
||
|
this.lEncryptionAlgorithm.Text = "Encryption algorithm:";
|
||
|
//
|
||
|
// btnEncrypt
|
||
|
//
|
||
|
this.btnEncrypt.DialogResult = System.Windows.Forms.DialogResult.OK;
|
||
|
this.btnEncrypt.Location = new System.Drawing.Point(125, 435);
|
||
|
this.btnEncrypt.Name = "btnEncrypt";
|
||
|
this.btnEncrypt.Size = new System.Drawing.Size(75, 25);
|
||
|
this.btnEncrypt.TabIndex = 7;
|
||
|
this.btnEncrypt.Text = "Encrypt";
|
||
|
this.btnEncrypt.Click += new System.EventHandler(this.btnEncrypt_Click);
|
||
|
//
|
||
|
// btnCancel
|
||
|
//
|
||
|
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
|
||
|
this.btnCancel.Location = new System.Drawing.Point(205, 435);
|
||
|
this.btnCancel.Name = "btnCancel";
|
||
|
this.btnCancel.Size = new System.Drawing.Size(75, 25);
|
||
|
this.btnCancel.TabIndex = 8;
|
||
|
this.btnCancel.Text = "Cancel";
|
||
|
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
|
||
|
//
|
||
|
// openPDFDialog
|
||
|
//
|
||
|
this.openPDFDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
|
||
|
this.openPDFDialog.InitialDirectory = ".";
|
||
|
//
|
||
|
// savePDFDialog
|
||
|
//
|
||
|
this.savePDFDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
|
||
|
this.savePDFDialog.InitialDirectory = ".";
|
||
|
//
|
||
|
// openCertDialog
|
||
|
//
|
||
|
this.openCertDialog.Filter = "Raw X.509 certificate (*.cer, *.csr, *.crt)|*.CER;*.CSR;*.CRT|Base64-encoded X.50" +
|
||
|
"9 certificate (*.pem)|*.PEM|PKCS#12 certificate (*.pfx, *.p12)|*.PFX; *.P12|All " +
|
||
|
"files (*.*)|*.*";
|
||
|
this.openCertDialog.InitialDirectory = ".";
|
||
|
//
|
||
|
// lInfo
|
||
|
//
|
||
|
this.lInfo.ForeColor = System.Drawing.SystemColors.Highlight;
|
||
|
this.lInfo.Location = new System.Drawing.Point(8, 8);
|
||
|
this.lInfo.Name = "lInfo";
|
||
|
this.lInfo.Size = new System.Drawing.Size(371, 49);
|
||
|
this.lInfo.TabIndex = 9;
|
||
|
this.lInfo.Text = "This sample illustrates the use of PDFEncryptor component for encrypting PDF docu" +
|
||
|
"ments. Please pick the encryption type and specify either encryption password or" +
|
||
|
" certificate and then click \'Encrypt\'";
|
||
|
this.lInfo.UseMnemonic = false;
|
||
|
//
|
||
|
// MainForm
|
||
|
//
|
||
|
this.AcceptButton = this.btnEncrypt;
|
||
|
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
|
||
|
this.CancelButton = this.btnCancel;
|
||
|
this.ClientSize = new System.Drawing.Size(389, 467);
|
||
|
this.Controls.Add(this.lInfo);
|
||
|
this.Controls.Add(this.btnCancel);
|
||
|
this.Controls.Add(this.btnEncrypt);
|
||
|
this.Controls.Add(this.gbEncProps);
|
||
|
this.Controls.Add(this.lDest);
|
||
|
this.Controls.Add(this.lSource);
|
||
|
this.Controls.Add(this.btnBrowseDest);
|
||
|
this.Controls.Add(this.btnBrowseSource);
|
||
|
this.Controls.Add(this.tbDest);
|
||
|
this.Controls.Add(this.tbSource);
|
||
|
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
|
||
|
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
|
||
|
this.MaximizeBox = false;
|
||
|
this.Name = "MainForm";
|
||
|
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
|
||
|
this.Text = "PDF Encryptor demo";
|
||
|
this.Load += new System.EventHandler(this.MainForm_Load);
|
||
|
this.gbEncProps.ResumeLayout(false);
|
||
|
this.gbEncProps.PerformLayout();
|
||
|
this.ResumeLayout(false);
|
||
|
this.PerformLayout();
|
||
|
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
/// <summary>
|
||
|
/// The main entry point for the application.
|
||
|
/// </summary>
|
||
|
[STAThread]
|
||
|
static void Main()
|
||
|
{
|
||
|
Application.Run(new MainForm());
|
||
|
}
|
||
|
|
||
|
private void btnBrowseSource_Click(object sender, System.EventArgs e)
|
||
|
{
|
||
|
openPDFDialog.FileName = tbSource.Text;
|
||
|
if (openPDFDialog.ShowDialog() == DialogResult.OK)
|
||
|
tbSource.Text = openPDFDialog.FileName;
|
||
|
}
|
||
|
|
||
|
private void btnBrowseDest_Click(object sender, System.EventArgs e)
|
||
|
{
|
||
|
savePDFDialog.FileName = tbDest.Text;
|
||
|
if (savePDFDialog.ShowDialog() == DialogResult.OK)
|
||
|
tbDest.Text = savePDFDialog.FileName;
|
||
|
}
|
||
|
|
||
|
private void btnBrowseCert_Click(object sender, System.EventArgs e)
|
||
|
{
|
||
|
openCertDialog.FileName = tbCert.Text;
|
||
|
if (openCertDialog.ShowDialog() == DialogResult.OK)
|
||
|
tbCert.Text = openCertDialog.FileName;
|
||
|
}
|
||
|
|
||
|
private void rbPasswordEncryption_CheckedChanged(object sender, System.EventArgs e)
|
||
|
{
|
||
|
tbPassword.Enabled = rbPasswordEncryption.Checked;
|
||
|
lPassword.Enabled = rbPasswordEncryption.Checked;
|
||
|
tbCert.Enabled = !rbPasswordEncryption.Checked;
|
||
|
tbCertPassword.Enabled = !rbPasswordEncryption.Checked;
|
||
|
btnBrowseCert.Enabled = !rbPasswordEncryption.Checked;
|
||
|
lCertificate.Enabled = !rbPasswordEncryption.Checked;
|
||
|
lCertPassword.Enabled = !rbPasswordEncryption.Checked;
|
||
|
|
||
|
if (rbPasswordEncryption.Checked)
|
||
|
{
|
||
|
if (cbEncryptionAlgorithm.Items.Count == 4)
|
||
|
cbEncryptionAlgorithm.Items.Add("AES/256 bits (Acrobat X)");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (cbEncryptionAlgorithm.Items.Count == 5)
|
||
|
cbEncryptionAlgorithm.Items.RemoveAt(4);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Certificate LoadCertificate(string file, string password)
|
||
|
{
|
||
|
Certificate cert = null;
|
||
|
|
||
|
if (file.Length > 0)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
CertificateManager certmanager = new CertificateManager();
|
||
|
|
||
|
certmanager.ImportFromFile(file, password);
|
||
|
|
||
|
cert = certmanager.Certificate;
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
MessageBox.Show("Cannot load certificate!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return cert;
|
||
|
}
|
||
|
|
||
|
private void btnEncrypt_Click(object sender, System.EventArgs e)
|
||
|
{
|
||
|
using (PDFEncryptor PDFEncryptor = new PDFEncryptor())
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
PDFEncryptor.InputFile = tbSource.Text;
|
||
|
PDFEncryptor.OutputFile = tbDest.Text;
|
||
|
|
||
|
if (rbPasswordEncryption.Checked)
|
||
|
{
|
||
|
PDFEncryptor.UserPassword = tbPassword.Text;
|
||
|
PDFEncryptor.OwnerPassword = tbPassword.Text;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
PDFEncryptor.EncryptionCertificate = LoadCertificate(tbCert.Text, tbCertPassword.Text);
|
||
|
}
|
||
|
|
||
|
PDFEncryptor.EncryptMetadata = cbEncryptMetadata.Checked;
|
||
|
|
||
|
if (cbEncryptionAlgorithm.SelectedIndex <= 1)
|
||
|
{
|
||
|
PDFEncryptor.EncryptionAlgorithm = "RC4";
|
||
|
if (cbEncryptionAlgorithm.SelectedIndex == 1)
|
||
|
PDFEncryptor.Config("RC4KeyBits=40");
|
||
|
else
|
||
|
PDFEncryptor.Config("RC4KeyBits=128");
|
||
|
}
|
||
|
else if (cbEncryptionAlgorithm.SelectedIndex == 2)
|
||
|
{
|
||
|
PDFEncryptor.EncryptionAlgorithm = "AES128";
|
||
|
}
|
||
|
else if (cbEncryptionAlgorithm.SelectedIndex > 3)
|
||
|
{
|
||
|
PDFEncryptor.EncryptionAlgorithm = "AES256";
|
||
|
if (PDFEncryptor.EncryptionCertificate == null)
|
||
|
{
|
||
|
if (cbEncryptionAlgorithm.SelectedIndex == 3)
|
||
|
PDFEncryptor.Config("HardenedKeyGeneration=false");
|
||
|
else
|
||
|
PDFEncryptor.Config("HardenedKeyGeneration=true");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
PDFEncryptor.Encrypt();
|
||
|
|
||
|
MessageBox.Show("PDF document successfully encrypted", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
this.Close();
|
||
|
}
|
||
|
|
||
|
private void MainForm_Load(object sender, System.EventArgs e)
|
||
|
{
|
||
|
cbEncryptionAlgorithm.SelectedIndex = 2;
|
||
|
}
|
||
|
|
||
|
private void btnCancel_Click(object sender, System.EventArgs e)
|
||
|
{
|
||
|
this.Close();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|