using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Test
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmMain : System.Windows.Forms.Form
	{
		private System.Windows.Forms.FontDialog fontDialog1;
		private System.Windows.Forms.ColorDialog colorDialog1;
		private System.Windows.Forms.PrintDialog printDialog1;
		private System.Windows.Forms.GroupBox gbSections;
		private System.Windows.Forms.CheckBox cbSection13;
		private System.Windows.Forms.CheckBox cbSection12;
		private System.Windows.Forms.CheckBox cbSection11;
		private System.Windows.Forms.CheckBox cbSection10;
		private System.Windows.Forms.CheckBox cbSection9;
		private System.Windows.Forms.CheckBox cbSection8;
		private System.Windows.Forms.CheckBox cbSection7;
		private System.Windows.Forms.CheckBox cbSection6;
		private System.Windows.Forms.CheckBox cbSection5;
		private System.Windows.Forms.CheckBox cbSection4;
		private System.Windows.Forms.CheckBox cbSection3;
		private System.Windows.Forms.CheckBox cbSection2;
		private System.Windows.Forms.CheckBox cbSection1;
		private System.Windows.Forms.Label lbUserInfo;
		private System.Windows.Forms.TextBox tbUserInfo;
		private System.Windows.Forms.CheckBox cbHardware;
		private System.Windows.Forms.TextBox tbHardware;
		private System.Windows.Forms.CheckBox cbExpiration;
		private System.Windows.Forms.CheckBox cbSection16;
		private System.Windows.Forms.CheckBox cbSection15;
		private System.Windows.Forms.CheckBox cbSection14;
		private System.Windows.Forms.Button btnGenerate;
		private System.Windows.Forms.Button btnGenerateFP;
		private System.Windows.Forms.Button btnClose;
		private System.Windows.Forms.TextBox tbKey;
		private System.Windows.Forms.DateTimePicker dtpExpiration;
        private GroupBox groupBox1;
        private ComboBox cbKeyFormat;
        private RadioButton rbShortKey;
        private RadioButton rbLongKey;
        private Button btnVerify;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmMain()
		{
			//
			// 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.fontDialog1 = new System.Windows.Forms.FontDialog();
            this.colorDialog1 = new System.Windows.Forms.ColorDialog();
            this.printDialog1 = new System.Windows.Forms.PrintDialog();
            this.gbSections = new System.Windows.Forms.GroupBox();
            this.cbSection16 = new System.Windows.Forms.CheckBox();
            this.cbSection15 = new System.Windows.Forms.CheckBox();
            this.cbSection14 = new System.Windows.Forms.CheckBox();
            this.cbSection13 = new System.Windows.Forms.CheckBox();
            this.cbSection12 = new System.Windows.Forms.CheckBox();
            this.cbSection11 = new System.Windows.Forms.CheckBox();
            this.cbSection10 = new System.Windows.Forms.CheckBox();
            this.cbSection9 = new System.Windows.Forms.CheckBox();
            this.cbSection8 = new System.Windows.Forms.CheckBox();
            this.cbSection7 = new System.Windows.Forms.CheckBox();
            this.cbSection6 = new System.Windows.Forms.CheckBox();
            this.cbSection5 = new System.Windows.Forms.CheckBox();
            this.cbSection4 = new System.Windows.Forms.CheckBox();
            this.cbSection3 = new System.Windows.Forms.CheckBox();
            this.cbSection2 = new System.Windows.Forms.CheckBox();
            this.cbSection1 = new System.Windows.Forms.CheckBox();
            this.lbUserInfo = new System.Windows.Forms.Label();
            this.tbUserInfo = new System.Windows.Forms.TextBox();
            this.cbHardware = new System.Windows.Forms.CheckBox();
            this.tbHardware = new System.Windows.Forms.TextBox();
            this.cbExpiration = new System.Windows.Forms.CheckBox();
            this.btnGenerate = new System.Windows.Forms.Button();
            this.btnGenerateFP = new System.Windows.Forms.Button();
            this.btnClose = new System.Windows.Forms.Button();
            this.tbKey = new System.Windows.Forms.TextBox();
            this.dtpExpiration = new System.Windows.Forms.DateTimePicker();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.cbKeyFormat = new System.Windows.Forms.ComboBox();
            this.rbShortKey = new System.Windows.Forms.RadioButton();
            this.rbLongKey = new System.Windows.Forms.RadioButton();
            this.btnVerify = new System.Windows.Forms.Button();
            this.gbSections.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // gbSections
            // 
            this.gbSections.Controls.Add(this.cbSection16);
            this.gbSections.Controls.Add(this.cbSection15);
            this.gbSections.Controls.Add(this.cbSection14);
            this.gbSections.Controls.Add(this.cbSection13);
            this.gbSections.Controls.Add(this.cbSection12);
            this.gbSections.Controls.Add(this.cbSection11);
            this.gbSections.Controls.Add(this.cbSection10);
            this.gbSections.Controls.Add(this.cbSection9);
            this.gbSections.Controls.Add(this.cbSection8);
            this.gbSections.Controls.Add(this.cbSection7);
            this.gbSections.Controls.Add(this.cbSection6);
            this.gbSections.Controls.Add(this.cbSection5);
            this.gbSections.Controls.Add(this.cbSection4);
            this.gbSections.Controls.Add(this.cbSection3);
            this.gbSections.Controls.Add(this.cbSection2);
            this.gbSections.Controls.Add(this.cbSection1);
            this.gbSections.Location = new System.Drawing.Point(8, 120);
            this.gbSections.Name = "gbSections";
            this.gbSections.Size = new System.Drawing.Size(328, 128);
            this.gbSections.TabIndex = 0;
            this.gbSections.TabStop = false;
            this.gbSections.Text = "Crypt Sections";
            // 
            // cbSection16
            // 
            this.cbSection16.Location = new System.Drawing.Point(248, 96);
            this.cbSection16.Name = "cbSection16";
            this.cbSection16.Size = new System.Drawing.Size(78, 24);
            this.cbSection16.TabIndex = 15;
            this.cbSection16.Text = "Section 16";
            // 
            // cbSection15
            // 
            this.cbSection15.Location = new System.Drawing.Point(248, 72);
            this.cbSection15.Name = "cbSection15";
            this.cbSection15.Size = new System.Drawing.Size(78, 24);
            this.cbSection15.TabIndex = 14;
            this.cbSection15.Text = "Section 15";
            // 
            // cbSection14
            // 
            this.cbSection14.Location = new System.Drawing.Point(248, 48);
            this.cbSection14.Name = "cbSection14";
            this.cbSection14.Size = new System.Drawing.Size(78, 24);
            this.cbSection14.TabIndex = 13;
            this.cbSection14.Text = "Section 14";
            // 
            // cbSection13
            // 
            this.cbSection13.Location = new System.Drawing.Point(248, 24);
            this.cbSection13.Name = "cbSection13";
            this.cbSection13.Size = new System.Drawing.Size(78, 24);
            this.cbSection13.TabIndex = 12;
            this.cbSection13.Text = "Section 13";
            // 
            // cbSection12
            // 
            this.cbSection12.Location = new System.Drawing.Point(168, 96);
            this.cbSection12.Name = "cbSection12";
            this.cbSection12.Size = new System.Drawing.Size(80, 24);
            this.cbSection12.TabIndex = 11;
            this.cbSection12.Text = "Section 12";
            // 
            // cbSection11
            // 
            this.cbSection11.Location = new System.Drawing.Point(168, 72);
            this.cbSection11.Name = "cbSection11";
            this.cbSection11.Size = new System.Drawing.Size(80, 24);
            this.cbSection11.TabIndex = 10;
            this.cbSection11.Text = "Section 11";
            // 
            // cbSection10
            // 
            this.cbSection10.Location = new System.Drawing.Point(168, 48);
            this.cbSection10.Name = "cbSection10";
            this.cbSection10.Size = new System.Drawing.Size(80, 24);
            this.cbSection10.TabIndex = 9;
            this.cbSection10.Text = "Section 10";
            // 
            // cbSection9
            // 
            this.cbSection9.Location = new System.Drawing.Point(168, 24);
            this.cbSection9.Name = "cbSection9";
            this.cbSection9.Size = new System.Drawing.Size(80, 24);
            this.cbSection9.TabIndex = 8;
            this.cbSection9.Text = "Section 9";
            // 
            // cbSection8
            // 
            this.cbSection8.Location = new System.Drawing.Point(88, 96);
            this.cbSection8.Name = "cbSection8";
            this.cbSection8.Size = new System.Drawing.Size(80, 24);
            this.cbSection8.TabIndex = 7;
            this.cbSection8.Text = "Section 8";
            // 
            // cbSection7
            // 
            this.cbSection7.Location = new System.Drawing.Point(88, 72);
            this.cbSection7.Name = "cbSection7";
            this.cbSection7.Size = new System.Drawing.Size(80, 24);
            this.cbSection7.TabIndex = 6;
            this.cbSection7.Text = "Section 7";
            // 
            // cbSection6
            // 
            this.cbSection6.Location = new System.Drawing.Point(88, 48);
            this.cbSection6.Name = "cbSection6";
            this.cbSection6.Size = new System.Drawing.Size(80, 24);
            this.cbSection6.TabIndex = 5;
            this.cbSection6.Text = "Section 6";
            // 
            // cbSection5
            // 
            this.cbSection5.Location = new System.Drawing.Point(88, 24);
            this.cbSection5.Name = "cbSection5";
            this.cbSection5.Size = new System.Drawing.Size(80, 24);
            this.cbSection5.TabIndex = 4;
            this.cbSection5.Text = "Section 5";
            // 
            // cbSection4
            // 
            this.cbSection4.Location = new System.Drawing.Point(8, 96);
            this.cbSection4.Name = "cbSection4";
            this.cbSection4.Size = new System.Drawing.Size(80, 24);
            this.cbSection4.TabIndex = 3;
            this.cbSection4.Text = "Section 4";
            // 
            // cbSection3
            // 
            this.cbSection3.Location = new System.Drawing.Point(8, 72);
            this.cbSection3.Name = "cbSection3";
            this.cbSection3.Size = new System.Drawing.Size(80, 24);
            this.cbSection3.TabIndex = 2;
            this.cbSection3.Text = "Section 3";
            // 
            // cbSection2
            // 
            this.cbSection2.Location = new System.Drawing.Point(8, 48);
            this.cbSection2.Name = "cbSection2";
            this.cbSection2.Size = new System.Drawing.Size(80, 24);
            this.cbSection2.TabIndex = 1;
            this.cbSection2.Text = "Section 2";
            // 
            // cbSection1
            // 
            this.cbSection1.Location = new System.Drawing.Point(8, 24);
            this.cbSection1.Name = "cbSection1";
            this.cbSection1.Size = new System.Drawing.Size(80, 24);
            this.cbSection1.TabIndex = 0;
            this.cbSection1.Text = "Section 1";
            // 
            // lbUserInfo
            // 
            this.lbUserInfo.Location = new System.Drawing.Point(8, 8);
            this.lbUserInfo.Name = "lbUserInfo";
            this.lbUserInfo.Size = new System.Drawing.Size(100, 16);
            this.lbUserInfo.TabIndex = 1;
            this.lbUserInfo.Text = "User information";
            // 
            // tbUserInfo
            // 
            this.tbUserInfo.Location = new System.Drawing.Point(8, 24);
            this.tbUserInfo.Name = "tbUserInfo";
            this.tbUserInfo.Size = new System.Drawing.Size(328, 20);
            this.tbUserInfo.TabIndex = 2;
            // 
            // cbHardware
            // 
            this.cbHardware.Location = new System.Drawing.Point(8, 56);
            this.cbHardware.Name = "cbHardware";
            this.cbHardware.Size = new System.Drawing.Size(104, 24);
            this.cbHardware.TabIndex = 3;
            this.cbHardware.Text = "Hardware ID";
            this.cbHardware.CheckedChanged += new System.EventHandler(this.cbHardware_CheckedChanged);
            // 
            // tbHardware
            // 
            this.tbHardware.Enabled = false;
            this.tbHardware.Location = new System.Drawing.Point(136, 56);
            this.tbHardware.Name = "tbHardware";
            this.tbHardware.Size = new System.Drawing.Size(200, 20);
            this.tbHardware.TabIndex = 4;
            // 
            // cbExpiration
            // 
            this.cbExpiration.Location = new System.Drawing.Point(8, 88);
            this.cbExpiration.Name = "cbExpiration";
            this.cbExpiration.Size = new System.Drawing.Size(128, 24);
            this.cbExpiration.TabIndex = 5;
            this.cbExpiration.Text = "Key Expiration Date";
            this.cbExpiration.CheckedChanged += new System.EventHandler(this.cbExpiration_CheckedChanged);
            // 
            // btnGenerate
            // 
            this.btnGenerate.Location = new System.Drawing.Point(6, 336);
            this.btnGenerate.Name = "btnGenerate";
            this.btnGenerate.Size = new System.Drawing.Size(160, 23);
            this.btnGenerate.TabIndex = 7;
            this.btnGenerate.Text = "Generate";
            this.btnGenerate.Click += new System.EventHandler(this.btnGenerate_Click);
            // 
            // btnGenerateFP
            // 
            this.btnGenerateFP.Location = new System.Drawing.Point(174, 336);
            this.btnGenerateFP.Name = "btnGenerateFP";
            this.btnGenerateFP.Size = new System.Drawing.Size(160, 23);
            this.btnGenerateFP.TabIndex = 8;
            this.btnGenerateFP.Text = "Generate from project";
            this.btnGenerateFP.Click += new System.EventHandler(this.btnGenerateFP_Click);
            // 
            // btnClose
            // 
            this.btnClose.Location = new System.Drawing.Point(6, 368);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(328, 23);
            this.btnClose.TabIndex = 9;
            this.btnClose.Text = "Close";
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click_1);
            // 
            // tbKey
            // 
            this.tbKey.Location = new System.Drawing.Point(8, 397);
            this.tbKey.Multiline = true;
            this.tbKey.Name = "tbKey";
            this.tbKey.Size = new System.Drawing.Size(328, 88);
            this.tbKey.TabIndex = 10;
            // 
            // dtpExpiration
            // 
            this.dtpExpiration.Enabled = false;
            this.dtpExpiration.Location = new System.Drawing.Point(136, 88);
            this.dtpExpiration.Name = "dtpExpiration";
            this.dtpExpiration.Size = new System.Drawing.Size(200, 20);
            this.dtpExpiration.TabIndex = 11;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.cbKeyFormat);
            this.groupBox1.Controls.Add(this.rbShortKey);
            this.groupBox1.Controls.Add(this.rbLongKey);
            this.groupBox1.Location = new System.Drawing.Point(9, 254);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(324, 76);
            this.groupBox1.TabIndex = 12;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Key type";
            // 
            // cbKeyFormat
            // 
            this.cbKeyFormat.FormattingEnabled = true;
            this.cbKeyFormat.Items.AddRange(new object[] {
            "10 digits",
            "16 digits",
            "32 digits",
            "64 digits"});
            this.cbKeyFormat.Location = new System.Drawing.Point(7, 44);
            this.cbKeyFormat.Name = "cbKeyFormat";
            this.cbKeyFormat.Size = new System.Drawing.Size(311, 21);
            this.cbKeyFormat.TabIndex = 2;
            // 
            // rbShortKey
            // 
            this.rbShortKey.AutoSize = true;
            this.rbShortKey.Location = new System.Drawing.Point(167, 20);
            this.rbShortKey.Name = "rbShortKey";
            this.rbShortKey.Size = new System.Drawing.Size(71, 17);
            this.rbShortKey.TabIndex = 1;
            this.rbShortKey.Text = "Short Key";
            this.rbShortKey.UseVisualStyleBackColor = true;
            // 
            // rbLongKey
            // 
            this.rbLongKey.AutoSize = true;
            this.rbLongKey.Checked = true;
            this.rbLongKey.Location = new System.Drawing.Point(7, 20);
            this.rbLongKey.Name = "rbLongKey";
            this.rbLongKey.Size = new System.Drawing.Size(70, 17);
            this.rbLongKey.TabIndex = 0;
            this.rbLongKey.TabStop = true;
            this.rbLongKey.Text = "Long Key";
            this.rbLongKey.UseVisualStyleBackColor = true;
            // 
            // btnVerify
            // 
            this.btnVerify.Location = new System.Drawing.Point(8, 491);
            this.btnVerify.Name = "btnVerify";
            this.btnVerify.Size = new System.Drawing.Size(328, 22);
            this.btnVerify.TabIndex = 13;
            this.btnVerify.Text = "Verify";
            this.btnVerify.UseVisualStyleBackColor = true;
            this.btnVerify.Click += new System.EventHandler(this.btnVerify_Click);
            // 
            // frmMain
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(344, 522);
            this.Controls.Add(this.btnVerify);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.dtpExpiration);
            this.Controls.Add(this.tbKey);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.btnGenerateFP);
            this.Controls.Add(this.btnGenerate);
            this.Controls.Add(this.cbExpiration);
            this.Controls.Add(this.tbHardware);
            this.Controls.Add(this.cbHardware);
            this.Controls.Add(this.tbUserInfo);
            this.Controls.Add(this.lbUserInfo);
            this.Controls.Add(this.gbSections);
            this.Name = "frmMain";
            this.Text = "KeyGen .NET Application";
            this.Load += new System.EventHandler(this.frmMain_Load);
            this.gbSections.ResumeLayout(false);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            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 btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close ();
		}

		private void btnGenerate_Click(object sender, System.EventArgs e)
		{
            uint dwResult = 0;
            //string keybuffer;
			Enigma_KeyGen_IDE.GenKeyParams KeyParams = new Enigma_KeyGen_IDE.GenKeyParams ();

			// Clear control
			tbKey.Clear();
			// fill out key buffer	
            byte[] keybuffer = new byte[256];

            if (rbLongKey.Checked)
            {
                KeyParams.KeyType = Enigma_KeyGen_IDE.KEY_TYPE_LONG;
            }
            if (rbShortKey.Checked)
            {
                KeyParams.KeyType = Enigma_KeyGen_IDE.KEY_TYPE_SHORT;
            }
            switch (cbKeyFormat.SelectedIndex)
            {
                case 0:
                    {
                        KeyParams.KeyFormat = Enigma_KeyGen_IDE.KEY_FORMAT_10;
                        break;
                    }
                case 1:
                    {
                        KeyParams.KeyFormat = Enigma_KeyGen_IDE.KEY_FORMAT_16;
                        break;
                    }
                case 2:
                    {
                        KeyParams.KeyFormat = Enigma_KeyGen_IDE.KEY_FORMAT_32;
                        break;
                    }
                case 3:
                    {
                        KeyParams.KeyFormat = Enigma_KeyGen_IDE.KEY_FORMAT_64;
                        break;
                    }
                default:
                    {
                        KeyParams.KeyFormat = Enigma_KeyGen_IDE.KEY_FORMAT_32;
                        break;
                    }
            }

            KeyParams.Key = keybuffer.ToString();
			KeyParams.KeyLen = 256;
			// fill out user info
			KeyParams.RegInfo = tbUserInfo.Text ;			
			KeyParams.RegInfoLen = KeyParams.RegInfo.Length;
			// Expiration information
			if (cbExpiration.Checked)
			{
				KeyParams.UseKeyExpiration = 1;
				KeyParams.ExpirationYear  = (short)dtpExpiration.Value.Year;
				KeyParams.ExpirationMonth = (byte)dtpExpiration.Value.Month;
				KeyParams.ExpirationDay   = (byte)dtpExpiration.Value.Day;
			}
			else
			{
				KeyParams.UseKeyExpiration = 0;
			}
			// Hardware ID
			if (cbHardware.Checked )
			{
				KeyParams.UseHardwareLocking = 1;
				KeyParams.HardwareID = tbHardware.Text;
			} 
			else
			{
				KeyParams.UseHardwareLocking = 0;
			}
			// Crypted sections]
            KeyParams.Section1 = (cbSection1.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section2 = (cbSection2.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section3 = (cbSection3.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section4 = (cbSection4.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section5 = (cbSection5.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section6 = (cbSection6.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section7 = (cbSection7.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section8 = (cbSection8.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section9 = (cbSection9.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section10 = (cbSection10.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section11 = (cbSection11.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section12 = (cbSection12.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section13 = (cbSection13.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section14 = (cbSection14.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section15 = (cbSection15.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section16 = (cbSection16.Checked == true) ? (byte)1 : (byte)0;

			// Secure constant (get it from project file)!
			// or use special utility
            if (KeyParams.KeyType == Enigma_KeyGen_IDE.KEY_TYPE_LONG)
            {
                KeyParams.D = "3K4RHBXV93J75R72XBF68M5GTQ2PDDVCJCQ9ZZ7CXBZYJNY6ASWVB6S3X4JP9TWPBANMW2HSMPAZ7GY5WZF8TQYJ36VUQZQ7J4F6JF";
                KeyParams.N = "7MSYMACUVPSKBB44WSEF3Q7T6ATNEDQ9D3UGQRP8SYVCXDDEX8TB78PFHWXGR4RVVTWVCSXANQWHDLWF2XTMS7PAA3396RALRP3UUK";
            }
            if (KeyParams.KeyType == Enigma_KeyGen_IDE.KEY_TYPE_SHORT)
            {
                KeyParams.D = "5V8YRKNAXCEPB46WAV4RPT95D6WMPHPKFJBXHDVDM";
                KeyParams.N = "CNC74RPSW3DKWRSGRLDZS3D9G6XRYJ98QWHYX86AT";
            }
            KeyParams.A = "F4A190D21E05F4DF387A18CFBB70426E";
            KeyParams.B = "C7A44A94C94D7F290037E9DACCC2CB23";

            dwResult = Enigma_KeyGen_IDE.KG_GenerateRegistrationKey(ref KeyParams);

            if (dwResult == Enigma_KeyGen_IDE.ERROR_OK)
            {
                tbKey.Text = KeyParams.Key;
            }
            else
            {
                switch (dwResult)
                {
                    case Enigma_KeyGen_IDE.ERROR_KEY_BUFFER_NOT_ALLOCATED      : 
                        tbKey.Text = "ERROR_KEY_BUFFER_NOT_ALLOCATED"     ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_KEY_BUFFER_LESS               : 
                        tbKey.Text = "ERROR_KEY_BUFFER_LESS"              ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_REGINFO_BUFFER_NOT_ALLOCATED  : 
                        tbKey.Text = "ERROR_REGINFO_BUFFER_NOT_ALLOCATED" ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_REGINFO_BUFFER_EMPTY          : 
                        tbKey.Text = "ERROR_REGINFO_BUFFER_EMPTY"         ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_HARDWARE_TYPE_UNKNOWN         : 
                        tbKey.Text = "ERROR_HARDWARE_TYPE_UNKNOWN"        ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_HARDWARE_ID                   : 
                        tbKey.Text = "ERROR_HARDWARE_ID"                  ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_HARDWARE_BUFFER_NOT_ALLOCATED : 
                        tbKey.Text = "ERROR_HARDWARE_BUFFER_NOT_ALLOCATED";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_HARDWARE_BUFFER_EMPTY         : 
                        tbKey.Text = "ERROR_HARDWARE_BUFFER_EMPTY"        ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_D            : 
                        tbKey.Text = "ERROR_INVALID_CONSTANT_D"           ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_N            : 
                        tbKey.Text = "ERROR_INVALID_CONSTANT_N"           ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_A            : 
                        tbKey.Text = "ERROR_INVALID_CONSTANT_A"           ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_B            : 
                        tbKey.Text = "ERROR_INVALID_CONSTANT_B"           ;
                        break;
                    case Enigma_KeyGen_IDE.ERROR_PROJECT_FILE_NOT_FOUND        : 
                        tbKey.Text = "ERROR_PROJECT_FILE_NOT_FOUND";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_PROJECT_FILE_INVALID          : 
                        tbKey.Text = "ERROR_PROJECT_FILE_INVALID"         ;
                        break;
                    default: 
                        tbKey .Text = "ERROR_UNKNOWN";
                        break;
                }
            }			
		}

		private void cbHardware_CheckedChanged(object sender, System.EventArgs e)
		{
			tbHardware.Enabled = cbHardware.Checked;
		}

		private void cbExpiration_CheckedChanged(object sender, System.EventArgs e)
		{
			dtpExpiration.Enabled = cbExpiration.Checked;
		}

        private void btnClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnGenerateFP_Click(object sender, EventArgs e)
        {
            uint dwResult = 0;
            //string keybuffer;
            Enigma_KeyGen_IDE.GenKeyParams KeyParams = new Enigma_KeyGen_IDE.GenKeyParams();

            // Clear control
            tbKey.Clear();
            // fill out key buffer	
            byte[] keybuffer = new byte[256];

            KeyParams.Key = keybuffer.ToString();
            KeyParams.KeyLen = 256;
            // fill out user info
            KeyParams.RegInfo = tbUserInfo.Text;
            KeyParams.RegInfoLen = KeyParams.RegInfo.Length;
            // Expiration information
            if (cbExpiration.Checked)
            {
                KeyParams.UseKeyExpiration = 1;
                KeyParams.ExpirationYear = (short)dtpExpiration.Value.Year;
                KeyParams.ExpirationMonth = (byte)dtpExpiration.Value.Month;
                KeyParams.ExpirationDay = (byte)dtpExpiration.Value.Day;
            }
            else
            {
                KeyParams.UseKeyExpiration = 0;
            }
            // Hardware ID
            if (cbHardware.Checked)
            {
                KeyParams.UseHardwareLocking = 1;
                KeyParams.HardwareID = tbHardware.Text;
            }
            else
            {
                KeyParams.UseHardwareLocking = 0;
            }
            // Crypted sections]
            KeyParams.Section1 = (cbSection1.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section2 = (cbSection2.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section3 = (cbSection3.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section4 = (cbSection4.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section5 = (cbSection5.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section6 = (cbSection6.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section7 = (cbSection7.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section8 = (cbSection8.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section9 = (cbSection9.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section10 = (cbSection10.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section11 = (cbSection11.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section12 = (cbSection12.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section13 = (cbSection13.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section14 = (cbSection14.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section15 = (cbSection15.Checked == true) ? (byte)1 : (byte)0;
            KeyParams.Section16 = (cbSection16.Checked == true) ? (byte)1 : (byte)0;


            dwResult = Enigma_KeyGen_IDE.KG_GenerateRegistrationKeyFromProject("Default.enigma", ref KeyParams);

            if (dwResult == Enigma_KeyGen_IDE.ERROR_OK)
            {
                tbKey.Text = KeyParams.Key;
            }
            else
            {
                switch (dwResult)
                {
                    case Enigma_KeyGen_IDE.ERROR_KEY_BUFFER_NOT_ALLOCATED:
                        tbKey.Text = "ERROR_KEY_BUFFER_NOT_ALLOCATED";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_KEY_BUFFER_LESS:
                        tbKey.Text = "ERROR_KEY_BUFFER_LESS";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_REGINFO_BUFFER_NOT_ALLOCATED:
                        tbKey.Text = "ERROR_REGINFO_BUFFER_NOT_ALLOCATED";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_REGINFO_BUFFER_EMPTY:
                        tbKey.Text = "ERROR_REGINFO_BUFFER_EMPTY";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_HARDWARE_TYPE_UNKNOWN:
                        tbKey.Text = "ERROR_HARDWARE_TYPE_UNKNOWN";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_HARDWARE_ID:
                        tbKey.Text = "ERROR_HARDWARE_ID";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_HARDWARE_BUFFER_NOT_ALLOCATED:
                        tbKey.Text = "ERROR_HARDWARE_BUFFER_NOT_ALLOCATED";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_HARDWARE_BUFFER_EMPTY:
                        tbKey.Text = "ERROR_HARDWARE_BUFFER_EMPTY";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_D:
                        tbKey.Text = "ERROR_INVALID_CONSTANT_D";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_N:
                        tbKey.Text = "ERROR_INVALID_CONSTANT_N";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_A:
                        tbKey.Text = "ERROR_INVALID_CONSTANT_A";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_B:
                        tbKey.Text = "ERROR_INVALID_CONSTANT_B";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_PROJECT_FILE_NOT_FOUND:
                        tbKey.Text = "ERROR_PROJECT_FILE_NOT_FOUND";
                        break;
                    case Enigma_KeyGen_IDE.ERROR_PROJECT_FILE_INVALID:
                        tbKey.Text = "ERROR_PROJECT_FILE_INVALID";
                        break;
                    default:
                        tbKey.Text = "ERROR_UNKNOWN";
                        break;
                }
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            cbKeyFormat.SelectedIndex = 2;
        }

        private void btnVerify_Click(object sender, EventArgs e)
        {
            uint dwResult = 0;
            Enigma_KeyGen_IDE.TVerifyKey VerifyKey = new Enigma_KeyGen_IDE.TVerifyKey();

            if (tbUserInfo.Text == "")
            {
                MessageBox.Show("User info field is empty!");
            }
            if (tbKey.Text == "")
            {
                MessageBox.Show("Key field is empty!");
            }
            VerifyKey.Key = tbKey.Text;
            VerifyKey.RegInfo = tbUserInfo.Text;

            if (rbLongKey.Checked)
            {
                VerifyKey.KeyType = Enigma_KeyGen_IDE.KEY_TYPE_LONG;
            }
            if (rbShortKey.Checked)
            {
                VerifyKey.KeyType = Enigma_KeyGen_IDE.KEY_TYPE_SHORT;
            }
            switch (cbKeyFormat.SelectedIndex)
            {
                case 0:
                    {
                        VerifyKey.KeyFormat = Enigma_KeyGen_IDE.KEY_FORMAT_10;
                        break;
                    }
                case 1:
                    {
                        VerifyKey.KeyFormat = Enigma_KeyGen_IDE.KEY_FORMAT_16;
                        break;
                    }
                case 2:
                    {
                        VerifyKey.KeyFormat = Enigma_KeyGen_IDE.KEY_FORMAT_32;
                        break;
                    }
                case 3:
                    {
                        VerifyKey.KeyFormat = Enigma_KeyGen_IDE.KEY_FORMAT_64;
                        break;
                    }
                default:
                    {
                        VerifyKey.KeyFormat = Enigma_KeyGen_IDE.KEY_FORMAT_32;
                        break;
                    }
            }
            // Secure constant (get it from project file)!
            // or use special utility
            if (VerifyKey.KeyType == Enigma_KeyGen_IDE.KEY_TYPE_LONG)
            {
                VerifyKey.E = "35AL5XHXFAJZUKEUZVFHSQ2Q3BNECXCAFTEPXQ9S8KWNF89XPYNZCXCAUEEXYVGQJAN5MTZ58BUX6MG5FFP2NUX7QXCF2XK5T344GHF";
                VerifyKey.N = "7MSYMACUVPSKBB44WSEF3Q7T6ATNEDQ9D3UGQRP8SYVCXDDEX8TB78PFHWXGR4RVVTWVCSXANQWHDLWF2XTMS7PAA3396RALRP3UUK";
            }
            if (VerifyKey.KeyType == Enigma_KeyGen_IDE.KEY_TYPE_SHORT)
            {
                VerifyKey.E = "3WMT5MUA2J3VG4BE3P3B3T9C74YGC7JEZYF2N323SD";
                VerifyKey.N = "CNC74RPSW3DKWRSGRLDZS3D9G6XRYJ98QWHYX86AT";
            }
            VerifyKey.A = "F4A190D21E05F4DF387A18CFBB70426E";
            VerifyKey.B = "C7A44A94C94D7F290037E9DACCC2CB23";

            dwResult = Enigma_KeyGen_IDE.KG_VerifyRegistrationInfo(ref VerifyKey);

            switch (dwResult)
            {
                case Enigma_KeyGen_IDE.ERROR_OK:
                    MessageBox.Show(string.Format("Valid key! Created: {0}/{1}/{2}", VerifyKey.CreateDay, VerifyKey.CreateMonth, VerifyKey.CreateYear));
                    break;
                case Enigma_KeyGen_IDE.ERROR_KEY_BUFFER_NOT_ALLOCATED:
                    MessageBox.Show("ERROR_KEY_BUFFER_NOT_ALLOCATED");
                    break;
                case Enigma_KeyGen_IDE.ERROR_KEY_BUFFER_LESS:
                    MessageBox.Show("ERROR_KEY_BUFFER_LESS");
                    break;
                case Enigma_KeyGen_IDE.ERROR_REGINFO_BUFFER_NOT_ALLOCATED:
                    MessageBox.Show("ERROR_REGINFO_BUFFER_NOT_ALLOCATED");
                    break;
                case Enigma_KeyGen_IDE.ERROR_REGINFO_BUFFER_EMPTY:
                    MessageBox.Show("ERROR_REGINFO_BUFFER_EMPTY");
                    break;
                case Enigma_KeyGen_IDE.ERROR_INVALID_KEYTYPE:
                    MessageBox.Show("ERROR_HARDWARE_TYPE_UNKNOWN");
                    break;
                case Enigma_KeyGen_IDE.ERROR_INVALID_REGINFO:
                    MessageBox.Show("ERROR_HARDWARE_ID");
                    break;
                case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_E:
                    MessageBox.Show("ERROR_INVALID_CONSTANT_D");
                    break;
                case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_N:
                    MessageBox.Show("ERROR_INVALID_CONSTANT_N");
                    break;
                case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_A:
                    MessageBox.Show("ERROR_INVALID_CONSTANT_A");
                    break;
                case Enigma_KeyGen_IDE.ERROR_INVALID_CONSTANT_B:
                    MessageBox.Show("ERROR_INVALID_CONSTANT_B");
                    break;
                case Enigma_KeyGen_IDE.ERROR_PROJECT_FILE_NOT_FOUND:
                    MessageBox.Show("ERROR_PROJECT_FILE_NOT_FOUND");
                    break;
                case Enigma_KeyGen_IDE.ERROR_PROJECT_FILE_INVALID:
                    MessageBox.Show("ERROR_PROJECT_FILE_INVALID");
                    break;
                default:
                    MessageBox.Show("ERROR_UNKNOWN");
                    break;
            }
        }
	}
}
