Click here to Skip to main content
11,704,044 members (69,954 online)
Click here to Skip to main content

License Keys in VB.NET - Revisited

, 10 Jul 2014 LGPL3 18.3K 3.3K 36
Rate this:
Please Sign up or sign in to vote.
License Key class to make generating and validating keys easy.

Introduction

This is a VB.NET class that creates secure license keys that encode a Product ID, a Serial Number, and 16 bits of configuration data which can optionally be treated as a date value, such as for an expiration date for subscription-based licensing.

This solution is simpler and easier to use than my previous one from the article at Using VB to Create & Check License Keys, which uses a different approach entirely and has some challenges in a .NET environment.

Please note that earlier versions of this article used code that will result in keys that are not compatible with this code.

Background

Software key codes are a popular way of authorizing use of a program. They need to be reasonably short, but secure from hacks intended to work around them. Allowing the flexibility to store an extra 16-bits of configuration data that can be used as either a raw number, a date, or as 16 boolean options is a very useful addition to many other approaches.

Using the Code

Included in the source project is a class called LicKey.vb. Simply add this class and the Base32.vb and Encryption.vb source files to your project, then create an instance of a LicKey object:

Dim LK As New LicKey

To generate a code, simply set the SerialNo, ProductID, and OptValue properties, and read the result with the KeyCode property:  Optionally, you can set the Salt property to make your keys more unique than the default.

Private Sub AppMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     LK.SerialNo = 1
     LK.ProductID = 1
     LK.OptValue = 0

     Dim Code As String = LK.KeyCode
     tbKey.Text = Code
     tbLen.Text = tbKey.Text.Length
     Call SetChecks()
 End Sub

 Private Sub tbKey_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbKey.TextChanged

     LK.KeyCode = tbKey.Text.ToUpper

     If LK.SerialNo = 0 Then
         tbDecode.Text = "Invalid Key Entered"
     Else
         tbDecode.Text = "Serial: " & LK.SerialNo.ToString & vbCrLf
         tbDecode.Text = tbDecode.Text & "ProdID: " & LK.ProductID.ToString & vbCrLf
         tbDecode.Text = tbDecode.Text & "OptVal: " & LK.OptValue.ToString & vbCrLf
     End If
     Call SetChecks()
 End Sub

The above creates an instance of the class named LK, and sets the relevant parameters, and then reads out the encoded license key and displays it in a textbox named tbKey, along with its length in a tbLen textbox.

Decoding a key is very easy as well. Once an instance is created, simply set the KeyCode() property to the license key. If it is valid, then the SerialNo, ProductID, and OptValue properties will hold the data that is encoded into the key. If it is invalid, then SerialNo, ProductID, and OptValue will all be zero.

The OptValue() property may be used in any of three modes. It may be treated as a simple 16-bit number, as a set of 16 options which can be read via the OptionEnabled(x) method, set via SetOption(x) method, or unset via the UnsetOption(x) method. It may also be used in a date mode via the ExpDate() property.

Internally, the class generates and decodes keys that are AES-256 symmetrically encrypted with a key generated from the calling program's Application.ProductName property. This ensures that all keys created for a particular product can only be used with that product - other use of the same library will generate an incompatible key code. The class internally holds two Integer and one UInt16 variables in a SerialInf structure, which is converted to a byte array and then encrypted to generate a code, or a code is decrypted and marshaled into the structure.

Note that an invalid code cannot be decrypted, and will result in all zeroes in the structure.

Demo Application

Included in the solution is a simple demo application that shows all of the features of the class and can serve as the base for your own keycode generation and management system.

Credits

Many thanks go to wampus1 for his .NET Encryption Simplified article and class, which made the encryption/decryption of the key codes much easier.

History

  • Created on 3/18/2014
  • Updated on 7/9/2014 to add the Salt property and to make internal keys more deterministic.

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

 
QuestionHow do I use this? Pin
savedlema20-May-15 9:12
membersavedlema20-May-15 9:12 
QuestionExpDate <-> Options Pin
PeterCicaMolnar13-Sep-14 23:56
memberPeterCicaMolnar13-Sep-14 23:56 
AnswerRe: ExpDate <-> Options Pin
maz233130-Apr-15 19:05
membermaz233130-Apr-15 19:05 
QuestionIs there an option to expand on the Product ID and Serial No ? Pin
Tino Fourie15-Jul-14 11:11
memberTino Fourie15-Jul-14 11:11 
QuestionError Downloading Pin
Demon Slayer11-Jun-14 0:12
memberDemon Slayer11-Jun-14 0:12 
Questiontrying your code but i see a problem Pin
duup11-Apr-14 7:05
memberduup11-Apr-14 7:05 
AnswerRe: trying your code but i see a problem Pin
maz23319-Jul-14 11:16
membermaz23319-Jul-14 11:16 
QuestionError packing the solution Pin
Hagane_dc25-Mar-14 14:30
memberHagane_dc25-Mar-14 14:30 
QuestionZip file is corrupted. Pin
Member 384703922-Mar-14 3:56
memberMember 384703922-Mar-14 3:56 
AnswerRe: Zip file is corrupted. Pin
Member 807299624-Mar-14 16:48
memberMember 807299624-Mar-14 16:48 
GeneralMy vote of 5 Pin
Prasad Khandekar21-Mar-14 3:53
professionalPrasad Khandekar21-Mar-14 3:53 
QuestionZIP File Pin
Rinaldo196118-Mar-14 9:58
memberRinaldo196118-Mar-14 9:58 
AnswerRe: ZIP File Pin
maz233118-Mar-14 10:45
membermaz233118-Mar-14 10:45 
GeneralRe: ZIP File Pin
Rinaldo196118-Mar-14 12:08
memberRinaldo196118-Mar-14 12:08 
GeneralRe: ZIP File Pin
Malenko18-Mar-14 22:02
memberMalenko18-Mar-14 22:02 

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
Web01 | 2.8.150819.1 | Last Updated 10 Jul 2014
Article Copyright 2014 by maz2331
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid