Published on: 05 Oct, 2019

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

Tumblr Digg Google Plus

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.
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.