Click here to Skip to main content
6,595,444 members and growing! (19,709 online)
Email Password   helpLost your password?
Development Lifecycle » Installation » General     Intermediate

Using VB to create & check license keys

By maz2331

Generate and check license keys embedding 16-bit configuration information
VB 7.x, VB 8.0, VB 6, Windows, .NETVS.NET2003, VS2005, CEO, Dev
Posted:23 May 2007
Views:36,795
Bookmarked:74 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 4.89 Rating: 3.76 out of 5
2 votes, 10.0%
1
1 vote, 5.0%
2
1 vote, 5.0%
3
3 votes, 15.0%
4
13 votes, 65.0%
5
Screenshot - Create-1.jpg

Introduction

Create and validate secure "License Keys" for your propreitary 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-charater 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 MS 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 proprietery 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 recieved 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 that 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 attempt to modify key "permission" byte...

Screenshot - Check-Fail.jpg

History

Initial article - 5/23/2007

License

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

maz2331


Member

Location: United States United States

Other popular Installation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralUsing VB 2005 to create & check license keys- Pinmemberbhatiamanoj178:27 22 Apr '09  
QuestionConverting to VB2008 Pinmemberpjvdstap5:00 1 Jul '08  
AnswerRe: Converting to VB2008 [modified] PinmemberDennis Betten15:21 13 Nov '08  
QuestionVB.net PinmemberRens Duijsens3:48 8 Apr '08  
GeneralIn C#??? Pinmemberabalbo3:55 24 Dec '07  
QuestionDotNEt version... PinmemberTanmay Broachwala5:56 24 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 May 2007
Editor:
Copyright 2007 by maz2331
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project