by Brandon Staggs.

Editor’s Note: Brandon has written a really useful article for microISVs struggling to develop their own serial number schemes, and was kind enough to let me reprint it here at 47hats.com.

Most micro-ISVs use a serial number/registration code system to allow end users to unlock or activate their purchase. The problem most of us have run into is that a few days or weeks after our software is released, someone has developed a keygen, a crack, or has leaked a serial number across the internet.

There are several possible solutions to this problem. You could license a system like Armadillo/Software Passport or ASProtect, or you could distribute a separate full version as a download for your paying customers. Each option has advantages and disadvantages. What I am going to show you is a way to keep “rolling your own” license key system while making working cracks harder for crackers to produce, and working keygens a thing of the past.

Aside: If you think it’s crazy to post this publicly where crackers can see it, don’t worry about that. I’m not posting anything they haven’t seen before. The entire point of partial key verification is that your code never includes enough information to reverse engineer a key generation algorithm. Also, I offer no warranty of any kind — this is for your information only! Now, on with things.

Our license key system must meet some basic requirements.

  1. License keys must be easy enough to type in.
  2. We must be able to blacklist (revoke) a license key in the case of chargebacks or purchases with stolen credit cards.
  3. No “phoning home” to test keys. Although this practice is becoming more and more prevalent, I still do not appreciate it as a user, so will not ask my users to put up with it.
  4. It should not be possible for a cracker to disassemble our released application and produce a working “keygen” from it. This means that our application will not fully test a key for verification. Only some of the key is to be tested. Further, each release of the application should test a different portion of the key, so that a phony key based on an earlier release will not work on a later release of our software.
  5. Important: it should not be possible for a legitimate user to accidentally type in an invalid key that will appear to work but fail on a future version due to a typographical error.

The solution is called a Partial Key Verification System because your software never tests the full key. Since your application does not include the code to test every portion of the key, it is impossible for a cracker to build a working valid key generator just by disassembling your executable code.

This system is not a way to prevent cracks entirely. It will still be possible for a cracker to edit your executable to jump over verification code. But such cracks only work on one specific release, and I’ll suggest a couple of tricks to make their job harder to complete successfully.

Let’s jump right in. I’ll show you the system with Delphi code. (Given the readable nature of Delphi Pascal, you should be able to use these examples to build your own system in any language.)

An aside: if you think it’s crazy to post this publicly where crackers can see it, don’t worry! I’m not posting anything they don’t know. The whole point of this system is that your code never contains enough information for a cracker to reverse-engineer your key system. My blog post here doesn’t give them any information they don’t already have. Also, I’m not offering any kind of warranty with this information. This is for your information only, and all that. Now, on with things!

1. The Key Format

This example will create and test keys of 20 characters (with hyphens added for user convenience). A valid key will look like this:
A279-1717-7D7A-CA2E-7154

Once the hyphens are stripped, this is how the key breaks down:

Seed value

Key Byte 0

Key Byte 1

Key Byte 2

Key Byte 3

Checksum

A2791717

7D

7A

CA

2E

7154

This sample system only uses four bytes for key verification, but a real system should use many more and larger values, so keep that in mind if you begin implementing your own PKVS.

2. The Seed Value

This sample system uses a 64-bit integer as a “seed” to generate the “Key Bytes” from. The example project just generates random values for seeds, but when you implement a system like this, you must ensure that the seeds are always unique, because the seed is used when blacklisting a key. The seed could itself be a hash of a user name and time of generation, or any number of things

3. Computing Key Bytes

Here is the heart of the PKVS. Each “byte” of the key is a result of an operation on the seed value. Here is a simple “byte” value computation function. It performs some bit twiddling based on the supplied parameters:

function PKV_GetKeyByte(const Seed : Int64; a, b, c : Byte) : Byte;begin  a := a mod 
style='color:#E00000'>25;  b := b mod 
style='color:#E00000'>3;
  if a mod 
style='color:#E00000'>2 = 0 then    result := ((Seed shr a) and $
style='color:#E00000'>000000FF) xor ((Seed shr b) or c)  else    result := ((Seed shr a) and $
style='color:#E00000'>000000FF) xor ((Seed shr b) and c);end;

We’ll see in a moment how this function is used in the key generation and checking algorithms. Please keep in mind that this example function is very simplistic. A more effective function would use larger values than bytes and employ a more complex hashing system.

4. We need a checksum

Once we have our seed and bytes formed into a string of characters, we need to add a checksum to it. This way we can know when a user makes a mistake entering their key, without having to actually check each portion of the key for validity.

function PKV_GetChecksum(const s : String) : String;
var  left, right, sum : Word;  i : Integer;begin  left := $
style='color:#E00000'>0056;  right := $
style='color:#E00000'>00AF;
  if Length(s) > 
style='color:#E00000'>0 then    for i := 
style='color:#E00000'>1 to Length(s) do    begin      right := right + Byte(s[i]);      if right > $
style='color:#E00000'>00FF then        Dec(right, $
style='color:#E00000'>00FF);      Inc(left, right);      if left > $
style='color:#E00000'>00FF then        Dec(left, $
style='color:#E00000'>00FF);    end;
  sum := (left shl 
style='color:#E00000'>8) + right;  result := IntToHex(sum, 
style='color:#E00000'>4);end;

This function computes a simple 8-bit value from the supplied string and returns it as a hexidecimal string, which we tack on to the end of our key.

Note that because this routine is always used to check a key in your application, a would-be keygen coder will be able to generate keys that pass the checksum test. That’s okay. The point of the checksum is only to prevent your users from mistyping their own valid license keys, and it will aid in determining if a key was deliberately forged.

5. Putting it together: generating a valid key

For our key generation program, we’re going to need a single function we can call to get a license key string from a seed value. Here it is:

function PKV_MakeKey(const Seed : Int64) : String;var  KeyBytes : array[
style='color:#E00000'>0..3] of Byte;  i : Integer;begin  
style='color:blue'>// Fill KeyBytes with values derived from Seed.  
style='color:blue'>// The parameters used here must be extactly the same  
style='color:blue'>// as the ones used in the PKV_CheckKey function.  
style='color:blue'>// A real key system should use more than four bytes.
  KeyBytes[
style='color:#E00000'>0] := PKV_GetKeyByte(Seed, 
style='color:#E00000'>24, 3, 
style='color:#E00000'>200);  KeyBytes[
style='color:#E00000'>1] := PKV_GetKeyByte(Seed, 
style='color:#E00000'>10, 0, 
style='color:#E00000'>56);  KeyBytes[
style='color:#E00000'>2] := PKV_GetKeyByte(Seed, 
style='color:#E00000'>1, 2, 
style='color:#E00000'>91);  KeyBytes[
style='color:#E00000'>3] := PKV_GetKeyByte(Seed, 
style='color:#E00000'>7, 1, 
style='color:#E00000'>100);
   
style='color:blue'>// the key string begins with a hexidecimal string of the seed  result := IntToHex(Seed, 
style='color:#E00000'>8); 
  
style='color:blue'>// then is followed by hexidecimal strings of each byte in the key  for i := 
style='color:#E00000'>0 to 3 do    result := result + IntToHex(KeyBytes[i], 
style='color:#E00000'>2);
  
style='color:blue'>// add checksum to key string  result := result + PKV_GetChecksum(result);
  
style='color:blue'>// Add some hyphens to make it easier to type  i := Length(result) - 
style='color:#E00000'>3;  while (i > 
style='color:#E00000'>1) do  begin    Insert(
style='color:#00A000'>'-', result, i);    Dec(i, 
style='color:#E00000'>4);  end;end;

Important: never compile this valid key generator function into your release application! It is only to be used on your end to generate valid keys. The success of a PKVS is based on keeping the parameters used in the PKV_GetKeyByte call secret and away from the prying eyes of crackers. Remember: if it’s in your compiled executable, a cracker can see it!

6. Testing a key in your application

Your application needs two functions for testing a key.

The first is a function for testing only the checksum value. You’ll use this to test the key when a user types it in. To make it harder for a cracker, this is all you want to test at first. More on this later.

The second function actually verifies portions of the key to see if they are valid, and also checks against the blacklist to see if a key should be rejected based on its seed value.

First we need to define the constants:

const  KEY_GOOD = 
style='color:#E00000'>0;  KEY_INVALID = 
style='color:#E00000'>1;  KEY_BLACKLISTED = 
style='color:#E00000'>2;  KEY_PHONY = 
style='color:#E00000'>3; 
  BL : array[
style='color:#E00000'>0..0] of String = (                                
style='color:#00A000'>'11111111'                               );

Above, BL is an array of blacklist strings. Important: only include the seed portion. Remember: if you put it in your program, a cracker can see it. So do not put an entire key in the blacklist. That just makes it easier for a cracker to see what a valid key should look like.

Here is the checksum check function:

function PKV_CheckKeyChecksum(const Key : String) : Boolean;var  s, c : String;begin  result := False;
  
style='color:blue'>// remove cosmetic hypens and normalize case  s := UpperCase(StringReplace(Key, 
style='color:#00A000'>'-', '', [rfReplaceAll]));  if Length(s) <> 
style='color:#E00000'>20 then    exit; 
style='color:blue'>// Our keys are always 20 characters long
  
style='color:blue'>// last four characters are the checksum  c := Copy(s, 
style='color:#E00000'>17, 4);  SetLength(s, 
style='color:#E00000'>16);
  
style='color:blue'>// compare the supplied checksum against the real checksum for  
style='color:blue'>// the key string.  result := c = PKV_GetChecksum(s);end;

And finally, we come to the function that tests keys for validity. In the sample code, I am using conditional defines to allow me to easily exclude “key bytes” from the checking function, but you could just as easily comment them out. My advice is to only include one or two checks in a release, and to change which ones are checked for each release. Again, our example only has four “check bytes” but you should use many more.

function PKV_CheckKey(const S : String) : Integer;var  Key, kb : String;  Seed : Int64;  i : Integer;  b : Byte;begin  result := KEY_INVALID;  if not PKV_CheckKeyChecksum(S) then    exit; 
style='color:blue'>// bad checksum or wrong number of characters
  
style='color:blue'>// remove cosmetic hypens and normalize case  Key := UpperCase(StringReplace(S, 
style='color:#00A000'>'-', '', [rfReplaceAll]));
  
style='color:blue'>// test against blacklist  if Length(BL) > 
style='color:#E00000'>0 then    for i := Low(BL) to High(BL) do      if StartsStr(BL[i], Key) then      begin        result := KEY_BLACKLISTED;        exit;      end;
  
style='color:blue'>// At this point, the key is either valid or forged,  
style='color:blue'>// because a forged key can have a valid checksum.  
style='color:blue'>// We now test the "bytes" of the key to determine if it is  
style='color:blue'>// actually valid.
  
style='color:blue'>// When building your release application, use conditional defines  
style='color:blue'>// or comment out most of the byte checks!  This is the heart  
style='color:blue'>// of the partial key verification system. By not compiling in  
style='color:blue'>// each check, there is no way for someone to build a keygen that  
style='color:blue'>// will produce valid keys.  If an invalid keygen is released, you  
style='color:blue'>// simply change which byte checks are compiled in, and any serial  
style='color:blue'>// number built with the fake keygen no longer works.
  
style='color:blue'>// Note that the parameters used for PKV_GetKeyByte calls MUST  
style='color:blue'>// MATCH the values that PKV_MakeKey uses to make the key in the  
style='color:blue'>// first place!
  result := KEY_PHONY;
  
style='color:blue'>// extract the Seed from the supplied key string  if not TryStrToInt64(
style='color:#00A000'>'$' + Copy(Key, 
style='color:#E00000'>1, 8), Seed) then    exit; 
  
style='color:blue'>{$IFDEF KEY00}  kb := Copy(Key, 
style='color:#E00000'>9, 2);  b := PKV_GetKeyByte(Seed, 
style='color:#E00000'>24, 3, 
style='color:#E00000'>200);  if kb <> IntToHex(b, 
style='color:#E00000'>2) then    exit;  
style='color:blue'>{$ENDIF}
  
style='color:blue'>{$IFDEF KEY01}  kb := Copy(Key, 
style='color:#E00000'>11, 2);  b := PKV_GetKeyByte(Seed, 
style='color:#E00000'>10, 0, 
style='color:#E00000'>56);  if kb <> IntToHex(b, 
style='color:#E00000'>2) then    exit;
  
style='color:blue'>{$ENDIF}
  
style='color:blue'>{$IFDEF KEY02}  kb := Copy(Key, 
style='color:#E00000'>13, 2);  b := PKV_GetKeyByte(Seed, 
style='color:#E00000'>1, 2, 
style='color:#E00000'>91);  if kb <> IntToHex(b, 
style='color:#E00000'>2) then    exit;  
style='color:blue'>{$ENDIF} 
  
style='color:blue'>{$IFDEF KEY03}  kb := Copy(Key, 
style='color:#E00000'>15, 2);  b := PKV_GetKeyByte(Seed, 
style='color:#E00000'>7, 1, 
style='color:#E00000'>100);  if kb <> IntToHex(b, 
style='color:#E00000'>2) then    exit;  
style='color:blue'>{$ENDIF} 
  
style='color:blue'>// If we get this far, then it means the key is either good, or was made  
style='color:blue'>// with a keygen derived from "this" release.
  result := KEY_GOOD;end;

6. Making it harder for crackers

So far you have the tools you need to make a license key system that is virtually impervious to being “keygenned,” as far as valid keys go. It is still possible for a cracker to alter your executable to skip key verification, and a cracker will still be able to create a keygen that works for whatever version of your application he has. So what else is there to do?

  1. The first step I suggest is inlining the PKV_GetKeyByte, PKV_GetChecksum, PKV_CheckKeyChecksum, and PKV_CheckKey functions. Recent versions of Delphi support the inline compiler directive that forces the compiler to “unroll” the function in-place rather than actually make a function call. This results in larger code, but also gives the cracker that many more places to examine while he is dissecting your executable. It also prevents him from finding the single entry point for PKV_CheckKey and making it always return KEY_GOOD.
  2. When your application tests the stored key at startup, and when the user enters the key, only check the checksum. If your program immediately goes into its “key byte” verification code when it starts up or when the key is entered by the user, it’s just asking the cracker to watch carefully and see how it’s done! Just verify the checksum first, and give a polite error message if it is invalid (remember, this is where your customer is putting in his key that he gave you money for!). Elsewhere in your code, sprinkle the real checks into various operations. User clicks File>Save? There’s a good time to really check the key, instead of just the checksum. Perhaps set up a timer and check it a full minute after the code is entered. The possibilities are endless, and any unique ideas you come up with will make your program that much more tedious for a cracker to work on. Crackers have lots of programs to fool around with, and if yours gets to be too frustrating, they may get sloppy and release only a partially working crack, or skip your application altogether.
  3. Keep on top of cracks, phony keygens, and leaked keys. Even though it isn’t possible for a cracker to create a fully valid key based on what you compiled into your program, he can (and probably will) release a keygen that works with whatever version he downloaded. Set up a Google Alert for “Your_program keygen serial crack” and when one is released, immediately change which “key bytes” your program checks, or add the leaked key to the blacklist, and recompile. Suddenly none of the keys, keygens, and cracks released work anymore. Also, do a “silent” update of your download file when you are just recompiling for this reason. No need to announce to the cracker that their keygen suddenly doesn’t work any more.

7. Further development

This PKVS example is very simple in its implementation. There are several things that could be added to make it more powerful.

  1. Replace PKV_GetKeyByte with a more robust version. There are many hashing algorithms available that can be used as a starting point for calculating the portions of your key used to determine if it is valid. Using the sample function presented here would be a mistake as it could be reverse engineered with valid leaked keys and brute-force algorithms.
  2. Instead of using hexidecimal, use the entire alphabet. This will let you pack a lot more data into the key string without making it too long. Note that the comparisons and byte generation checks in this example will have to be re-written to work with any other base numbering system.
  3. The example implementation is a simple “serial number” system and does not tie the key to a user name or other information. However, it would be trivial to make the seed itself a checksum value of another string, such as the user’s name.
  4. Anything could be added to the key string. You could add additional values to store activation or expiration dates, for example.
  5. Your key generation system should maintain a database of issued keys. You should always test your key check function against every key you have issued. Also, keep a database of blacklisted or leaked keys, and always test your check function against them to ensure it returns the expected results.

Concluding remarks

Whatever you do, don’t focus all of your energy on your licensing system. It is important, but creating usable software that customers need is more important. A good license key system will certainly improve your sales, but you can’t hope to convert every one searching for “MyProgram Serialz” into a customer. A system like the one described here can be implemented in a day or two, but constantly trying to outsmart crackers is a never-ending battle.

===

Brandon has been running his Micro-ISV business since 1995. His flagship product is SwordSearcher Bible Software. Brandon also currently serves on the ASP Board of Directors.

One thought on “Implementing a Partial Serial Number Verification System

Comments are closed.