securebb-win-demos/PDF Decryptor (WinForms)/pdfencprops.cs

332 lines
14 KiB
C#
Raw Permalink Normal View History

2024-08-08 13:09:34 +06:00
using System;
using System.Windows.Forms;
using nsoftware.SecureBlackbox;
namespace PDFDecryptorDemo
{
public class EncPropsForm : Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.btnCancel = new System.Windows.Forms.Button();
this.btnOK = new System.Windows.Forms.Button();
this.gbEncProps = new System.Windows.Forms.GroupBox();
this.lProvideCertificate = new System.Windows.Forms.Label();
this.lProvidePassword = new System.Windows.Forms.Label();
this.lMetadataStatus = 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.lEncryptionAlgorithm = new System.Windows.Forms.Label();
this.openCertDialog = new System.Windows.Forms.OpenFileDialog();
this.lInfo = new System.Windows.Forms.Label();
this.gbEncProps.SuspendLayout();
this.SuspendLayout();
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(204, 296);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 3;
this.btnCancel.Text = "Cancel";
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Location = new System.Drawing.Point(124, 296);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 23);
this.btnOK.TabIndex = 2;
this.btnOK.Text = "OK";
//
// gbEncProps
//
this.gbEncProps.Controls.Add(this.lProvideCertificate);
this.gbEncProps.Controls.Add(this.lProvidePassword);
this.gbEncProps.Controls.Add(this.lMetadataStatus);
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.lEncryptionAlgorithm);
this.gbEncProps.Location = new System.Drawing.Point(12, 45);
this.gbEncProps.Name = "gbEncProps";
this.gbEncProps.Size = new System.Drawing.Size(367, 243);
this.gbEncProps.TabIndex = 1;
this.gbEncProps.TabStop = false;
//
// lProvideCertificate
//
this.lProvideCertificate.AutoSize = true;
this.lProvideCertificate.Location = new System.Drawing.Point(16, 129);
this.lProvideCertificate.Name = "lProvideCertificate";
this.lProvideCertificate.Size = new System.Drawing.Size(162, 13);
this.lProvideCertificate.TabIndex = 4;
this.lProvideCertificate.Text = "Provide certificate for decryption:";
//
// lProvidePassword
//
this.lProvidePassword.AutoSize = true;
this.lProvidePassword.Location = new System.Drawing.Point(16, 78);
this.lProvidePassword.Name = "lProvidePassword";
this.lProvidePassword.Size = new System.Drawing.Size(161, 13);
this.lProvidePassword.TabIndex = 2;
this.lProvidePassword.Text = "Provide password for decryption:";
//
// lMetadataStatus
//
this.lMetadataStatus.AutoSize = true;
this.lMetadataStatus.Location = new System.Drawing.Point(16, 45);
this.lMetadataStatus.Name = "lMetadataStatus";
this.lMetadataStatus.Size = new System.Drawing.Size(86, 13);
this.lMetadataStatus.TabIndex = 1;
this.lMetadataStatus.Text = "Metadata status:";
//
// tbCertPassword
//
this.tbCertPassword.Enabled = false;
this.tbCertPassword.Location = new System.Drawing.Point(32, 211);
this.tbCertPassword.Name = "tbCertPassword";
this.tbCertPassword.PasswordChar = '*';
this.tbCertPassword.Size = new System.Drawing.Size(128, 20);
this.tbCertPassword.TabIndex = 9;
//
// btnBrowseCert
//
this.btnBrowseCert.Enabled = false;
this.btnBrowseCert.Location = new System.Drawing.Point(286, 163);
this.btnBrowseCert.Name = "btnBrowseCert";
this.btnBrowseCert.Size = new System.Drawing.Size(75, 27);
this.btnBrowseCert.TabIndex = 7;
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(32, 195);
this.lCertPassword.Name = "lCertPassword";
this.lCertPassword.Size = new System.Drawing.Size(105, 13);
this.lCertPassword.TabIndex = 8;
this.lCertPassword.Text = "Certificate password:";
//
// lCertificate
//
this.lCertificate.AutoSize = true;
this.lCertificate.Enabled = false;
this.lCertificate.Location = new System.Drawing.Point(32, 149);
this.lCertificate.Name = "lCertificate";
this.lCertificate.Size = new System.Drawing.Size(110, 13);
this.lCertificate.TabIndex = 5;
this.lCertificate.Text = "Decryption certificate:";
//
// tbCert
//
this.tbCert.Enabled = false;
this.tbCert.Location = new System.Drawing.Point(32, 167);
this.tbCert.Name = "tbCert";
this.tbCert.Size = new System.Drawing.Size(248, 20);
this.tbCert.TabIndex = 6;
//
// tbPassword
//
this.tbPassword.Location = new System.Drawing.Point(35, 97);
this.tbPassword.Name = "tbPassword";
this.tbPassword.PasswordChar = '*';
this.tbPassword.Size = new System.Drawing.Size(128, 20);
this.tbPassword.TabIndex = 3;
//
// lEncryptionAlgorithm
//
this.lEncryptionAlgorithm.AutoSize = true;
this.lEncryptionAlgorithm.Location = new System.Drawing.Point(16, 19);
this.lEncryptionAlgorithm.Name = "lEncryptionAlgorithm";
this.lEncryptionAlgorithm.Size = new System.Drawing.Size(105, 13);
this.lEncryptionAlgorithm.TabIndex = 0;
this.lEncryptionAlgorithm.Text = "Encryption algorithm:";
//
// 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(9, 9);
this.lInfo.Name = "lInfo";
this.lInfo.Size = new System.Drawing.Size(371, 35);
this.lInfo.TabIndex = 0;
this.lInfo.Text = "The PDF document is encrypted. Depending on the document\'s encryption type you wo" +
"uld need to provide a decryption password or decryption certificate.";
this.lInfo.UseMnemonic = false;
//
// EncPropsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(389, 327);
this.Controls.Add(this.lInfo);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.gbEncProps);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "EncPropsForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Encryption properties";
this.gbEncProps.ResumeLayout(false);
this.gbEncProps.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.GroupBox gbEncProps;
private System.Windows.Forms.TextBox tbCertPassword;
private System.Windows.Forms.Button btnBrowseCert;
private System.Windows.Forms.Label lCertPassword;
private System.Windows.Forms.Label lCertificate;
private System.Windows.Forms.TextBox tbCert;
private System.Windows.Forms.TextBox tbPassword;
private System.Windows.Forms.OpenFileDialog openCertDialog;
private System.Windows.Forms.Label lProvideCertificate;
private System.Windows.Forms.Label lProvidePassword;
private System.Windows.Forms.Label lMetadataStatus;
private System.Windows.Forms.Label lInfo;
private System.Windows.Forms.Label lEncryptionAlgorithm;
public EncPropsForm()
{
InitializeComponent();
}
private void btnBrowseCert_Click(object sender, EventArgs e)
{
openCertDialog.FileName = tbCert.Text;
if (openCertDialog.ShowDialog() == DialogResult.OK)
tbCert.Text = openCertDialog.FileName;
}
private int m_Count = 0;
public void SetPDFEncryptionProps(PDFDecryptor PDFDecryptor)
{
string Alg = PDFDecryptor.DocumentInfo.EncryptionAlgorithm;
if (Alg == "RC4")
{
Alg = Alg + "/" + PDFDecryptor.Config("RC4KeyBits");
}
lEncryptionAlgorithm.Text = "Encryption algorithm: " + Alg;
if (PDFDecryptor.DocumentInfo.EncryptionType == PDFEncryptionTypes.petPassword)
{
if (m_Count > 0)
MessageBox.Show("Invalid password", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (PDFDecryptor.DocumentInfo.MetadataEncrypted)
lMetadataStatus.Text = "Metadata status: encrypted";
else
lMetadataStatus.Text = "Metadata status: not encrypted";
lProvidePassword.Enabled = true;
tbPassword.Enabled = true;
lProvideCertificate.Enabled = false;
tbCert.Enabled = false;
tbCertPassword.Enabled = false;
btnBrowseCert.Enabled = false;
}
else
{
lMetadataStatus.Text = ""; // metadata status could be read only after decryption
lProvidePassword.Enabled = false;
tbPassword.Enabled = false;
lProvideCertificate.Enabled = true;
tbCert.Enabled = true;
tbCertPassword.Enabled = true;
btnBrowseCert.Enabled = true;
}
}
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;
}
public void GetPDFDecryptionInfo(PDFDecryptor PDFDecryptor)
{
if (PDFDecryptor.DocumentInfo.EncryptionType == PDFEncryptionTypes.petPassword)
PDFDecryptor.Password = tbPassword.Text;
else
{
try
{
PDFDecryptor.DecryptionCertificate = LoadCertificate(tbCert.Text, tbCertPassword.Text);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
m_Count++;
}
}
}
}