IMG-LOGO

How to create a simple license key generator in C#?

andy - 05 Oct, 2019 29416 Views 12 Comment

In this tutorial, I am going to show you how to create a simple license key generator using MD5 encoding in C#. License key is useful especially if you own a software company and want to license your products based on company/single-use strategy.

We are going to create the license key based on three important keys which are:

  1. Your Product Name

    The product name is your main unique identifier. It is important to use especially if you have multiple products.

  2. Client Id Identifier

    The client id identifier is a unique identifier belongs to your client or customer. It can be their website domain name or if you are based on Desktop software, you can use the CPU id or event the Windows Product id on client local machine key, etc. Or if you are targetting mobile audience, you can use their mobile IMEI number.

  3. Version Number

    The version number represents the current version of your module or application.

We are going to create a console application in C#. I am separating the methods into 2 sub methods. You are free to combine them if you want. The license key will be in 28 characters length with separated by a dash for every 4 letters. I have included the unformatted license key. Just in case you prefer the unformatted version.

Here is the full source code in C#.

using System;
using System.Security.Cryptography;
using System.Text;

namespace LicenseKeyGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            string productName = "MyModuleName";
            string clientIdentificationId = "clientwebsite.com";
            string versionNumber = "01.00.00";

            string productIdentifier = (productName + "-" + clientIdentificationId + "-" + versionNumber).ToLower();

            Console.WriteLine(string.Format("Product Identifier: {0}", productIdentifier));
            Console.WriteLine(string.Format("Unformatted License Key: {0}", GetMd5Sum(productIdentifier)));
            Console.WriteLine(string.Format("License Key: {0}", FormatLicenseKey(GetMd5Sum(productIdentifier))));
            Console.ReadLine();
        }

        static string GenerateLicenseKey(string productIdentifier)
        {
            return FormatLicenseKey(GetMd5Sum(productIdentifier));
        }

        static string GetMd5Sum(string productIdentifier)
        {
            System.Text.Encoder enc = System.Text.Encoding.Unicode.GetEncoder();
            byte[] unicodeText = new byte[productIdentifier.Length * 2];
            enc.GetBytes(productIdentifier.ToCharArray(), 0, productIdentifier.Length, unicodeText, 0, true);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(unicodeText);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                sb.Append(result[i].ToString("X2"));
            }
            return sb.ToString();
        }

        static string FormatLicenseKey(string productIdentifier)
        {
            productIdentifier = productIdentifier.Substring(0, 28).ToUpper();
            char[] serialArray = productIdentifier.ToCharArray();
            StringBuilder licenseKey = new StringBuilder();

            int j = 0;
            for (int i = 0; i < 28; i++)
            {
                for (j = i; j < 4 + i; j++)
                {
                    licenseKey.Append(serialArray[j]);
                }
                if (j == 28)
                {
                    break;
                }
                else
                {
                    i = (j) - 1;
                    licenseKey.Append("-");
                }
            }
            return licenseKey.ToString();
        }
    }
}

Demo Files

You can download the sample code below.

Download

Comments

Susan SSS
29 Jan, 2020
Why would you want to include the full version number in the key? Every time you release a new version... that user's key... is now a totally different key? Huh?
andy
31 Jan, 2020
Yes that is right. This will be quite handy especially if you only want to give your customers to specific free upgrade. Otherwise they will receive the free upgrade for live.
Jeff
06 Jul, 2020
There are several reasons to have your version # as part of the keys. a) It keeps Kengen to only work with one version of your software aka work for the crackers b) when you decode it on your side it allows you to create a partitioning scheme licensing data. This can be accomplished by redirection on your web API endpoint on the licensing service.
andy
26 Jul, 2020
Jeff, yes that is right. This is the reason why we want to limit the upgrade. So when our tried to upgrade our software without purchasing or extending new license. They will end up have to contact us. The web api end point is good to check the status of the license or requesting a new license key. What i would suggest is to put the keygen itself in the code but we have to ensure the method is private and we obfuscate the code and disable the code to be decompiled, there some software available to do so. The reason for the keygen in the code to ensure the program still work even there is no internet connection if we want to validate a request of the program through a web api end point.
Jeff
06 Nov, 2020
The reason to embed the version into the key to limit the ability of people who develop kengen crackers to only one version at a time to be able to generate fake licenses.
Dan
08 Feb, 2021
Hi, Thanks for the demo. How do you verify the key is legit on the users side when he runs his copy of the app?
andy
20 Feb, 2021
You can embed the above code into your application code. There you inside your application you can perform a validation against the provided license key. One thing to remember, you have to encrypt your code or prevent it to be compiled. Otherwise, someone will be able to review the code and generate the code. Another extra option you can use is to enable sending the application information into your server. It will then expected to return a certain value and you can perform extra validation checking if needed.
Arvindo
01 Jul, 2021
Hi, Of course this code seems to be helpfull. Thank you guys...keep your self with nice ideas. Hugs
Robert
02 Sep, 2021
Your blog is too much useful for me.Thanks for sharing. Barocrack
Austin
15 Nov, 2021
Good day, thanks for the tutorial, please how do i reverse the license code back to it original text using the MD5 for validation purposes Thanks Austino
andy
15 Nov, 2021
Hi Austino, the purpose of this license generator is not doing the reverse. Let says you created a program for a website that requires a domain name to activate. You will need your client to provide the domain name right. By having this information you can compare whether the key is valid or not. So there is no need to do the reverse.
shubhma
09 Dec, 2021
How to store the generated lic key in our system at particular location?
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

How to remove html tags from string in c#?

Sometimes you need to remove HTML tags from string to ensure there are no dangerous or malicious scripts especially when you want to store the string or data text into the database.

Free Open Source .Net CMS

Are you looking for an open source .Net CMS for your site? When we say open source it means you can get full access to the source code without having to pay a cent.