using Chilkat;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace DescargaMasiva
{
	public class FrmAddAccount : Form
	{
		private IContainer components = null;

		private TextBox txtLicensing;

		private Label label1;

		private TextBox txtMessage;

		private Button btnCancel;

		private Button btnOk;

		private Label label2;

		private Label lblId;

		public FrmAddAccount()
		{
			this.InitializeComponent();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			base.Close();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			try
			{
				string str = this.txtLicensing.Text.Trim();
				if (this.GetKeyAccounts(str).Count > 0)
				{
					Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
					configuration.AppSettings.Settings["licensing"].Value = str;
					configuration.Save(ConfigurationSaveMode.Modified);
				}
				base.DialogResult = System.Windows.Forms.DialogResult.OK;
			}
			catch (Exception exception)
			{
				MessageBox.Show(exception.Message);
			}
		}

		private static string DecrypString(string encrypted, string key)
		{
			string str;
			try
			{
				Crypt2 crypt2 = new Crypt2();
				if (!crypt2.UnlockComponent("ZOILAACrypt_etPAZumSXXpy"))
				{
					throw new Exception(crypt2.LastErrorText);
				}
				crypt2.CryptAlgorithm = "3des";
				crypt2.CipherMode = "ecb";
				crypt2.KeyLength = 128;
				crypt2.PaddingScheme = 3;
				crypt2.EncodingMode = "hex";
				crypt2.HashAlgorithm = "md5";
				crypt2.SetEncodedKey(crypt2.HashStringENC(key), "hex");
				str = crypt2.DecryptStringENC(encrypted);
			}
			catch (Exception exception)
			{
				throw new Exception(exception.Message);
			}
			return str;
		}

		protected override void Dispose(bool disposing)
		{
			bool flag;
			flag = (!disposing ? true : this.components == null);
			if (!flag)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void FrmAddAccount_Load(object sender, EventArgs e)
		{
			try
			{
				string computerId = FrmAddAccount.GetComputerId();
				Clipboard.SetText(computerId);
				this.lblId.Text = computerId;
				string item = ConfigurationManager.AppSettings["licensing"];
				this.txtLicensing.Text = item;
				this.txtMessage.Text = "";
				foreach (string keyAccount in this.GetKeyAccounts())
				{
					this.txtMessage.AppendText(string.Concat(keyAccount, (char)10));
				}
			}
			catch (Exception exception)
			{
				this.txtMessage.Text = exception.Message;
			}
		}

		private static string GetComputerId()
		{
			string str1;
			try
			{
				IEnumerable<ManagementBaseObject> managementBaseObjects = (new ManagementObjectSearcher("SELECT * FROM Win32_baseboard")).Get().Cast<ManagementBaseObject>();
				IEnumerable<object> objs = managementBaseObjects.Select<ManagementBaseObject, object>((ManagementBaseObject m) => {
					object item = m["SerialNumber"];
					return item;
				});
				str1 = objs.Select<object, string>((object s) => {
					string str = s.ToString();
					return str;
				}).FirstOrDefault<string>();
			}
			catch (Exception exception)
			{
				throw new Exception(exception.Message);
			}
			return str1;
		}

		public List<string> GetKeyAccounts()
		{
			List<string> keyAccounts;
			try
			{
				string item = ConfigurationManager.AppSettings["licensing"];
				keyAccounts = this.GetKeyAccounts(item);
			}
			catch (Exception exception)
			{
				throw new Exception(exception.Message);
			}
			return keyAccounts;
		}

		public List<string> GetKeyAccounts(string license)
		{
			List<string> strs;
			try
			{
				Regex regex = new Regex("[a-z|\\&|A-Z]{3,4}([0-9]{2})(1[0-2]|0[1-9])([0-2][0-9]|3[0-1])([a-zA-Z0-9]{3})");
				string computerId = FrmAddAccount.GetComputerId();
				IEnumerable<Match> matches = regex.Matches(FrmAddAccount.DecrypString(license, computerId)).Cast<Match>();
				List<string> list = matches.Select<Match, string>((Match m) => {
					string value = m.Value;
					return value;
				}).ToList<string>();
				strs = list;
			}
			catch (Exception exception)
			{
				throw new Exception(exception.Message);
			}
			return strs;
		}

		private void InitializeComponent()
		{
			this.txtLicensing = new TextBox();
			this.label1 = new Label();
			this.txtMessage = new TextBox();
			this.btnCancel = new Button();
			this.btnOk = new Button();
			this.label2 = new Label();
			this.lblId = new Label();
			base.SuspendLayout();
			this.txtLicensing.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
			this.txtLicensing.Location = new Point(12, 54);
			this.txtLicensing.Name = "txtLicensing";
			this.txtLicensing.Size = new System.Drawing.Size(330, 20);
			this.txtLicensing.TabIndex = 1;
			this.txtLicensing.TextChanged += new EventHandler(this.txtLicensing_TextChanged);
			this.label1.AutoSize = true;
			this.label1.Location = new Point(9, 38);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(105, 13);
			this.label1.TabIndex = 0;
			this.label1.Text = "Número de Licencia:";
			this.txtMessage.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
			this.txtMessage.Location = new Point(12, 80);
			this.txtMessage.Multiline = true;
			this.txtMessage.Name = "txtMessage";
			this.txtMessage.ReadOnly = true;
			this.txtMessage.Size = new System.Drawing.Size(330, 121);
			this.txtMessage.TabIndex = 2;
			this.btnCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new Point(267, 207);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(75, 23);
			this.btnCancel.TabIndex = 4;
			this.btnCancel.Text = "&Cancelar";
			this.btnCancel.UseVisualStyleBackColor = true;
			this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
			this.btnOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
			this.btnOk.Location = new Point(186, 207);
			this.btnOk.Name = "btnOk";
			this.btnOk.Size = new System.Drawing.Size(75, 23);
			this.btnOk.TabIndex = 3;
			this.btnOk.Text = "&Aceptar";
			this.btnOk.UseVisualStyleBackColor = true;
			this.btnOk.Click += new EventHandler(this.btnOk_Click);
			this.label2.AutoSize = true;
			this.label2.Location = new Point(9, 9);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(24, 13);
			this.label2.TabIndex = 5;
			this.label2.Text = "ID: ";
			this.lblId.AutoSize = true;
			this.lblId.Location = new Point(39, 9);
			this.lblId.Name = "lblId";
			this.lblId.Size = new System.Drawing.Size(0, 13);
			this.lblId.TabIndex = 6;
			base.AutoScaleDimensions = new SizeF(6f, 13f);
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			base.CancelButton = this.btnCancel;
			base.ClientSize = new System.Drawing.Size(354, 242);
			base.ControlBox = false;
			base.Controls.Add(this.lblId);
			base.Controls.Add(this.label2);
			base.Controls.Add(this.btnOk);
			base.Controls.Add(this.btnCancel);
			base.Controls.Add(this.txtMessage);
			base.Controls.Add(this.label1);
			base.Controls.Add(this.txtLicensing);
			base.Name = "FrmAddAccount";
			this.Text = "Licencia";
			base.Load += new EventHandler(this.FrmAddAccount_Load);
			base.ResumeLayout(false);
			base.PerformLayout();
		}

		private void txtLicensing_TextChanged(object sender, EventArgs e)
		{
			try
			{
				this.txtMessage.Text = "";
				string computerId = FrmAddAccount.GetComputerId();
				string str = FrmAddAccount.DecrypString(this.txtLicensing.Text.Trim(), computerId);
				char[] chrArray = new char[] { ',' };
				List<string> list = str.Split(chrArray).ToList<string>();
				foreach (string str1 in list)
				{
					this.txtMessage.AppendText(string.Concat(str1, "\n"));
				}
			}
			catch (Exception exception)
			{
				this.txtMessage.Text = exception.Message;
			}
		}
	}
}