using LevelCode.License.DataAccess; using LevelCode.License.Models; using LevelCode.License.Security; using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace LevelCode.License.Forms { public partial class FrmGerarLicencaKey : Form { private List _licencas; public FrmGerarLicencaKey() { InitializeComponent(); // 🔐 Garante que as chaves RSA existem KeyManager.GarantirChaves(); CarregarLicencas(); } private void CarregarLicencas() { using (DbConexao db = new DbConexao()) { LicencaDAL dal = new LicencaDAL(db); _licencas = dal.BuscarTodas(); dgvLicencas.AutoGenerateColumns = true; dgvLicencas.DataSource = _licencas; } } private void btnGerarKey_Click(object sender, EventArgs e) { if (dgvLicencas.CurrentRow == null) { MessageBox.Show( "Selecione uma licença.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } ModeloLicenca licenca = dgvLicencas.CurrentRow.DataBoundItem as ModeloLicenca; if (licenca == null) return; SaveFileDialog sfd = new SaveFileDialog { Filter = "Arquivo de Licença (*.key)|*.key", FileName = $"license_{licenca.IdLicenca}.key" }; if (sfd.ShowDialog() != DialogResult.OK) return; try { // 🔑 Chave privada SEMPRE via KeyManager string chavePrivada = KeyManager.ObterChavePrivada(); LicensePayload payload = new LicensePayload { // 🔴 ESSENCIAL PARA O CLIENTE Key = licenca.LicenseKey, // (opcional, mas útil) LicencaId = licenca.IdLicenca, Cliente = licenca.Cliente, ExpiraEm = licenca.ExpiraEm, LimiteMaquinas = licenca.LimiteMaquinas, // ✅ AGORA COMPATÍVEL Modulos = licenca.ModulosLista }; LicenseEngine.GerarArquivoKey( payload, chavePrivada, sfd.FileName); MessageBox.Show( "Arquivo .key gerado com sucesso!\n\n" + "Chaves RSA em:\n" + @"C:\LevelCode\Licensing\Keys\", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show( ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnGerarChaves_Click(object sender, EventArgs e) { KeyManager.GarantirChaves(); MessageBox.Show( "Chaves RSA geradas com sucesso!\n\n" + @"C:\LevelCode\Licensing\Keys\", "Chaves criadas", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void button1_Click(object sender, EventArgs e) { } } }