Click here to Skip to main content
13,345,254 members (50,841 online)
Click here to Skip to main content
Add your own
alternative version


135 bookmarked
Posted 12 Aug 2008

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.



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


    '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

    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:


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


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

Happy coding!


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


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.

You may also be interested in...


Comments and Discussions

QuestionDownload doesn't work Pin
jsarnold18-Jun-17 13:36
memberjsarnold18-Jun-17 13:36 
GeneralMy vote of 5 Pin
Member 1264943016-Mar-17 12:46
memberMember 1264943016-Mar-17 12:46 
SuggestionVirtual Machines Pin
Thomas Markas26-Sep-16 4:40
memberThomas Markas26-Sep-16 4:40 
QuestionProtecting Executable Software On USB Pin
Member 1262975211-Jul-16 14:06
memberMember 1262975211-Jul-16 14:06 
QuestionHow TO Pin
Member 1127766129-May-15 5:50
memberMember 1127766129-May-15 5:50 
QuestionHow to get Motherboard serial number in windows ? Pin
Murulimadhav15-Feb-15 20:15
memberMurulimadhav15-Feb-15 20:15 
Questionone time activation Pin
Member 1071514823-Apr-14 16:06
memberMember 1071514823-Apr-14 16:06 
SuggestionMy vote of 4 Pin
Kyaw Zin Soe29-Jan-14 19:15
memberKyaw Zin Soe29-Jan-14 19:15 
QuestionMake activation permanent on the client computer Pin
Member 104658007-Jan-14 4:43
memberMember 104658007-Jan-14 4:43 
Questionhow can I make diffent serial no. for different user computer ? Pin
Member 101867145-Aug-13 23:21
memberMember 101867145-Aug-13 23:21 
Questionhow to implement this ? Pin
apank21101-Jun-13 6:01
memberapank21101-Jun-13 6:01 
AnswerRe: how to implement this ? Pin
Member 1024078519-May-14 21:11
memberMember 1024078519-May-14 21:11 
GeneralMy vote of 2 Pin
behzad20006-May-13 6:25
memberbehzad20006-May-13 6:25 
GeneralMy vote of 1 Pin
behzad20006-May-13 4:58
memberbehzad20006-May-13 4:58 
QuestionModification for .Net 4 Pin
JimSharples18-Oct-12 22:52
memberJimSharples18-Oct-12 22:52 
AnswerRe: Modification for .Net 4 Pin
apank21102-Jun-13 17:48
memberapank21102-Jun-13 17:48 
GeneralRe: Modification for .Net 4 Pin
JimSharples3-Jun-13 0:37
memberJimSharples3-Jun-13 0:37 
GeneralRe: Modification for .Net 4 Pin
apank21103-Jun-13 21:16
memberapank21103-Jun-13 21:16 
GeneralRe: Modification for .Net 4 Pin
Member 92935403-Feb-14 5:25
memberMember 92935403-Feb-14 5:25 
AnswerRe: Modification for .Net 4 Pin
arizane16-Jun-15 17:07
memberarizane16-Jun-15 17:07 
QuestionThank You Pin
TonyS196829-Aug-12 3:58
memberTonyS196829-Aug-12 3:58 
QuestionQuestion about Key Activation Pin
Member 86897091-Apr-12 21:11
memberMember 86897091-Apr-12 21:11 
GeneralMy vote of 5 Pin
andy(-RKO)20-Mar-12 0:43
memberandy(-RKO)20-Mar-12 0:43 
GeneralMy vote of 5 Pin
thileep201012-Jan-12 22:19
memberthileep201012-Jan-12 22:19 
GeneralMy vote of 5 Pin
Zizoovic20-Dec-11 3:27
memberZizoovic20-Dec-11 3:27 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 12 Aug 2008
Article Copyright 2008 by Yasser M. Jaffal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid