How to Generate Serial Key in C#

By FoxLearn 12/9/2024 1:37:39 PM   9.66K
Serial key generation and validation are crucial components of software licensing.

The SKGL is a lightweight .NET library that simplifies these processes. This article explains how to implement serial key generation and validation using SKGL in a Windows Forms application.

How to Implement Serial Key Generation and Validation Using SKGL Library?

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "SerialKeyDemo" and then click OK

Right click on your project select Manage NuGet Packages -> Search SoftwareProtector -> Install

software protector in c#

Software Protector is an open source 100% managed .NET licensing system based on SKGL Project. Generate keys for your software, and validate them using SKGL library in your own project.

Design your form as shown below.

c# license key

The btnGenerate_Click event handler uses the SKGL.Generate class to create a serial key. This key is based on a secret phrase and the number of valid days provided by the user.

private void btnGenerate_Click(object sender, EventArgs e)
{
    // Generate a serial key
    SKGL.Generate generate = new SKGL.Generate();
    generate.secretPhase = txtPassword.Text; // Secret phrase from user input
    txtSerial.Text = generate.doKey(Convert.ToInt32(txtDay.Text)); // Generate the key
}

The btnValid_Click event handler uses the SKGL.Validate class to verify the serial key. It checks the key’s validity and provides details like the creation date, expiration date, and remaining valid days.

private void btnValid_Click(object sender, EventArgs e)
{
    // Validate the serial key
    SKGL.Validate validate = new SKGL.Validate();
    validate.secretPhase = txtPassword.Text; // Secret phrase used during key generation
    validate.Key = txtSerial.Text; // Serial key to validate
    txtStatus.Text = "Creation date: " + validate.CreationDate + "\r\n" + "Expire date: " + validate.ExpireDate + "\r\n" + "Day left: " + validate.DaysLeft;
}

VIDEO TUTORIAL