securebb-win-demos/XML Decryptor (WinForms)/xmldecryptor.cs
2024-08-08 13:09:34 +06:00

247 lines
9.0 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 System.Collections.Generic;
using nsoftware.SecureBlackbox;
using System.IO;
/// <summary>
/// Summary description for frmMain.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog dlgOpen;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private XMLDecryptor decryptor;
private TextBox edOutputFile;
private Button sbBrowseOutputFile;
private Label lDataFile;
private TextBox edInputFile;
private Button btnDecrypt;
private Button sbBrowseInputFile;
private Label lbInputFile;
private Label label1;
private SaveFileDialog dlgSave;
public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
decryptor = new XMLDecryptor();
decryptor.OnDecryptionInfoNeeded += new XMLDecryptor.OnDecryptionInfoNeededHandler(DecryptionInfoNeeded);
decryptor.OnSaveExternalData += new XMLDecryptor.OnSaveExternalDataHandler(SaveExternalData);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
decryptor.Dispose();
}
}
base.Dispose(disposing);
}
private void DecryptionInfoNeeded(object s, XMLDecryptorDecryptionInfoNeededEventArgs e)
{
DecryptPropsForm decryptProps = new DecryptPropsForm((XMLDecryptor)s);
e.CancelDecryption = (decryptProps.ShowDialog() != DialogResult.OK);
}
private void SaveExternalData(object s, XMLDecryptorSaveExternalDataEventArgs e)
{
dlgSave.InitialDirectory = Application.StartupPath;
dlgSave.FileName = edOutputFile.Text;
if (dlgSave.ShowDialog() == DialogResult.OK)
File.WriteAllBytes(dlgSave.FileName, e.ExternalData);
}
#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.dlgOpen = new System.Windows.Forms.OpenFileDialog();
this.edOutputFile = new System.Windows.Forms.TextBox();
this.sbBrowseOutputFile = new System.Windows.Forms.Button();
this.lDataFile = new System.Windows.Forms.Label();
this.edInputFile = new System.Windows.Forms.TextBox();
this.btnDecrypt = new System.Windows.Forms.Button();
this.sbBrowseInputFile = new System.Windows.Forms.Button();
this.lbInputFile = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.dlgSave = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// edOutputFile
//
this.edOutputFile.Location = new System.Drawing.Point(70, 87);
this.edOutputFile.Name = "edOutputFile";
this.edOutputFile.Size = new System.Drawing.Size(305, 20);
this.edOutputFile.TabIndex = 18;
//
// sbBrowseOutputFile
//
this.sbBrowseOutputFile.Location = new System.Drawing.Point(381, 84);
this.sbBrowseOutputFile.Name = "sbBrowseOutputFile";
this.sbBrowseOutputFile.Size = new System.Drawing.Size(75, 25);
this.sbBrowseOutputFile.TabIndex = 19;
this.sbBrowseOutputFile.Text = "Browse ...";
this.sbBrowseOutputFile.Click += new System.EventHandler(this.sbBrowseOutputFile_Click);
//
// lDataFile
//
this.lDataFile.AutoSize = true;
this.lDataFile.Location = new System.Drawing.Point(6, 90);
this.lDataFile.Name = "lDataFile";
this.lDataFile.Size = new System.Drawing.Size(58, 13);
this.lDataFile.TabIndex = 17;
this.lDataFile.Text = "Output file:";
//
// edInputFile
//
this.edInputFile.Location = new System.Drawing.Point(70, 46);
this.edInputFile.Name = "edInputFile";
this.edInputFile.Size = new System.Drawing.Size(305, 20);
this.edInputFile.TabIndex = 14;
//
// btnDecrypt
//
this.btnDecrypt.Location = new System.Drawing.Point(381, 144);
this.btnDecrypt.Name = "btnDecrypt";
this.btnDecrypt.Size = new System.Drawing.Size(75, 25);
this.btnDecrypt.TabIndex = 16;
this.btnDecrypt.Text = "Decrypt";
this.btnDecrypt.Click += new System.EventHandler(this.btnDecrypt_Click);
//
// sbBrowseInputFile
//
this.sbBrowseInputFile.Location = new System.Drawing.Point(381, 43);
this.sbBrowseInputFile.Name = "sbBrowseInputFile";
this.sbBrowseInputFile.Size = new System.Drawing.Size(75, 25);
this.sbBrowseInputFile.TabIndex = 15;
this.sbBrowseInputFile.Text = "Browse ...";
this.sbBrowseInputFile.Click += new System.EventHandler(this.sbBrowseInputFile_Click);
//
// lbInputFile
//
this.lbInputFile.AutoSize = true;
this.lbInputFile.Location = new System.Drawing.Point(5, 49);
this.lbInputFile.Name = "lbInputFile";
this.lbInputFile.Size = new System.Drawing.Size(50, 13);
this.lbInputFile.TabIndex = 13;
this.lbInputFile.Text = "Input file:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.ForeColor = System.Drawing.SystemColors.Highlight;
this.label1.Location = new System.Drawing.Point(5, 5);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(439, 26);
this.label1.TabIndex = 21;
this.label1.Text = "This sample illustrates the use of XMLDecryptor component for decrypting XML docu" +
"ments. \r\nPlease pick the XML document and then click \'Decrypt\'. ";
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 181);
this.Controls.Add(this.label1);
this.Controls.Add(this.edOutputFile);
this.Controls.Add(this.sbBrowseOutputFile);
this.Controls.Add(this.lDataFile);
this.Controls.Add(this.edInputFile);
this.Controls.Add(this.btnDecrypt);
this.Controls.Add(this.sbBrowseInputFile);
this.Controls.Add(this.lbInputFile);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "XML Decryptor Demo";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void sbBrowseInputFile_Click(object sender, EventArgs e)
{
dlgOpen.InitialDirectory = Application.StartupPath;
dlgOpen.FileName = edInputFile.Text;
if (dlgOpen.ShowDialog() == DialogResult.OK)
edInputFile.Text = dlgOpen.FileName;
}
private void sbBrowseOutputFile_Click(object sender, EventArgs e)
{
dlgSave.InitialDirectory = Application.StartupPath;
dlgSave.FileName = edOutputFile.Text;
if (dlgSave.ShowDialog() == DialogResult.OK)
edOutputFile.Text = dlgSave.FileName;
}
private void btnDecrypt_Click(object sender, EventArgs e)
{
try
{
decryptor.InputFile = edInputFile.Text;
decryptor.OutputFile = edOutputFile.Text;
decryptor.Decrypt();
MessageBox.Show("XML file successfully decrypted");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}