securebb-win-demos/Public Key Crypto (WinForms)/publickeycrypto.cs

648 lines
26 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;
namespace SignDetached
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmMainForm : System.Windows.Forms.Form
{
internal System.Windows.Forms.GroupBox gbSettings;
internal System.Windows.Forms.Label lblInputEncoding;
internal System.Windows.Forms.ComboBox comboEncoding;
internal System.Windows.Forms.Button btnBrowseInputFile;
internal System.Windows.Forms.TextBox tbInputFile;
internal System.Windows.Forms.Label lblInputFilename;
internal System.Windows.Forms.TextBox tbSignatureFile;
internal System.Windows.Forms.Label lblSignatureFilename;
internal System.Windows.Forms.Button btnBrowseOutputFile;
internal System.Windows.Forms.ComboBox comboKeyAlg;
internal System.Windows.Forms.Label lblKeyAlg;
internal System.Windows.Forms.Button btnBrowseKeyFile;
internal System.Windows.Forms.Label lblPassword;
internal System.Windows.Forms.TextBox tbKeyFile;
internal System.Windows.Forms.TextBox tbPassphrase;
internal System.Windows.Forms.Label lblKeyFilename;
internal System.Windows.Forms.Button btnGo;
internal System.Windows.Forms.OpenFileDialog dlgOpenKeyFile;
internal System.Windows.Forms.OpenFileDialog dlgOpenFile;
internal System.Windows.Forms.SaveFileDialog dlgSaveFile;
private System.Windows.Forms.Label lblKeyContainerType;
private System.Windows.Forms.ComboBox comboKeyContainerType;
private RadioButton rbVerify;
private RadioButton rbSign;
private Label label9;
internal ComboBox comboCurve;
internal Label lblCurve;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmMainForm()
{
//
// 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.btnGo = new System.Windows.Forms.Button();
this.gbSettings = new System.Windows.Forms.GroupBox();
this.comboCurve = new System.Windows.Forms.ComboBox();
this.lblCurve = new System.Windows.Forms.Label();
this.rbVerify = new System.Windows.Forms.RadioButton();
this.rbSign = new System.Windows.Forms.RadioButton();
this.comboKeyContainerType = new System.Windows.Forms.ComboBox();
this.lblKeyContainerType = new System.Windows.Forms.Label();
this.lblInputEncoding = new System.Windows.Forms.Label();
this.comboEncoding = new System.Windows.Forms.ComboBox();
this.btnBrowseInputFile = new System.Windows.Forms.Button();
this.tbInputFile = new System.Windows.Forms.TextBox();
this.lblInputFilename = new System.Windows.Forms.Label();
this.tbSignatureFile = new System.Windows.Forms.TextBox();
this.lblSignatureFilename = new System.Windows.Forms.Label();
this.btnBrowseOutputFile = new System.Windows.Forms.Button();
this.comboKeyAlg = new System.Windows.Forms.ComboBox();
this.lblKeyAlg = new System.Windows.Forms.Label();
this.btnBrowseKeyFile = new System.Windows.Forms.Button();
this.lblPassword = new System.Windows.Forms.Label();
this.tbKeyFile = new System.Windows.Forms.TextBox();
this.tbPassphrase = new System.Windows.Forms.TextBox();
this.lblKeyFilename = new System.Windows.Forms.Label();
this.dlgOpenKeyFile = new System.Windows.Forms.OpenFileDialog();
this.dlgOpenFile = new System.Windows.Forms.OpenFileDialog();
this.dlgSaveFile = new System.Windows.Forms.SaveFileDialog();
this.label9 = new System.Windows.Forms.Label();
this.gbSettings.SuspendLayout();
this.SuspendLayout();
//
// btnGo
//
this.btnGo.Location = new System.Drawing.Point(378, 403);
this.btnGo.Name = "btnGo";
this.btnGo.Size = new System.Drawing.Size(70, 25);
this.btnGo.TabIndex = 4;
this.btnGo.Text = "Sign";
this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
//
// gbSettings
//
this.gbSettings.Controls.Add(this.comboCurve);
this.gbSettings.Controls.Add(this.lblCurve);
this.gbSettings.Controls.Add(this.rbVerify);
this.gbSettings.Controls.Add(this.rbSign);
this.gbSettings.Controls.Add(this.comboKeyContainerType);
this.gbSettings.Controls.Add(this.lblKeyContainerType);
this.gbSettings.Controls.Add(this.lblInputEncoding);
this.gbSettings.Controls.Add(this.comboEncoding);
this.gbSettings.Controls.Add(this.btnBrowseInputFile);
this.gbSettings.Controls.Add(this.tbInputFile);
this.gbSettings.Controls.Add(this.lblInputFilename);
this.gbSettings.Controls.Add(this.tbSignatureFile);
this.gbSettings.Controls.Add(this.lblSignatureFilename);
this.gbSettings.Controls.Add(this.btnBrowseOutputFile);
this.gbSettings.Controls.Add(this.comboKeyAlg);
this.gbSettings.Controls.Add(this.lblKeyAlg);
this.gbSettings.Controls.Add(this.btnBrowseKeyFile);
this.gbSettings.Controls.Add(this.lblPassword);
this.gbSettings.Controls.Add(this.tbKeyFile);
this.gbSettings.Controls.Add(this.tbPassphrase);
this.gbSettings.Controls.Add(this.lblKeyFilename);
this.gbSettings.Location = new System.Drawing.Point(8, 34);
this.gbSettings.Name = "gbSettings";
this.gbSettings.Size = new System.Drawing.Size(440, 361);
this.gbSettings.TabIndex = 3;
this.gbSettings.TabStop = false;
this.gbSettings.Text = "Settings";
//
// comboCurve
//
this.comboCurve.DisplayMember = "0";
this.comboCurve.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboCurve.ItemHeight = 13;
this.comboCurve.Items.AddRange(new object[] {
"",
"SECP112R1",
"SECT113R1",
"SECP128R1",
"SECT131R1",
"SECP160K1",
"SECT163K1",
"C2PNB176W1",
"C2TNB191V1",
"SECP192K1",
"SECT193R1",
"C2PNB208W1",
"SECP224K1",
"SECT233K1",
"SECT239K1",
"SECP256K1",
"C2PNB272W1",
"SECT283K1",
"C2PNB304W1",
"C2TNB359V1",
"C2PNB368W1",
"SECP384R1",
"SECT409K1",
"C2TNB431R1",
"BRAINPOOLP512R1",
"SECP521R1",
"SECT571K1"});
this.comboCurve.Location = new System.Drawing.Point(312, 224);
this.comboCurve.Name = "comboCurve";
this.comboCurve.Size = new System.Drawing.Size(92, 21);
this.comboCurve.TabIndex = 16;
//
// lblCurve
//
this.lblCurve.AutoSize = true;
this.lblCurve.Location = new System.Drawing.Point(312, 208);
this.lblCurve.Name = "lblCurve";
this.lblCurve.Size = new System.Drawing.Size(54, 13);
this.lblCurve.TabIndex = 17;
this.lblCurve.Text = "EC curve:";
//
// rbVerify
//
this.rbVerify.AutoSize = true;
this.rbVerify.Location = new System.Drawing.Point(105, 25);
this.rbVerify.Name = "rbVerify";
this.rbVerify.Size = new System.Drawing.Size(53, 17);
this.rbVerify.TabIndex = 15;
this.rbVerify.Text = "Verify";
this.rbVerify.UseVisualStyleBackColor = true;
this.rbVerify.CheckedChanged += new System.EventHandler(this.rbSign_CheckedChanged);
//
// rbSign
//
this.rbSign.AutoSize = true;
this.rbSign.Checked = true;
this.rbSign.Location = new System.Drawing.Point(16, 25);
this.rbSign.Name = "rbSign";
this.rbSign.Size = new System.Drawing.Size(45, 17);
this.rbSign.TabIndex = 14;
this.rbSign.TabStop = true;
this.rbSign.Text = "Sign";
this.rbSign.UseVisualStyleBackColor = true;
this.rbSign.CheckedChanged += new System.EventHandler(this.rbSign_CheckedChanged);
//
// comboKeyContainerType
//
this.comboKeyContainerType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboKeyContainerType.Items.AddRange(new object[] {
"Generic private key",
"X.509 certificate"});
this.comboKeyContainerType.Location = new System.Drawing.Point(16, 224);
this.comboKeyContainerType.Name = "comboKeyContainerType";
this.comboKeyContainerType.Size = new System.Drawing.Size(142, 21);
this.comboKeyContainerType.TabIndex = 13;
this.comboKeyContainerType.SelectedIndexChanged += new System.EventHandler(this.comboKeyContainerType_SelectedIndexChanged);
//
// lblKeyContainerType
//
this.lblKeyContainerType.AutoSize = true;
this.lblKeyContainerType.Location = new System.Drawing.Point(16, 208);
this.lblKeyContainerType.Name = "lblKeyContainerType";
this.lblKeyContainerType.Size = new System.Drawing.Size(102, 13);
this.lblKeyContainerType.TabIndex = 12;
this.lblKeyContainerType.Text = "Key container type:";
//
// lblInputEncoding
//
this.lblInputEncoding.AutoSize = true;
this.lblInputEncoding.Location = new System.Drawing.Point(16, 157);
this.lblInputEncoding.Name = "lblInputEncoding";
this.lblInputEncoding.Size = new System.Drawing.Size(54, 13);
this.lblInputEncoding.TabIndex = 6;
this.lblInputEncoding.Text = "Encoding:";
//
// comboEncoding
//
this.comboEncoding.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboEncoding.ItemHeight = 13;
this.comboEncoding.Items.AddRange(new object[] {
"Binary",
"Base64",
"Compact",
"JSON"});
this.comboEncoding.Location = new System.Drawing.Point(16, 175);
this.comboEncoding.Name = "comboEncoding";
this.comboEncoding.Size = new System.Drawing.Size(88, 21);
this.comboEncoding.TabIndex = 5;
//
// btnBrowseInputFile
//
this.btnBrowseInputFile.Location = new System.Drawing.Point(360, 68);
this.btnBrowseInputFile.Name = "btnBrowseInputFile";
this.btnBrowseInputFile.Size = new System.Drawing.Size(70, 25);
this.btnBrowseInputFile.TabIndex = 1;
this.btnBrowseInputFile.Text = "Browse ...";
this.btnBrowseInputFile.Click += new System.EventHandler(this.btnBrowseInputFile_Click);
//
// tbInputFile
//
this.tbInputFile.Location = new System.Drawing.Point(16, 71);
this.tbInputFile.Name = "tbInputFile";
this.tbInputFile.Size = new System.Drawing.Size(336, 21);
this.tbInputFile.TabIndex = 0;
//
// lblInputFilename
//
this.lblInputFilename.Location = new System.Drawing.Point(16, 55);
this.lblInputFilename.Name = "lblInputFilename";
this.lblInputFilename.Size = new System.Drawing.Size(112, 16);
this.lblInputFilename.TabIndex = 1;
this.lblInputFilename.Text = "Input filename:";
//
// tbSignatureFile
//
this.tbSignatureFile.Location = new System.Drawing.Point(16, 119);
this.tbSignatureFile.Name = "tbSignatureFile";
this.tbSignatureFile.Size = new System.Drawing.Size(336, 21);
this.tbSignatureFile.TabIndex = 2;
//
// lblSignatureFilename
//
this.lblSignatureFilename.Location = new System.Drawing.Point(16, 103);
this.lblSignatureFilename.Name = "lblSignatureFilename";
this.lblSignatureFilename.Size = new System.Drawing.Size(112, 16);
this.lblSignatureFilename.TabIndex = 1;
this.lblSignatureFilename.Text = "Output filename:";
//
// btnBrowseOutputFile
//
this.btnBrowseOutputFile.Location = new System.Drawing.Point(360, 116);
this.btnBrowseOutputFile.Name = "btnBrowseOutputFile";
this.btnBrowseOutputFile.Size = new System.Drawing.Size(70, 25);
this.btnBrowseOutputFile.TabIndex = 3;
this.btnBrowseOutputFile.Text = "Browse ...";
this.btnBrowseOutputFile.Click += new System.EventHandler(this.btnBrowseOutputFile_Click);
//
// comboKeyAlg
//
this.comboKeyAlg.DisplayMember = "0";
this.comboKeyAlg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboKeyAlg.ItemHeight = 13;
this.comboKeyAlg.Items.AddRange(new object[] {
"",
"RSA",
"DSA",
"EC",
"ECDSA",
"DH",
"EDDSA"});
this.comboKeyAlg.Location = new System.Drawing.Point(179, 224);
this.comboKeyAlg.Name = "comboKeyAlg";
this.comboKeyAlg.Size = new System.Drawing.Size(104, 21);
this.comboKeyAlg.TabIndex = 4;
this.comboKeyAlg.SelectedIndexChanged += new System.EventHandler(this.comboKeyAlg_SelectedIndexChanged);
//
// lblKeyAlg
//
this.lblKeyAlg.AutoSize = true;
this.lblKeyAlg.Location = new System.Drawing.Point(179, 208);
this.lblKeyAlg.Name = "lblKeyAlg";
this.lblKeyAlg.Size = new System.Drawing.Size(76, 13);
this.lblKeyAlg.TabIndex = 11;
this.lblKeyAlg.Text = "Key algorithm:";
//
// btnBrowseKeyFile
//
this.btnBrowseKeyFile.Location = new System.Drawing.Point(360, 265);
this.btnBrowseKeyFile.Name = "btnBrowseKeyFile";
this.btnBrowseKeyFile.Size = new System.Drawing.Size(70, 25);
this.btnBrowseKeyFile.TabIndex = 8;
this.btnBrowseKeyFile.Text = "Browse ...";
this.btnBrowseKeyFile.Click += new System.EventHandler(this.btnBrowseKeyFile_Click);
//
// lblPassword
//
this.lblPassword.Location = new System.Drawing.Point(16, 300);
this.lblPassword.Name = "lblPassword";
this.lblPassword.Size = new System.Drawing.Size(100, 16);
this.lblPassword.TabIndex = 1;
this.lblPassword.Text = "Password:";
//
// tbKeyFile
//
this.tbKeyFile.Location = new System.Drawing.Point(16, 268);
this.tbKeyFile.Name = "tbKeyFile";
this.tbKeyFile.Size = new System.Drawing.Size(336, 21);
this.tbKeyFile.TabIndex = 7;
//
// tbPassphrase
//
this.tbPassphrase.Location = new System.Drawing.Point(16, 316);
this.tbPassphrase.Name = "tbPassphrase";
this.tbPassphrase.PasswordChar = '*';
this.tbPassphrase.Size = new System.Drawing.Size(144, 21);
this.tbPassphrase.TabIndex = 10;
//
// lblKeyFilename
//
this.lblKeyFilename.Location = new System.Drawing.Point(16, 252);
this.lblKeyFilename.Name = "lblKeyFilename";
this.lblKeyFilename.Size = new System.Drawing.Size(176, 16);
this.lblKeyFilename.TabIndex = 0;
this.lblKeyFilename.Text = "Private key container file:";
//
// dlgOpenKeyFile
//
this.dlgOpenKeyFile.Filter = "All key and certificate files (*.*)|*.*";
//
// label9
//
this.label9.ForeColor = System.Drawing.SystemColors.Highlight;
this.label9.Location = new System.Drawing.Point(5, 9);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(383, 16);
this.label9.TabIndex = 55;
this.label9.Text = "This sample shows how to detached sign and verify signature.";
//
// frmMainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.ClientSize = new System.Drawing.Size(458, 436);
this.Controls.Add(this.label9);
this.Controls.Add(this.btnGo);
this.Controls.Add(this.gbSettings);
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 = "frmMainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Public key crypto Demo";
this.Load += new System.EventHandler(this.frmMainForm_Load);
this.gbSettings.ResumeLayout(false);
this.gbSettings.PerformLayout();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMainForm());
}
private void DoPasswordNeeded(object sender, CryptoKeyManagerPasswordNeededEventArgs e)
{
e.Password = tbPassphrase.Text;
e.Cancel = false;
}
private void LoadKeyFromFile(PublicKeyCrypto crypto)
{
CryptoKeyManager keymanager = new CryptoKeyManager();
keymanager.OnPasswordNeeded += new CryptoKeyManager.OnPasswordNeededHandler(DoPasswordNeeded);
if (comboKeyContainerType.SelectedIndex == 1)
{
CertificateManager certmanager = new CertificateManager();
certmanager.ImportFromFile(tbKeyFile.Text, tbPassphrase.Text);
keymanager.Certificate = certmanager.Certificate;
keymanager.ImportFromCert();
crypto.Key = keymanager.Key;
}
else
{
keymanager.ImportFromFile(tbKeyFile.Text, 1, comboKeyAlg.Text, comboCurve.Text, "", 0, tbPassphrase.Text); // Format = kffAuto, KeyType = ktAuto
crypto.Key = keymanager.Key;
}
}
private void DoSignDetached()
{
PublicKeyCrypto crypto = new PublicKeyCrypto();
try
{
switch (comboEncoding.SelectedIndex)
{
case 1: crypto.OutputEncoding = PublicKeyCryptoOutputEncodings.cetBase64; break;
case 2: crypto.OutputEncoding = PublicKeyCryptoOutputEncodings.cetCompact; break;
case 3: crypto.OutputEncoding = PublicKeyCryptoOutputEncodings.cetJSON; break;
default: crypto.OutputEncoding = PublicKeyCryptoOutputEncodings.cetBinary; break;
}
// loading key
LoadKeyFromFile(crypto);
// signing input data
crypto.SignFile(tbInputFile.Text, tbSignatureFile.Text, true);
MessageBox.Show("The file was signed successfully", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DoVerifyDetached()
{
PublicKeyCrypto crypto = new PublicKeyCrypto();
try
{
switch (comboEncoding.SelectedIndex)
{
case 1: crypto.InputEncoding = PublicKeyCryptoInputEncodings.cetBase64; break;
case 2: crypto.InputEncoding = PublicKeyCryptoInputEncodings.cetCompact; break;
case 3: crypto.InputEncoding = PublicKeyCryptoInputEncodings.cetJSON; break;
default: crypto.InputEncoding = PublicKeyCryptoInputEncodings.cetBinary; break;
}
// loading key
LoadKeyFromFile(crypto);
// verifying input data
crypto.VerifyDetachedFile(tbInputFile.Text, tbSignatureFile.Text);
switch (crypto.SignatureValidationResult)
{
case PublicKeyCryptoSignatureValidationResults.svtValid: MessageBox.Show("Verification succeeded", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information); break;
case PublicKeyCryptoSignatureValidationResults.svtCorrupted: MessageBox.Show("Verification corrupted", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); break;
case PublicKeyCryptoSignatureValidationResults.svtFailure: MessageBox.Show("Verification failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); break;
default: MessageBox.Show("Verification unknown", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); break;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnBrowseInputFile_Click(object sender, System.EventArgs e)
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
tbInputFile.Text = dlgOpenFile.FileName;
}
}
private void btnBrowseOutputFile_Click(object sender, System.EventArgs e)
{
if (rbSign.Checked)
{
if (dlgSaveFile.ShowDialog() == DialogResult.OK)
{
tbSignatureFile.Text = dlgSaveFile.FileName;
}
}
else
{
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
tbSignatureFile.Text = dlgOpenFile.FileName;
}
}
}
private void btnBrowseKeyFile_Click(object sender, System.EventArgs e)
{
if (dlgOpenKeyFile.ShowDialog() == DialogResult.OK)
{
tbKeyFile.Text = dlgOpenKeyFile.FileName;
}
}
private void frmMainForm_Load(object sender, System.EventArgs e)
{
comboEncoding.SelectedIndex = 0;
comboKeyContainerType.SelectedIndex = 0;
comboKeyAlg.SelectedIndex = 0;
}
private void rbSign_CheckedChanged(object sender, EventArgs e)
{
if (rbSign.Checked)
{
lblSignatureFilename.Text = "Output filename:";
btnGo.Text = "Sign";
}
else
{
lblSignatureFilename.Text = "Signature filename:";
btnGo.Text = "Verify";
}
}
private void comboKeyContainerType_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboKeyContainerType.SelectedIndex == 0)
{
lblKeyAlg.Enabled = true;
comboKeyAlg.Enabled = true;
comboKeyAlg.SelectedIndex = 0;
}
else
{
lblKeyAlg.Enabled = false;
comboKeyAlg.Enabled = false;
comboKeyAlg.SelectedIndex = 0;
}
}
private void comboKeyAlg_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboKeyAlg.Text == "EC")
{
lblCurve.Enabled = comboKeyAlg.Enabled;
comboCurve.Enabled = comboKeyAlg.Enabled;
comboCurve.SelectedIndex = 0;
}
else
{
lblCurve.Enabled = false;
comboCurve.Enabled = false;
comboCurve.SelectedIndex = 0;
}
}
private void btnGo_Click(object sender, EventArgs e)
{
if (!System.IO.File.Exists(tbInputFile.Text))
MessageBox.Show("Source file not found");
else if (tbSignatureFile.Text == "")
MessageBox.Show("Please, select output file");
else if (!System.IO.File.Exists(tbKeyFile.Text))
MessageBox.Show("Key or certificate file not found");
else
{
if (rbSign.Checked)
{
DoSignDetached();
}
else
{
if (!System.IO.File.Exists(tbSignatureFile.Text))
{
MessageBox.Show("Signature file not found");
}
else
{
DoVerifyDetached();
}
}
}
}
}
}