Click here to Skip to main content
11,641,211 members (61,611 online)
Click here to Skip to main content

Using VB to Create & Check License Keys

, 23 May 2007 LGPL3 132.7K 12.8K 128
Rate this:
Please Sign up or sign in to vote.
Generate and check license keys embedding 16-bit configuration information
Screenshot - Create-1.jpg

Introduction

Create and validate secure "License Keys" for your proprietary code and embed up to 16-bits of "configuration data" into the key. This code is flexible and may be used in many different licensing schemes.

Background

Everyone is familiar with getting license keys to activate software. These keys are normally based on various encryption schemes, and serve to validate that a particular user is authorized to install or run the software. The code presented here provides an easy way to integrate this functionality into your own programs.

The keys generated by the sample application are MD5 hashes of a "Licensee" name, a "Serial Number, and a "secret" program name string that is embedded into the code. We then convert the hexadecimal 32-character string to Base32 to shorten the resulting key down to 26 characters. This is easier for end-users to type and looks better and more professional as well.

The code is pretty straight-forward for the most part, and can be easily translated into other languages such as C, C++, C#, Java, etc. and is presented in as "generic" of a Visual Basic form as possible to permit easy integration into applications. It can even be used in VBA applications such as Microsoft Access if desired.

Please note that all code in this article is licensed under the LGPL, so it can be incorporated into your programs with no royalties and doesn't modify the licensing terms of your proprietary code in any way. We do ask that if you make any changes to the key generation code itself that you release the code under the same terms as you received it.

Additionally, this code provides some useful string functions to encode/decode binary values encoded in a string to Base32 and to bitwise left and right shift these values by an arbitrary number of bits.

Using the Code

Simply include the KeyCodes.bas, StrFuncs.bas, and MD5_Crypt.bas files into your project. You can prompt for whatever information you consider to be relevant to your licensing scheme, and it should end up in two string values and one LONG integer value indicating the capabilities you wish to embed into the key code.

It is expected that users of this code will modify the key generation to meet their needs. The keycodes.bas file routines are easily modified to provide different key values and can form an easy base to build your own key code routines. One change that comes to mind is to shorten the Base32 string from 26-characters to 25-characters and then grouping the "digits" in groups of five to provide nicer-looking keys.

Here's the main code that generates the key based on text boxes:

  Private Const MyProductName = "KeyCodeDemoV1"  

   . . . 

  If Not (UserNameT = "") Or Not (ProdNameT = "") Then
    RawKey = GenKeyString(UserNameT, ProdNameT & MyProductName, FeatID)
    BinKey = HexStrToBinStr(RawKey)
    KeyCode = FormatKeyCode(Base32Enc(BinKey), 4)
  Else
    KeyCode = "Please Enter Licensee and/or Serial Number"
  End If

  . . .  

Key Strength

The keys generated by this code are relatively secure, as they are based around an MD5 hash of the data used to generate the key. You can improve security by generating a GUID during the installation, and concatenating it with the "user name" field, and then generating the keys via an online submission process. This is left as an exercise to the reader.

Key tampering is highly discouraged by the code's design. Each key is generated from an MD5 hash of the licensee, serial, and your "secret". Once this hash is created, we XOR the "permissions" bits with the last two characters of the key, then drop the first two characters of the key, run another MD5 hash of the truncated key with permissions, and then use the first and last bytes of the second MD5 hash as the first two characters of the final binary key value. These bytes verify the integrity of the permissions bits.

It is also possible to use an algorithm on the final keys to "scramble" them. Just ensure to "unscramble" them prior to attempts to verify the key with what it should be.

Sample Screenshots

  1. Create a key...

    Screenshot - Create-1.jpg

  2. Entering a key to check...

    Screenshot - Check-1.jpg

  3. A successful check...

    Screenshot - Check-OK.jpg

  4. A check that failed due to an attempt to modify key "permission" byte...

    Screenshot - Check-Fail.jpg

History

  • Initial article - 5/23/2007

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

maz2331
Systems Engineer iServe Technologies
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralVBScript Version - call from SQL Server or batch file Pin
richardbrigzy23-Jul-14 3:31
memberrichardbrigzy23-Jul-14 3:31 
GeneralRe: VBScript Version - call from SQL Server or batch file Pin
richardbrigzy23-Jul-14 4:24
memberrichardbrigzy23-Jul-14 4:24 
QuestionArithmetic operation resulted in an overflow? Pin
NaumHN10-Jan-14 5:03
memberNaumHN10-Jan-14 5:03 
QuestionShortening Key to 25 characters Pin
FireCoder7-Sep-13 0:32
memberFireCoder7-Sep-13 0:32 
Questiondll or C# project Pin
Grasmeiser27-Aug-13 3:21
memberGrasmeiser27-Aug-13 3:21 
GeneralMy vote of 5 Pin
Ahmad Eissa1-Nov-11 3:04
memberAhmad Eissa1-Nov-11 3:04 
QuestionAnyone able to get this to work with VBA and Access 2010? Pin
Member 825030019-Sep-11 14:55
memberMember 825030019-Sep-11 14:55 
QuestionUsing this in access 2010 with vba code Pin
Member 82081192-Sep-11 7:16
memberMember 82081192-Sep-11 7:16 
AnswerRe: Using this in access 2010 with vba code Pin
Member 949628022-Oct-12 15:39
memberMember 949628022-Oct-12 15:39 
QuestionExample code fails with non-Unicode Windows settings Pin
jamie.garroch28-Aug-11 0:00
memberjamie.garroch28-Aug-11 0:00 
AnswerRe: Example code fails with non-Unicode Windows settings Pin
Richard Winters20-Apr-12 0:36
memberRichard Winters20-Apr-12 0:36 
I haven't reviewed the code sample for this project, but I have had this exact problem with my own product key encoder/validator for the Chinese Taiwan language set. Frown | :(

However, the answer was as simple as changing the text encoding type to ASCII for all my file write and read routines, and changing the default language sets to English (en-us). Smile | :)

For our software requiring a code key, we have the user input it, we validate it, and if passes we write it to registration file which program checks for when first loaded to run in demo mode or full mode. Cool | :cool:

Code excerpts for VB2008e below:

{Place at top of code before first public class}Imports System
Imports System.Globalization
Imports System.Security.Permissions
Imports System.Threading
Imports System.Resources
Imports System.IO

{Place at beginning of main code class}
' Changes the CurrentCulture of the current thread to en-US. Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US", False)
Thread.CurrentThread.CurrentUICulture = New CultureInfo("en-US", False)

{Place as necessary to restrict the Product Key writing to pure ASCII}
Dim myProductKey As New StreamWriter(myFilePath, False, System.Text.Encoding.ASCII)

{Place as necessary to restrict the Product Key reading to pure ASCII}
Dim myProductKey As New StreamReader(myFilePath, System.Text.Encoding.ASCII)
........^--your own variable........................^--complete path/filename to product key file

{Since unicode is the default for VB2008e, create pure ascii string for input set}
Dim myAllowedChars As String = ""
'ASCII values for "0123456789"
For vm = 48 To 57
myAllowedChars += Chr(vm)
Next
'ASCII values for "ABCDEFGHIJKLMNP"
For vn = 65 To 78
myAllowedChars += Chr(vn)
Next
'Skip the Letter "O" to prevent confusion with Zero"0"
'ASCII values for "QRSTUVWXYZ"
For vo = 80 To 90
myAllowedChars += Chr(vo)
Next

GOOD LUCK and HAPPY CODING!
RoLoW
GeneralRe: Example code fails with non-Unicode Windows settings Pin
jamie.garroch16-Jul-12 1:49
memberjamie.garroch16-Jul-12 1:49 
AnswerRe: Example code fails with non-Unicode Windows settings Pin
EmpiricaConsultingLtd11-Apr-13 7:27
memberEmpiricaConsultingLtd11-Apr-13 7:27 
AnswerRe: Example code fails with non-Unicode Windows settings Pin
jamie.garroch17-Apr-13 4:44
memberjamie.garroch17-Apr-13 4:44 
Question.net version of this application Pin
Biju VA20-Jun-11 2:35
memberBiju VA20-Jun-11 2:35 
GeneralBase32 String Pin
tiggerc7-Jul-10 3:29
membertiggerc7-Jul-10 3:29 
GeneralUsing VB 2005 to create & check license keys- Pin
bhatiamanoj1722-Apr-09 7:27
memberbhatiamanoj1722-Apr-09 7:27 
QuestionConverting to VB2008 Pin
pjvdstap1-Jul-08 4:00
memberpjvdstap1-Jul-08 4:00 
AnswerRe: Converting to VB2008 [modified] Pin
Dennis Betten13-Nov-08 14:21
memberDennis Betten13-Nov-08 14:21 
GeneralRe: Converting to VB2008 [modified] Pin
Member 1011865529-Aug-13 23:26
memberMember 1011865529-Aug-13 23:26 
QuestionVB.net Pin
Rens Duijsens8-Apr-08 2:48
memberRens Duijsens8-Apr-08 2:48 
QuestionIn C#??? Pin
abalbo24-Dec-07 2:55
memberabalbo24-Dec-07 2:55 
QuestionDotNEt version... Pin
Tanmay Broachwala24-May-07 4:56
memberTanmay Broachwala24-May-07 4:56 
AnswerRe: DotNEt version... Pin
Member 109411802-Jun-15 17:20
memberMember 109411802-Jun-15 17:20 

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 | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 23 May 2007
Article Copyright 2007 by maz2331
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid