public void addOption(LicenseOption option) { if (options.Count != 0) { options.Add(option.Name, option); } }
private void BtGenerate_Click(object sender, EventArgs e) { int customerid = 0; int.TryParse(Txtcustomerid.Text, out customerid); int systemid = 0; int.TryParse(TxtSysID.Text, out systemid); string comment = Txtcomment.Text.Length > 0 ? Txtcomment.Text : "TEMS Pocket 8.1"; if (Txtdeviceid.Text == string.Empty) { MessageBox.Show("empty device id not allowed"); return; } List <LicenseOption> options = new List <LicenseOption>(); foreach (string optionname in checkedListBox1.CheckedItems) { LicenseOption option = new LicenseOption((LicenseOptionName)Enum.Parse(typeof(LicenseOptionName), optionname, true)); option.StartDate = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day); option.EndDate = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day); options.Add(option); } Parameter device = new Parameter(customerid, systemid, Txtdeviceid.Text, comment); device.SelectOption(options); LicenseManager manager = new LicenseManager(device); manager.SaveLicense(); if (manager.LoadLicenseFile() == false) { MessageBox.Show("License verify fail."); } MessageBox.Show("Generate License done."); }
public override void LoadFromList(IEnumerable <ConfigSection> sections, int version) { MemoryStream stream = new MemoryStream(); byte[] signature = null; foreach (ConfigSection section in sections) { if (section.Id == 0) { foreach (ConfigItem item in section) { switch (item.Tag) { case 0: { signature = item.RawValue; //writer.BaseStream.Write(signature,0,signature.Length); continue; } case 1: { this.timeStamp = item.DateTimeValue; //writer.Write(timeStamp.ToString()); continue; } case 2: { this.CustomerId = item.IntegerValue; //writer.Write(customerId); continue; } case 3: { this.SystemId = item.IntegerValue; //writer.Write(systemId); continue; } case 4: { this.DeviceId = item.StringValue; //writer.Write(deviceId); continue; } case 5: { this.Comment = item.StringValue; //writer.Write(comment); continue; } } } } else { LicenseOption option = new LicenseOption((LicenseOptionName)section.Id); foreach (ConfigItem item2 in section) { switch (item2.Tag) { case 0: { option.StartDate = item2.DateTimeValue; //writer.Write(option.Name.ToString()+"-"+item2.DateTimeValue.ToString()); continue; } case 1: { option.EndDate = item2.DateTimeValue; //writer.Write(option.Name.ToString() + "-" + item2.DateTimeValue.ToString()); continue; } } } addOption(option); } stream.WriteByte((byte)section.Id); foreach (ConfigItem item3 in section) { if ((section.Id != 0) || (item3.Tag != 0)) { stream.WriteByte((byte)item3.Tag); stream.Write(item3.RawValue, 0, item3.RawValue.Length); } } } if (signature == null) { throw new IOException("License hash not found."); } byte[] buffer = stream.ToArray(); BinaryWriter bufferbinary = new BinaryWriter(new FileStream(@"buffer.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)); bufferbinary.Write(buffer); bufferbinary.Flush(); bufferbinary.Close(); BinaryWriter fsign = new BinaryWriter(new FileStream(@"signature.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)); fsign.Write(signature); fsign.Close(); RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.ImportCspBlob(PublicKeyBlob); if (!provider.VerifyData(buffer, "SHA1", signature)) { throw new IOException("License signature invalid."); } }
protected override IEnumerable <ConfigSection> SaveToList() { List <ConfigSection> section = new List <ConfigSection>(); ConfigSection SectionAdd = new ConfigSection(0); ConfigItem ItemInsert = new ConfigItem(0, true); section.Add(SectionAdd); SectionAdd.Add(ItemInsert); foreach (ConfigSection sec in section) { if (sec.Id == 0) { foreach (ConfigItem itemsave in sec) { switch (itemsave.Tag) { case 0: { timeStamp = DateTime.Now; continue; } case 1: { this.comment = "IMEI"; continue; } case 2: { this.customerId = 1; continue; } case 3: { //-----------Set unique id ----------- // this.deviceId = ""; continue; } case 4: { this.systemId = 2797; continue; } case 5: { //---------- RSA need to sign data --------// continue; } } } } else { LicenseOption option = new LicenseOption((LicenseOptionName)sec.Id); foreach (ConfigItem ItemName in sec) { switch (ItemName.Tag) { case 0: { option.StartDate = DateTime.Now; continue; } case 1: { option.EndDate = DateTime.MaxValue; continue; } } } addOption(option); section.Add(sec); } } return(SaveToList()); // throw new Exception("generate license not support"); }