Click here to Skip to main content
Click here to Skip to main content
Go to top

Protecting Your Software Using Simple Serial Number/Activation Key Pair

, 12 Aug 2008
Rate this:
Please Sign up or sign in to vote.
This article guides you to simply, yet effectively, prevent your software customers from copying your product without your authorization. I admit it is not a top-security solution, and it will be easy to crack without protecting your compiled code, but it is just for conveying the idea.

act.JPG

Introduction

In this simple article, I am going to show how you can protect your software from unauthorized copying by creating a serial number/activation key pair based on the physical address (MAC) of the network adapter on the client's machine.

Getting the MAC Address and Generating the Serial Number

The first step is to get the MAC address of the client's machine. This could be achieved by using the ManagementClass class located in the System.Management assembly. We have to add a reference to that assembly to our project, and import it into SecurityManager.vb, which will be the class in which we place the GetSerial() and CheckKey() functions. These two functions will be responsible for generating the serial number from the MAC address and checking whether the key entered by the user is valid. As a first step, we define the GetSerial() function as follows:

Public Function GetSerial() As Long
    Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
    Dim mac As String = ""
    'Getting network adapters collection
    Dim moc As ManagementObjectCollection = mc.GetInstances

    'Here we iterate over available network adapters, 
    'picking the first possible one
    For Each mo As ManagementObject In moc
        If mo.Item("IPEnabled") Then
            mac = mo.Item("MacAddress").ToString
            Exit For
        End If
    Next

    mc.Dispose()

    'This is a simple function that we use to get a serial out
    'of our MAC address. Say that x is the MAC and y is the serial,
    'the function would be y += x[i] + (i * 2) where i is the index
    'of MAC address element.
    Dim sum As Long = 0
    Dim index As Integer = 1
    For Each ch As Char In mac
        If Char.IsDigit(ch) Then
            sum += sum + Integer.Parse(ch) * (index * 2)
        ElseIf Char.IsLetter(ch) Then
            Select Case ch.ToString.ToUpper
                Case "A"
                    sum += sum + 10 * (index * 2)
                Case "B"
                    sum += sum + 11 * (index * 2)
                Case "C"
                    sum += sum + 12 * (index * 2)
                Case "D"
                    sum += sum + 13 * (index * 2)
                Case "E"
                    sum += sum + 14 * (index * 2)
                Case "F"
                    sum += sum + 15 * (index * 2)
            End Select
        End If

        index += 1
    Next

    Return sum
End Function

This function will give us the unique serial number of each MAC address (not totally unique, but similar to hash function uniqueness).

Generating Activation Key from the Serial Number

The second step is to create the key generator which will generate the activation key from a given serial number. This generator will be placed in a class called KeyGenerator. This class will contain a function which will apply a simple mathematical function on the serial number to get the activation key. In this case, I will use the function f(x) = x2 + 53/x + 113 * (x/4).

Public Class KeyGenerator
    Public Function GenerateKey(ByVal serial As Long) As Long
        Dim x As Long = serial
        Return x * x + 53 / x + 113 * (x / 4)
    End Function
End Class

Back to SecurityManager.vb, we need to add one more function, which is CheckKey(). This function will take the activation key as a parameter, apply the key-generating function on the current MAC address, then compare the two keys to see whether they match or not.

Public Function CheckKey(ByVal key As Long) As Boolean
    Dim x As Long = GetSerial()
    Dim y As Long = x * x + 53 / x + 113 * (x / 4)
    Return y = key
End Function

One important note left: do not place all of these classes in your client's solution! Remember that the key-generating class is only owned by you.

Now you can use these classes to protect your software. You can also use more complicated functions to ensure more security. The key generator may look like this:

gen.JPG

If everything is alright, the user would get the following message.

msg.JPG

For more articles, please visit my blog (in Arabic only).

Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Yasser M. Jaffal
Instructor / Trainer Al-Quds University, Jerusalem
Palestinian Territory (Occupied) Palestinian Territory (Occupied)
I am holding a B.Sc in computer science from Al-Quds University, which is the only Arab university in Jerusalem. I ranked first among my colleagues with GPA of 80.2.
I have been working as teaching and research assistant at CS Dpartment, Al-Quds University since April 2006.
Currently, I am preparing to travel to Jordan to continue my graduate studies in Jordan University of Science and Technology (JUST) after being awarded a scholarship from German Academic Exchange Service (DAAD) for two years.

Comments and Discussions

 
Questionone time activation PinmemberMember 1071514823-Apr-14 15:06 
SuggestionMy vote of 4 PinmemberKyaw Zin Soe29-Jan-14 18:15 
QuestionMake activation permanent on the client computer PinmemberMember 104658007-Jan-14 3:43 
Questionhow can I make diffent serial no. for different user computer ? PinmemberMember 101867145-Aug-13 22:21 
Questionhow to implement this ? Pinmemberapank21101-Jun-13 5:01 
AnswerRe: how to implement this ? PinmemberMember 1024078519-May-14 20:11 
GeneralMy vote of 2 Pinmemberbehzad20006-May-13 5:25 
GeneralMy vote of 1 Pinmemberbehzad20006-May-13 3:58 
QuestionModification for .Net 4 [modified] PinmemberJimSharples18-Oct-12 21:52 
AnswerRe: Modification for .Net 4 Pinmemberapank21102-Jun-13 16:48 
GeneralRe: Modification for .Net 4 PinmemberJimSharples2-Jun-13 23:37 
GeneralRe: Modification for .Net 4 Pinmemberapank21103-Jun-13 20:16 
GeneralRe: Modification for .Net 4 PinmemberMember 92935403-Feb-14 4:25 
QuestionThank You PinmemberTonyS196829-Aug-12 2:58 
QuestionQuestion about Key Activation PinmemberMember 86897091-Apr-12 20:11 
GeneralMy vote of 5 Pinmemberandy(-RKO)19-Mar-12 23:43 
GeneralMy vote of 5 Pinmemberthileep201012-Jan-12 21:19 
GeneralMy vote of 5 PinmemberZizoovic20-Dec-11 2:27 
GeneralMy vote of 5 PinmemberOshtri Deka28-Nov-11 2:38 
GeneralMy vote of 5 PinmemberManfred R. Bihy9-May-11 2:42 
GeneralCode Change Pinmemberenexooone21-Sep-10 21:32 
GeneralMy vote of 5 PinmemberAwadh Abdullah13-Sep-10 23:28 
Generalwhere key will be store Pinmemberel3ashe216-Feb-10 1:18 
SuggestionRe: where key will be store PinmemberAhmad Al-Fagih24-Nov-13 23:17 
Generalneed help Pinmemberdudeman2222-Aug-09 4:49 
hi there,
 
I made a simple app on my mac using xcode.
 
How the hell do I copy protect it. I have looked everywhere and here is the closet i've got to the code.
 
I am a noob and kinda know whats going on in the code but how do I implement this into my project. I just need simple protection.
 
Any information will be much appreciated
 
cheers

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 | Mobile
Web03 | 2.8.140916.1 | Last Updated 12 Aug 2008
Article Copyright 2008 by Yasser M. Jaffal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid