Click here to Skip to main content
Click here to Skip to main content

License Key Generation

, 17 Jul 2005
Rate this:
Please Sign up or sign in to vote.
An article on license key generation.


This article is about a license key generation program. As you know when you get a software package it usually requires a license key. The keys that are generated show various pieces of information, such as product, customer and version. With this program and library you can create the key and later read it in to validate that the correct key was used. This is used not only for selling commercial products but also in deploying enterprise applications. With a license key you can turn features on and off for each customer, have product feature expire at the end of a demo period, or any other type of enabling or disabling you desire. Since I wanted to put this code in CodeProject I adhered to using NUnit as my unit testing tool, NDoc as my documentation tool, and FxCop as my code analyzer. In this project, I created an interactive test tool to test the various features of the library.


There are various ways to create keys; this article shows one such way. Another site that does License keys is SP extreme or Open License.

Using the code

The license keys are created with either a base 10 or base 16 integer data set. Most keys are base 16 for all of the numbers. The license key template is a string of characters that represent what actions will be taken when the key is created and decoded. Two types of templates are Bytes and Bits. Bytes are used when the key will be a simple key that does not require a lot of fields and data encoding. Bits are used when there will be more fields and the encoding will be harder to decipher. This version requires that all fields be on the byte boundary so it is a little easier to decipher the end result. The next version will not be aligned on a byte boundary so it will be harder to decipher. The key template has two characters that are defined by default. The x character states just a filler or random number. The c character defines a checksum character. These two characters are the only two that are reserved.

A sample of the template and the resulting license key for Byte values and Decimal numbers are:


A sample of the template and the resulting license key for Bit values and Hex numbers are:


With this library you can define the tokens that you want to replace real values with, such as product numbers, version numbers, serial numbers, or user counts.

        GenerateKey    gkey;
        gkey = new GenerateKey();
        gkey.LicenseTemplate = "xxxxxxxx-xxxx-xxxxxxxx-xxxx";
        gkey.MaxTokens = 0;
        gkey.UseBase10 = true;
        gkey.UseBytes = true;
        finalkey = gkey.GetLicenseKey();

The above example shows how to create a license key that has no tokens, uses base 10 numbers, and uses bytes for the template definition. Since no tokens are used, it really just creates a random number.

        GenerateKey    gkey;
        gkey = new GenerateKey();
        gkey.LicenseTemplate = "vvvvppppxxxxxxxxxxxx" + 
           "-xxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx" + 
        gkey.MaxTokens = 2;
        gkey.AddToken(0, "v", LicenseKey.GenerateKey.TokenTypes.NUMBER, "1");
        gkey.AddToken(1, "p", LicenseKey.GenerateKey.TokenTypes.NUMBER, "2");
        gkey.UseBase10 = false;
        gkey.UseBytes = false;
        finalkey = gkey.GetLicenseKey();

This example shows how to create a license key that has two tokens. The first token sets the version to use a token of v with a value of 1. The second token sets the product to use a token of p with a value of 2. It also uses hex numbers and bits for the template definition. The various token types are NUMBER, CHARACTER, and DATE. In NUnit, the NUMBER and CHARACTER types are used extensively. I have not finished DATE as of this article but will do so later. At this time I am not sure if you need it since you have NUMBER available to use. The original reason I thought DATE might be of value is it might pack better than just using numbers.

Some of the additional classes that I had to write to support the license key generation are a random number generator, a data type parser, a number display class, and a checksum class.

The random number class uses the C# Random class but adds some additional methods to support the requirements. The methods are:

  • GetRandomNumber

    Get a random number.

  • SetMaxLength

    Sets the max length of the number.

The checksum number class uses checksum routines that are pretty basic. Additional advanced algorithms can be added simply by adding the routine and an enumerated type. The methods are:

  • ChecksumAlgorithm

    Get/set the property to use the different checksum algorithms.

  • ChecksumNumber

    Gets the checksum number

  • CalculateChecksum

    Calculate the checksum based on the selected algorithm.

Since so many variations of a license key can be done I did all of the unit tests using NUnit. Another advantage of NUnit was that if I fixed something in one area I could also make sure that I did not break something or another. Since the keys are using random numbers, I iterated thirty times in many of the tests to make sure the numbers matched. As part of the tests, I also decoded the same key to make sure that whatever I encoded I also decoded the same value.

When you open the solution, you will notice there are three projects. One is the library so that when it is built anyone can use it. The second is an interactive test program so that you can test individual license keys and tokens. The third is the NUnit project so that you can add, run and debug the unit tests.

Points of Interest

Doing bit shifts in C# is somewhat different than C++ in that they are not allowed on chars only on integers. The time saving that I made by doing the entire unit test in NUnit was well worth the effort, I never would have had this much code coverage without using it. The Help system is written using NDoc so that is it in a standard format. Not to mention it is easier to search and use. For the library part, I ran FxCop routinely to make sure many of the best practices are adhered to.


If everyone is interested I can put the code at SourceForge as well. This way others can add, update or use the source code. I plan on adding the ability to set tokens on bit boundaries and not just on byte boundaries.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
I am a Director of Engineering, have an MBA and work in C# forms, Asp.Net and I have been writing Windows program since windows 3.0. I am currently working in the Healthcare industry.
I enjoy reading, music (most types), cars, and cigars. I am involved in opensource projects at codeplex.
My linkedin link is
Follow on   Twitter   Google+

Comments and Discussions

QuestionAm I blind? PinmemberHiDensity30-Jul-14 8:01 
QuestionValidating Licence PinmemberChris Anders15-Jul-14 5:14 
Suggestionimprovment in current scenario Pinmemberbhargavpp17-Oct-12 0:13 
QuestionExtension for Decoding on seperate Machine / Little bug in DisassembleKeyBits(string token) PinmemberMichael Bauschert24-Sep-12 23:52 
GeneralThis is really cool PinmemberImmorta112-Jul-12 9:11 
GeneralMy vote of 5 Pinmembersalmanabbasi29-Jun-12 2:40 
GeneralFetch missing nunit.framwork; License Key Generation PinmemberAarKayCee16-Apr-11 4:26 
GeneralRe: Fetch missing nunit.framwork; License Key Generation Pinmemberelectrawinds31-Oct-11 22:36 
GeneralMy vote of 5 Pinmembersateesh munagala30-Mar-11 8:09 
GeneralMy vote of 5 Pinmemberpolczym26-Mar-11 3:17 
GeneralMy vote of 5 PinmemberPravin Patil, Mumbai30-Jan-11 23:18 
Generalnunit.framework Pinmemberewansw24-Aug-10 23:37 
QuestionDate type Pinmembergirigadevi13-Apr-10 21:36 
GeneralValidating Encoded value Pinmemberkathyani6-Dec-09 19:26 
GeneralfinalKeys are not unique Pinmemberkwilder9-Aug-09 8:00 
GeneralRe: finalKeys are not unique PinmemberDonsw10-Aug-09 1:07 
QuestionEncoding random text Pinmembercbuffett20-Feb-07 14:14 
Generaldecode a given key PinmemberA55imilate9-Oct-06 2:34 
GeneralMany thanks PinmemberDavidAllen17-Mar-06 0:19 
QuestionHow to validate a key Pinmemberbarbutz7-Mar-06 7:00 
AnswerRe: How to validate a key PinmemberChris Anders24-Jul-14 8:00 
GeneralDecode an existing key Pinmemberdelfo4-Feb-06 5:11 
GeneralRe: Decode an existing key PinmemberMichael Bauschert24-Sep-12 23:56 
Questionlicense in code ? Pinmemberwebber12345629-Aug-05 18:28 
AnswerRe: license in code ? PinmemberDonsw3-Sep-05 7:08 
GeneralRe: license in code ? Pinmemberaotter23-Nov-05 6:46 
GeneralRe: license in code ? PinmemberDonsw25-Nov-05 6:58 
GeneralRe: license in code ? Pinmemberorthimnas221-Jul-10 9:24 
GeneralRe: license in code ? PinmemberDonsw2-Aug-10 2:52 
GeneralRe: license in code ? PinmemberMember 97301044-Jan-13 14:19 
AnswerRe: license in code ? PinmemberJeffrey Walton16-Nov-06 16:52 
GeneralEncoding hex values Pinmembermarkeric28-Jul-05 5:37 
GeneralRe: Encoding hex values PinmemberDonsw28-Jul-05 15:13 
GeneralRe: Encoding hex values PinmemberDonsw28-Jul-05 16:15 
QuestionSoftice ? PinmemberTrance Junkie27-Jul-05 22:03 
AnswerRe: Softice ? PinmemberDonsw28-Jul-05 15:15 
GeneralRe: Softice ? [modified] PinmemberYaroslav Sterkhov31-Jul-06 11:53 
GeneralNearly there PinmemberJaseNet27-Jul-05 20:35 
GeneralEncryption / Decryption PinsussAnonymous22-Jul-05 2:39 
GeneralRe: Encryption / Decryption PinmemberDonsw22-Jul-05 10:06 
GeneralRe: Encryption / Decryption PinmemberJeffrey Walton16-Nov-06 16:51 
GeneralCool but..... PinmemberRuben_uniek19-Jul-05 4:35 
GeneralRe: Cool but..... PinmemberAgeKay22-Jul-05 3:46 
GeneralRe: Cool but..... PinmemberDonsw22-Jul-05 10:09 
GeneralRe: Cool but..... Pinmemberjeropa2k26-Jul-05 3:23 
GeneralRe: Cool but..... PinmemberArch4ngel28-Jul-05 11:30 
GeneralRe: Cool but..... Pinmember[Marc]1-Aug-05 18:01 
GeneralRe: Cool but..... PinmemberDonsw3-Aug-05 2:30 
GeneralRe: Cool but..... Pinmember[Marc]3-Aug-05 15:18 
GeneralRe: Cool but..... PinsussAnonymous30-Sep-05 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 18 Jul 2005
Article Copyright 2005 by Donsw
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid