|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Security.Cryptography; |
| 4 | +using System.Text; |
| 5 | +using System.Windows.Forms; |
| 6 | + |
| 7 | +namespace KeyGenerator |
| 8 | +{ |
| 9 | + public partial class MainForm : Form |
| 10 | + { |
| 11 | + public MainForm() |
| 12 | + { |
| 13 | + InitializeComponent(); |
| 14 | + this.generateButton.Click += new System.EventHandler(this.generateButton_Click); |
| 15 | + this.copyPublicKeyButton.Click += new System.EventHandler(this.copyPublicKeyButton_Click); |
| 16 | + |
| 17 | + try |
| 18 | + { |
| 19 | + // Display the public key for the admin to copy into the main application |
| 20 | + this.publicKeyTextBox.Text = LicenseGenerator.GetPublicKey(); |
| 21 | + } |
| 22 | + catch (Exception ex) |
| 23 | + { |
| 24 | + MessageBox.Show($"A critical error occurred during key generation: {ex.Message}\n\nThe application will now close.", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 25 | + // Close the form immediately if key generation fails. |
| 26 | + // Using Load event to close because constructor is too early. |
| 27 | + this.Load += (s, e) => this.Close(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private void generateButton_Click(object? sender, EventArgs e) |
| 32 | + { |
| 33 | + string encryptedMachineId = this.machineIdTextBox.Text.Trim(); |
| 34 | + if (string.IsNullOrWhiteSpace(encryptedMachineId)) |
| 35 | + { |
| 36 | + MessageBox.Show("Please enter the user's Encrypted Machine ID.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + try |
| 41 | + { |
| 42 | + this.licenseKeyTextBox.Text = LicenseGenerator.GenerateLicenseKey(encryptedMachineId); |
| 43 | + } |
| 44 | + catch (Exception ex) |
| 45 | + { |
| 46 | + MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private void copyPublicKeyButton_Click(object? sender, EventArgs e) |
| 51 | + { |
| 52 | + Clipboard.SetText(this.publicKeyTextBox.Text); |
| 53 | + MessageBox.Show("Public key copied to clipboard.", "Copied", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static class LicenseGenerator |
| 58 | + { |
| 59 | + private const string PrivateKeyFileName = "private_key.b64"; |
| 60 | + private static readonly RSA _privateKey; |
| 61 | + |
| 62 | + static LicenseGenerator() |
| 63 | + { |
| 64 | + // This static constructor is called once when the class is first accessed. |
| 65 | + if (File.Exists(PrivateKeyFileName)) |
| 66 | + { |
| 67 | + // If a key file exists, load it. |
| 68 | + string privateKeyBase64 = File.ReadAllText(PrivateKeyFileName); |
| 69 | + _privateKey = RSA.Create(); |
| 70 | + _privateKey.ImportRSAPrivateKey(Convert.FromBase64String(privateKeyBase64), out _); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + // If no key file exists, generate a new one and save it. |
| 75 | + _privateKey = RSA.Create(2048); // Generate a new 2048-bit RSA key. |
| 76 | + byte[] privateKeyBytes = _privateKey.ExportRSAPrivateKey(); |
| 77 | + string privateKeyBase64 = Convert.ToBase64String(privateKeyBytes); |
| 78 | + File.WriteAllText(PrivateKeyFileName, privateKeyBase64); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static string GetPublicKey() |
| 83 | + { |
| 84 | + // Export the public part of the key in Base64 format. |
| 85 | + byte[] publicKeyBytes = _privateKey.ExportRSAPublicKey(); |
| 86 | + return Convert.ToBase64String(publicKeyBytes); |
| 87 | + } |
| 88 | + |
| 89 | + public static string GenerateLicenseKey(string encryptedMachineId) |
| 90 | + { |
| 91 | + // Decrypt the user's machine ID. |
| 92 | + byte[] encryptedBytes = Convert.FromBase64String(encryptedMachineId); |
| 93 | + byte[] decryptedBytes = _privateKey.Decrypt(encryptedBytes, RSAEncryptionPadding.OaepSHA256); |
| 94 | + string machineId = Encoding.UTF8.GetString(decryptedBytes); |
| 95 | + |
| 96 | + // Sign the decrypted machine ID to create the license key. |
| 97 | + byte[] machineIdBytes = Encoding.UTF8.GetBytes(machineId); |
| 98 | + byte[] signature = _privateKey.SignData(machineIdBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); |
| 99 | + return Convert.ToBase64String(signature); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments