securebb-win-demos/XML Encryptor (WinForms)/xmlencryptor.cs

780 lines
33 KiB
C#
Raw Permalink Normal View History

2024-08-08 13:09:34 +06:00
/*
* 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;
using System.IO;
namespace XMLEncryptorDemo
{
/// <summary>
/// Summary description for frmMain.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lbXMLFile;
private System.Windows.Forms.TextBox edXMLFile;
private System.Windows.Forms.Button sbBrowseXMLFile;
private System.Windows.Forms.OpenFileDialog dlgOpen;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button btnEncrypt;
private GroupBox gbKeyInfo;
private TextBox edPassphrase;
private Label lbPassphrase;
private TextBox edOutputFile;
private Button sbBrowseOutputFile;
private Label label2;
private SaveFileDialog dlgSave;
private GroupBox gbGeneralEnc;
private ComboBox cmbEncryptionMethod;
private Label lbEncryptionmethod;
private ComboBox cmbEncryptedDataType;
private Label lbEncryptedDataType;
private Label label3;
private Label lbCertificate;
private Button btnBrowseCert;
private TextBox edCertPassword;
private TextBox edCert;
private Label lbCertPassword;
private Label lbExternalFile;
private Button btnBrowseExternalFile;
private TextBox edExternalFile;
private TextBox edMimeType;
private Label lbMimeType;
private TextBox edXMLNode;
private Label label1;
private GroupBox groupBox1;
private ComboBox cmbKeyWrap;
private Label lbKeyWrap;
private ComboBox cmbKeyTransport;
private Label lbKeyTransport;
private GroupBox gbKEK;
private RadioButton rbKeyWrap;
private RadioButton rbKeyTransport;
private CheckBox cbEncryptKey;
private XMLEncryptor encryptor;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
encryptor = new XMLEncryptor();
cmbEncryptionMethod.SelectedIndex = 1;
cmbEncryptedDataType.SelectedIndex = 0;
cmbKeyTransport.SelectedIndex = 0;
cmbKeyWrap.SelectedIndex = 0;
updateSettings();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
encryptor.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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.lbXMLFile = new System.Windows.Forms.Label();
this.edXMLFile = new System.Windows.Forms.TextBox();
this.sbBrowseXMLFile = new System.Windows.Forms.Button();
this.btnEncrypt = new System.Windows.Forms.Button();
this.dlgOpen = new System.Windows.Forms.OpenFileDialog();
this.gbKeyInfo = new System.Windows.Forms.GroupBox();
this.lbCertificate = new System.Windows.Forms.Label();
this.btnBrowseCert = new System.Windows.Forms.Button();
this.edCertPassword = new System.Windows.Forms.TextBox();
this.edCert = new System.Windows.Forms.TextBox();
this.lbCertPassword = new System.Windows.Forms.Label();
this.edPassphrase = new System.Windows.Forms.TextBox();
this.lbPassphrase = new System.Windows.Forms.Label();
this.edOutputFile = new System.Windows.Forms.TextBox();
this.sbBrowseOutputFile = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.dlgSave = new System.Windows.Forms.SaveFileDialog();
this.gbGeneralEnc = new System.Windows.Forms.GroupBox();
this.lbExternalFile = new System.Windows.Forms.Label();
this.btnBrowseExternalFile = new System.Windows.Forms.Button();
this.edExternalFile = new System.Windows.Forms.TextBox();
this.edMimeType = new System.Windows.Forms.TextBox();
this.lbMimeType = new System.Windows.Forms.Label();
this.edXMLNode = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.cmbEncryptionMethod = new System.Windows.Forms.ComboBox();
this.lbEncryptionmethod = new System.Windows.Forms.Label();
this.cmbEncryptedDataType = new System.Windows.Forms.ComboBox();
this.lbEncryptedDataType = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.cmbKeyWrap = new System.Windows.Forms.ComboBox();
this.lbKeyWrap = new System.Windows.Forms.Label();
this.cmbKeyTransport = new System.Windows.Forms.ComboBox();
this.lbKeyTransport = new System.Windows.Forms.Label();
this.gbKEK = new System.Windows.Forms.GroupBox();
this.rbKeyWrap = new System.Windows.Forms.RadioButton();
this.rbKeyTransport = new System.Windows.Forms.RadioButton();
this.cbEncryptKey = new System.Windows.Forms.CheckBox();
this.gbKeyInfo.SuspendLayout();
this.gbGeneralEnc.SuspendLayout();
this.groupBox1.SuspendLayout();
this.gbKEK.SuspendLayout();
this.SuspendLayout();
//
// lbXMLFile
//
this.lbXMLFile.Location = new System.Drawing.Point(5, 43);
this.lbXMLFile.Name = "lbXMLFile";
this.lbXMLFile.Size = new System.Drawing.Size(60, 13);
this.lbXMLFile.TabIndex = 0;
this.lbXMLFile.Text = "Input file:";
//
// edXMLFile
//
this.edXMLFile.Location = new System.Drawing.Point(71, 40);
this.edXMLFile.Name = "edXMLFile";
this.edXMLFile.Size = new System.Drawing.Size(373, 20);
this.edXMLFile.TabIndex = 1;
//
// sbBrowseXMLFile
//
this.sbBrowseXMLFile.Location = new System.Drawing.Point(450, 37);
this.sbBrowseXMLFile.Name = "sbBrowseXMLFile";
this.sbBrowseXMLFile.Size = new System.Drawing.Size(75, 25);
this.sbBrowseXMLFile.TabIndex = 2;
this.sbBrowseXMLFile.Text = "Browse ...";
this.sbBrowseXMLFile.Click += new System.EventHandler(this.sbBrowseXMLFile_Click);
//
// btnEncrypt
//
this.btnEncrypt.Location = new System.Drawing.Point(585, 380);
this.btnEncrypt.Name = "btnEncrypt";
this.btnEncrypt.Size = new System.Drawing.Size(75, 25);
this.btnEncrypt.TabIndex = 8;
this.btnEncrypt.Text = "Encrypt";
this.btnEncrypt.Click += new System.EventHandler(this.btnEncrypt_Click);
//
// gbKeyInfo
//
this.gbKeyInfo.Controls.Add(this.lbCertificate);
this.gbKeyInfo.Controls.Add(this.btnBrowseCert);
this.gbKeyInfo.Controls.Add(this.edCertPassword);
this.gbKeyInfo.Controls.Add(this.edCert);
this.gbKeyInfo.Controls.Add(this.lbCertPassword);
this.gbKeyInfo.Controls.Add(this.edPassphrase);
this.gbKeyInfo.Controls.Add(this.lbPassphrase);
this.gbKeyInfo.Location = new System.Drawing.Point(335, 225);
this.gbKeyInfo.Name = "gbKeyInfo";
this.gbKeyInfo.Size = new System.Drawing.Size(325, 150);
this.gbKeyInfo.TabIndex = 15;
this.gbKeyInfo.TabStop = false;
this.gbKeyInfo.Text = "Key/certificate properties ";
//
// lbCertificate
//
this.lbCertificate.AutoSize = true;
this.lbCertificate.Location = new System.Drawing.Point(7, 75);
this.lbCertificate.Name = "lbCertificate";
this.lbCertificate.Size = new System.Drawing.Size(109, 13);
this.lbCertificate.TabIndex = 65;
this.lbCertificate.Text = "Encryption certificate:";
//
// btnBrowseCert
//
this.btnBrowseCert.Location = new System.Drawing.Point(244, 89);
this.btnBrowseCert.Name = "btnBrowseCert";
this.btnBrowseCert.Size = new System.Drawing.Size(75, 25);
this.btnBrowseCert.TabIndex = 64;
this.btnBrowseCert.Text = "Browse ...";
this.btnBrowseCert.Click += new System.EventHandler(this.btnBrowseCert_Click);
//
// edCertPassword
//
this.edCertPassword.Location = new System.Drawing.Point(118, 123);
this.edCertPassword.Name = "edCertPassword";
this.edCertPassword.PasswordChar = '*';
this.edCertPassword.Size = new System.Drawing.Size(201, 20);
this.edCertPassword.TabIndex = 63;
//
// edCert
//
this.edCert.Location = new System.Drawing.Point(10, 92);
this.edCert.Name = "edCert";
this.edCert.Size = new System.Drawing.Size(228, 20);
this.edCert.TabIndex = 62;
//
// lbCertPassword
//
this.lbCertPassword.AutoSize = true;
this.lbCertPassword.Location = new System.Drawing.Point(7, 126);
this.lbCertPassword.Name = "lbCertPassword";
this.lbCertPassword.Size = new System.Drawing.Size(105, 13);
this.lbCertPassword.TabIndex = 61;
this.lbCertPassword.Text = "Certificate password:";
//
// edPassphrase
//
this.edPassphrase.Location = new System.Drawing.Point(10, 41);
this.edPassphrase.Name = "edPassphrase";
this.edPassphrase.Size = new System.Drawing.Size(309, 20);
this.edPassphrase.TabIndex = 1;
//
// lbPassphrase
//
this.lbPassphrase.AutoSize = true;
this.lbPassphrase.Location = new System.Drawing.Point(7, 25);
this.lbPassphrase.Name = "lbPassphrase";
this.lbPassphrase.Size = new System.Drawing.Size(79, 13);
this.lbPassphrase.TabIndex = 0;
this.lbPassphrase.Text = "Key/Password:";
//
// edOutputFile
//
this.edOutputFile.Location = new System.Drawing.Point(71, 70);
this.edOutputFile.Name = "edOutputFile";
this.edOutputFile.Size = new System.Drawing.Size(373, 20);
this.edOutputFile.TabIndex = 19;
//
// sbBrowseOutputFile
//
this.sbBrowseOutputFile.Location = new System.Drawing.Point(450, 67);
this.sbBrowseOutputFile.Name = "sbBrowseOutputFile";
this.sbBrowseOutputFile.Size = new System.Drawing.Size(75, 25);
this.sbBrowseOutputFile.TabIndex = 20;
this.sbBrowseOutputFile.Text = "Browse ...";
this.sbBrowseOutputFile.Click += new System.EventHandler(this.sbBrowseOutputFile_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(5, 73);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(60, 13);
this.label2.TabIndex = 18;
this.label2.Text = "Output file:";
//
// gbGeneralEnc
//
this.gbGeneralEnc.Controls.Add(this.lbExternalFile);
this.gbGeneralEnc.Controls.Add(this.btnBrowseExternalFile);
this.gbGeneralEnc.Controls.Add(this.edExternalFile);
this.gbGeneralEnc.Controls.Add(this.edMimeType);
this.gbGeneralEnc.Controls.Add(this.lbMimeType);
this.gbGeneralEnc.Controls.Add(this.edXMLNode);
this.gbGeneralEnc.Controls.Add(this.label1);
this.gbGeneralEnc.Controls.Add(this.cmbEncryptionMethod);
this.gbGeneralEnc.Controls.Add(this.lbEncryptionmethod);
this.gbGeneralEnc.Controls.Add(this.cmbEncryptedDataType);
this.gbGeneralEnc.Controls.Add(this.lbEncryptedDataType);
this.gbGeneralEnc.Location = new System.Drawing.Point(5, 100);
this.gbGeneralEnc.Name = "gbGeneralEnc";
this.gbGeneralEnc.Size = new System.Drawing.Size(655, 120);
this.gbGeneralEnc.TabIndex = 22;
this.gbGeneralEnc.TabStop = false;
this.gbGeneralEnc.Text = "Encryption properties ";
//
// lbExternalFile
//
this.lbExternalFile.AutoSize = true;
this.lbExternalFile.Location = new System.Drawing.Point(49, 93);
this.lbExternalFile.Name = "lbExternalFile";
this.lbExternalFile.Size = new System.Drawing.Size(67, 13);
this.lbExternalFile.TabIndex = 68;
this.lbExternalFile.Text = "External File:";
//
// btnBrowseExternalFile
//
this.btnBrowseExternalFile.Location = new System.Drawing.Point(500, 87);
this.btnBrowseExternalFile.Name = "btnBrowseExternalFile";
this.btnBrowseExternalFile.Size = new System.Drawing.Size(75, 25);
this.btnBrowseExternalFile.TabIndex = 67;
this.btnBrowseExternalFile.Text = "Browse ...";
this.btnBrowseExternalFile.Click += new System.EventHandler(this.btnBrowseExternalFile_Click);
//
// edExternalFile
//
this.edExternalFile.Location = new System.Drawing.Point(120, 90);
this.edExternalFile.Name = "edExternalFile";
this.edExternalFile.Size = new System.Drawing.Size(374, 20);
this.edExternalFile.TabIndex = 66;
//
// edMimeType
//
this.edMimeType.Location = new System.Drawing.Point(415, 60);
this.edMimeType.Name = "edMimeType";
this.edMimeType.Size = new System.Drawing.Size(204, 20);
this.edMimeType.TabIndex = 13;
//
// lbMimeType
//
this.lbMimeType.Location = new System.Drawing.Point(345, 63);
this.lbMimeType.Name = "lbMimeType";
this.lbMimeType.Size = new System.Drawing.Size(64, 16);
this.lbMimeType.TabIndex = 12;
this.lbMimeType.Text = "Mime Type:";
//
// edXMLNode
//
this.edXMLNode.Location = new System.Drawing.Point(120, 60);
this.edXMLNode.Name = "edXMLNode";
this.edXMLNode.Size = new System.Drawing.Size(200, 20);
this.edXMLNode.TabIndex = 11;
//
// label1
//
this.label1.Location = new System.Drawing.Point(52, 63);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 10;
this.label1.Text = "XML Node:";
//
// cmbEncryptionMethod
//
this.cmbEncryptionMethod.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbEncryptionMethod.Items.AddRange(new object[] {
"3DES",
"AES128",
"AES192",
"AES256",
"Camellia128",
"Camellia192",
"Camellia256",
"DES",
"RC4",
"SEED"});
this.cmbEncryptionMethod.Location = new System.Drawing.Point(415, 27);
this.cmbEncryptionMethod.Name = "cmbEncryptionMethod";
this.cmbEncryptionMethod.Size = new System.Drawing.Size(214, 21);
this.cmbEncryptionMethod.TabIndex = 9;
//
// lbEncryptionmethod
//
this.lbEncryptionmethod.AutoSize = true;
this.lbEncryptionmethod.Location = new System.Drawing.Point(311, 30);
this.lbEncryptionmethod.Name = "lbEncryptionmethod";
this.lbEncryptionmethod.Size = new System.Drawing.Size(98, 13);
this.lbEncryptionmethod.TabIndex = 8;
this.lbEncryptionmethod.Text = "Encryption method:";
//
// cmbEncryptedDataType
//
this.cmbEncryptedDataType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbEncryptedDataType.Items.AddRange(new object[] {
"Element",
"Content",
"External File"});
this.cmbEncryptedDataType.Location = new System.Drawing.Point(120, 27);
this.cmbEncryptedDataType.Name = "cmbEncryptedDataType";
this.cmbEncryptedDataType.Size = new System.Drawing.Size(161, 21);
this.cmbEncryptedDataType.TabIndex = 4;
this.cmbEncryptedDataType.SelectedIndexChanged += new System.EventHandler(this.cmbEncryptedDataType_SelectedIndexChanged);
//
// lbEncryptedDataType
//
this.lbEncryptedDataType.AutoSize = true;
this.lbEncryptedDataType.Location = new System.Drawing.Point(5, 30);
this.lbEncryptedDataType.Name = "lbEncryptedDataType";
this.lbEncryptedDataType.Size = new System.Drawing.Size(111, 13);
this.lbEncryptedDataType.TabIndex = 2;
this.lbEncryptedDataType.Text = "Encrypted Data Type:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.ForeColor = System.Drawing.SystemColors.Highlight;
this.label3.Location = new System.Drawing.Point(5, 5);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(537, 26);
this.label3.TabIndex = 23;
this.label3.Text = resources.GetString("label3.Text");
//
// groupBox1
//
this.groupBox1.Controls.Add(this.cmbKeyWrap);
this.groupBox1.Controls.Add(this.lbKeyWrap);
this.groupBox1.Controls.Add(this.cmbKeyTransport);
this.groupBox1.Controls.Add(this.lbKeyTransport);
this.groupBox1.Controls.Add(this.gbKEK);
this.groupBox1.Controls.Add(this.cbEncryptKey);
this.groupBox1.Location = new System.Drawing.Point(5, 225);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(325, 170);
this.groupBox1.TabIndex = 24;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Key Encryption Key (KEK) properties ";
//
// cmbKeyWrap
//
this.cmbKeyWrap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbKeyWrap.Items.AddRange(new object[] {
"3DES",
"AES128",
"AES192",
"AES256",
"Camellia128",
"Camellia192",
"Camellia256",
"SEED"});
this.cmbKeyWrap.Location = new System.Drawing.Point(89, 140);
this.cmbKeyWrap.Name = "cmbKeyWrap";
this.cmbKeyWrap.Size = new System.Drawing.Size(192, 21);
this.cmbKeyWrap.TabIndex = 8;
//
// lbKeyWrap
//
this.lbKeyWrap.AutoSize = true;
this.lbKeyWrap.Location = new System.Drawing.Point(26, 143);
this.lbKeyWrap.Name = "lbKeyWrap";
this.lbKeyWrap.Size = new System.Drawing.Size(57, 13);
this.lbKeyWrap.TabIndex = 7;
this.lbKeyWrap.Text = "Key Wrap:";
//
// cmbKeyTransport
//
this.cmbKeyTransport.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbKeyTransport.Items.AddRange(new object[] {
"RSA v1.5",
"RSA-OAEP"});
this.cmbKeyTransport.Location = new System.Drawing.Point(89, 110);
this.cmbKeyTransport.Name = "cmbKeyTransport";
this.cmbKeyTransport.Size = new System.Drawing.Size(192, 21);
this.cmbKeyTransport.TabIndex = 6;
//
// lbKeyTransport
//
this.lbKeyTransport.AutoSize = true;
this.lbKeyTransport.Location = new System.Drawing.Point(7, 113);
this.lbKeyTransport.Name = "lbKeyTransport";
this.lbKeyTransport.Size = new System.Drawing.Size(76, 13);
this.lbKeyTransport.TabIndex = 5;
this.lbKeyTransport.Text = "Key Transport:";
//
// gbKEK
//
this.gbKEK.Controls.Add(this.rbKeyWrap);
this.gbKEK.Controls.Add(this.rbKeyTransport);
this.gbKEK.Location = new System.Drawing.Point(10, 50);
this.gbKEK.Name = "gbKEK";
this.gbKEK.Size = new System.Drawing.Size(271, 49);
this.gbKEK.TabIndex = 1;
this.gbKEK.TabStop = false;
this.gbKEK.Text = "Key Encryption Key type:";
//
// rbKeyWrap
//
this.rbKeyWrap.AutoSize = true;
this.rbKeyWrap.Checked = true;
this.rbKeyWrap.Location = new System.Drawing.Point(153, 22);
this.rbKeyWrap.Name = "rbKeyWrap";
this.rbKeyWrap.Size = new System.Drawing.Size(72, 17);
this.rbKeyWrap.TabIndex = 1;
this.rbKeyWrap.TabStop = true;
this.rbKeyWrap.Text = "Key Wrap";
this.rbKeyWrap.UseVisualStyleBackColor = true;
this.rbKeyWrap.CheckedChanged += new System.EventHandler(this.rbKeyWrap_CheckedChanged);
//
// rbKeyTransport
//
this.rbKeyTransport.AutoSize = true;
this.rbKeyTransport.Location = new System.Drawing.Point(13, 22);
this.rbKeyTransport.Name = "rbKeyTransport";
this.rbKeyTransport.Size = new System.Drawing.Size(91, 17);
this.rbKeyTransport.TabIndex = 0;
this.rbKeyTransport.Text = "Key Transport";
this.rbKeyTransport.UseVisualStyleBackColor = true;
this.rbKeyTransport.CheckedChanged += new System.EventHandler(this.rbKeyTransport_CheckedChanged);
//
// cbEncryptKey
//
this.cbEncryptKey.AutoSize = true;
this.cbEncryptKey.Location = new System.Drawing.Point(10, 25);
this.cbEncryptKey.Name = "cbEncryptKey";
this.cbEncryptKey.Size = new System.Drawing.Size(83, 17);
this.cbEncryptKey.TabIndex = 0;
this.cbEncryptKey.Text = "Encrypt Key";
this.cbEncryptKey.UseVisualStyleBackColor = true;
this.cbEncryptKey.CheckedChanged += new System.EventHandler(this.cbEncryptKey_CheckedChanged);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(664, 411);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.gbGeneralEnc);
this.Controls.Add(this.edOutputFile);
this.Controls.Add(this.sbBrowseOutputFile);
this.Controls.Add(this.label2);
this.Controls.Add(this.gbKeyInfo);
this.Controls.Add(this.edXMLFile);
this.Controls.Add(this.btnEncrypt);
this.Controls.Add(this.sbBrowseXMLFile);
this.Controls.Add(this.lbXMLFile);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "XML Encryptor demo";
this.gbKeyInfo.ResumeLayout(false);
this.gbKeyInfo.PerformLayout();
this.gbGeneralEnc.ResumeLayout(false);
this.gbGeneralEnc.PerformLayout();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.gbKEK.ResumeLayout(false);
this.gbKEK.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 updateSettings()
{
gbKEK.Enabled = cbEncryptKey.Checked;
cmbKeyTransport.Enabled = cbEncryptKey.Checked;
lbKeyTransport.Enabled = cmbKeyTransport.Enabled;
cmbKeyWrap.Enabled = cbEncryptKey.Checked;
lbKeyWrap.Enabled = cmbKeyWrap.Enabled;
cmbKeyTransport.Enabled = cbEncryptKey.Checked && rbKeyTransport.Checked;
lbKeyTransport.Enabled = cmbKeyTransport.Enabled;
cmbKeyWrap.Enabled = cbEncryptKey.Checked && rbKeyWrap.Checked;
lbKeyWrap.Enabled = cmbKeyWrap.Enabled;
edPassphrase.Enabled = cmbKeyWrap.Enabled || !cbEncryptKey.Checked;
lbPassphrase.Enabled = edPassphrase.Enabled;
edCert.Enabled = cmbKeyTransport.Enabled;
lbCertificate.Enabled = edCert.Enabled;
edCertPassword.Enabled = edCert.Enabled;
lbCertPassword.Enabled = edCert.Enabled;
btnBrowseCert.Enabled = edCert.Enabled;
edMimeType.Enabled = (cmbEncryptedDataType.SelectedIndex == 2);
lbMimeType.Enabled = edMimeType.Enabled;
edExternalFile.Enabled = (cmbEncryptedDataType.SelectedIndex == 2);
lbExternalFile.Enabled = edExternalFile.Enabled;
btnBrowseExternalFile.Enabled = edExternalFile.Enabled;
}
private byte[] getKey(string algorithm)
{
int len = 0;
if (algorithm.Equals("AES128", StringComparison.OrdinalIgnoreCase))
len = 16;
else if (algorithm.Equals("AES192", StringComparison.OrdinalIgnoreCase))
len = 24;
else if (algorithm.Equals("AES256", StringComparison.OrdinalIgnoreCase))
len = 32;
else if (algorithm.Equals("Camellia128", StringComparison.OrdinalIgnoreCase))
len = 16;
else if (algorithm.Equals("Camellia192", StringComparison.OrdinalIgnoreCase))
len = 24;
else if (algorithm.Equals("Camellia256", StringComparison.OrdinalIgnoreCase))
len = 32;
else if (algorithm.Equals("DES", StringComparison.OrdinalIgnoreCase))
len = 8;
else if (algorithm.Equals("3DES", StringComparison.OrdinalIgnoreCase))
len = 24;
else if (algorithm.Equals("RC4", StringComparison.OrdinalIgnoreCase))
len = 16;
else if (algorithm.Equals("SEED", StringComparison.OrdinalIgnoreCase))
len = 16;
// simple key derivation function from a Passphrase
// TODO: replace with SHA256 hash or KDF
string s = edPassphrase.Text;
while (s.Length < len)
s = s + "/" + edPassphrase.Text;
byte[] res = new byte[len];
for (int i = 0; i < len; i++)
res[i] = (byte)s[i];
return res;
}
private void sbBrowseXMLFile_Click(object sender, System.EventArgs e)
{
dlgOpen.Filter = "XML file (*.xml)|*.XML";
if (dlgOpen.ShowDialog() == DialogResult.OK)
edXMLFile.Text = dlgOpen.FileName;
}
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 sbBrowseOutputFile_Click(object sender, EventArgs e)
{
dlgOpen.Filter = "";
if (dlgSave.ShowDialog() == DialogResult.OK)
edOutputFile.Text = dlgSave.FileName;
}
private void btnBrowseCert_Click(object sender, EventArgs e)
{
if (dlgOpen.ShowDialog() == DialogResult.OK)
edCert.Text = dlgOpen.FileName;
}
private void cmbEncryptedDataType_SelectedIndexChanged(object sender, EventArgs e)
{
updateSettings();
}
private void cbEncryptKey_CheckedChanged(object sender, EventArgs e)
{
updateSettings();
}
private void rbKeyTransport_CheckedChanged(object sender, EventArgs e)
{
updateSettings();
}
private void rbKeyWrap_CheckedChanged(object sender, EventArgs e)
{
updateSettings();
}
private void btnEncrypt_Click(object sender, EventArgs e)
{
encryptor.InputFile = edXMLFile.Text;
encryptor.OutputFile = edOutputFile.Text;
encryptor.EncryptKey = cbEncryptKey.Checked;
switch (cmbEncryptedDataType.SelectedIndex)
{
case 1: encryptor.EncryptedDataType = XMLEncryptorEncryptedDataTypes.cxedtContent; break;
case 2:
encryptor.EncryptedDataType = XMLEncryptorEncryptedDataTypes.cxedtExternal;
if (edMimeType.Text.Length > 0)
encryptor.Config("MimeType=" + edMimeType.Text);
encryptor.ExternalData = File.ReadAllBytes(edExternalFile.Text);
break;
default: encryptor.EncryptedDataType = XMLEncryptorEncryptedDataTypes.cxedtElement; break;
}
encryptor.EncryptionMethod = cmbEncryptionMethod.Text;
encryptor.UseGCM = false;
encryptor.XMLNode = edXMLNode.Text;
if (encryptor.EncryptKey)
{
if (rbKeyTransport.Checked)
{
encryptor.KeyEncryptionType = XMLEncryptorKeyEncryptionTypes.cxetKeyTransport;
if (cmbKeyTransport.SelectedIndex == 0)
encryptor.KeyTransportMethod = XMLEncryptorKeyTransportMethods.cxktRSA15;
else
encryptor.KeyTransportMethod = XMLEncryptorKeyTransportMethods.cxktRSAOAEP;
encryptor.KeyEncryptionCertificate = LoadCertificate(edCert.Text, edCertPassword.Text);
}
else
{
encryptor.KeyEncryptionType = XMLEncryptorKeyEncryptionTypes.cxetKeyWrap;
encryptor.KeyWrapMethod = cmbKeyWrap.Text;
encryptor.KeyEncryptionKey = getKey(encryptor.KeyWrapMethod);
}
}
else
{
encryptor.EncryptionKey = getKey(encryptor.EncryptionMethod);
}
// Enable automatic formatting
encryptor.Config("XMLFormatting=auto");
try
{
encryptor.Encrypt();
MessageBox.Show("XML file successfully encrypted");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnBrowseExternalFile_Click(object sender, EventArgs e)
{
dlgOpen.Filter = "";
if (dlgSave.ShowDialog() == DialogResult.OK)
edExternalFile.Text = dlgSave.FileName;
}
}
}