Click here to Skip to main content
15,513,571 members
Articles / Programming Languages / Visual Basic
Tip/Trick
Posted 18 Mar 2014

Stats

90.6K views
11.3K downloads
46 bookmarked

License Keys in VB.NET - Revisited

Rate me:
Please Sign up or sign in to vote.
4.85/5 (15 votes)
10 Jul 2014LGPL33 min read
License Key class to make generating and validating keys easy.

Image 1

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:

VB.NET
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.

VB.NET
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)


Written By
Systems Engineer iServe Technologies
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionusing key in other form Pin
Member 140555398-Dec-18 20:32
Member 140555398-Dec-18 20:32 
QuestionUsing ExpDate Pin
DeveloppeurVb30-Mar-18 23:47
DeveloppeurVb30-Mar-18 23:47 
QuestionThe date is changing Pin
Balázs Lengyel18-Feb-18 6:19
Balázs Lengyel18-Feb-18 6:19 
AnswerRe: The date is changing Pin
maz23312-Oct-18 5:48
maz23312-Oct-18 5:48 
QuestionNew property Pin
chenaz198713-Feb-18 18:15
chenaz198713-Feb-18 18:15 
AnswerRe: New property Pin
maz23312-Oct-18 5:49
maz23312-Oct-18 5:49 
Questioncompiled exe recognized as trojan (Win32/Fuerboos.A!cl) by Windows Defender Pin
Member 238935924-Jan-18 14:00
Member 238935924-Jan-18 14:00 
AnswerRe: compiled exe recognized as trojan (Win32/Fuerboos.A!cl) by Windows Defender Pin
Member 238935925-Jan-18 14:54
Member 238935925-Jan-18 14:54 
QuestionCan you develop the same for access database Pin
Member 1262153622-Feb-17 9:21
Member 1262153622-Feb-17 9:21 
AnswerRe: Can you develop the same for access database Pin
maz233126-May-17 12:32
maz233126-May-17 12:32 
Questioncan I use processor ID and BIOS ID instead of serial and prod ID ? Pin
salemahmeed18-Dec-16 22:35
salemahmeed18-Dec-16 22:35 
AnswerRe: can I use processor ID and BIOS ID instead of serial and prod ID ? Pin
maz233126-May-17 12:31
maz233126-May-17 12:31 
Questionuse processor ID and BIOS ID instead of serial and prod ID Pin
salemahmeed18-Dec-16 22:35
salemahmeed18-Dec-16 22:35 
QuestionUse Option AND ExpDate Pin
Member 1253931726-May-16 0:17
Member 1253931726-May-16 0:17 
AnswerRe: Use Option AND ExpDate Pin
maz233119-Oct-16 18:26
maz233119-Oct-16 18:26 
QuestionFloating licences Pin
Member 86204139-Nov-15 11:14
Member 86204139-Nov-15 11:14 
BugSerial Relative to Current Date. Pin
iProgramIt24-Oct-15 20:46
professionaliProgramIt24-Oct-15 20:46 
Hello, I have found a flaw.
The serial Expiry Date is relative to the Current Date. For example:

25/10/2015 has expiry date of 27/10/2015
26/10/2015 has expiry date of 28/10/2015
27/10/2015 has expiry date of 29/10/2015

This has a difference of two days. I don't know if I have used this code incorrectly or if it is simply a flaw. If you could reply, I would really appreciate it.

Yours Sincerely,
iProgramIt
QuestionHow do I use this? Pin
savedlema20-May-15 10:12
savedlema20-May-15 10:12 
QuestionExpDate <-> Options Pin
PeterCicaMolnar14-Sep-14 0:56
PeterCicaMolnar14-Sep-14 0:56 
AnswerRe: ExpDate <-> Options Pin
maz233130-Apr-15 20:05
maz233130-Apr-15 20:05 
QuestionIs there an option to expand on the Product ID and Serial No ? Pin
Tino Fourie15-Jul-14 12:11
Tino Fourie15-Jul-14 12:11 
AnswerRe: Is there an option to expand on the Product ID and Serial No ? Pin
Member 1219908711-Dec-15 12:53
Member 1219908711-Dec-15 12:53 
QuestionError Downloading Pin
Demon Slayer11-Jun-14 1:12
Demon Slayer11-Jun-14 1:12 
Questiontrying your code but i see a problem Pin
duup11-Apr-14 8:05
duup11-Apr-14 8:05 
AnswerRe: trying your code but i see a problem Pin
maz23319-Jul-14 12:16
maz23319-Jul-14 12:16 

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

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