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