Click here to Skip to main content
14,979,362 members
Articles / Desktop Programming / Win32
Posted 12 Aug 2008


145 bookmarked

Protecting Your Software Using Simple Serial Number/Activation Key Pair

Rate me:
Please Sign up or sign in to vote.
4.30/5 (44 votes)
12 Aug 2008CPOL2 min read
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.

Comments and Discussions

GeneralMy vote of 5 Pin
Member 1478695030-Mar-20 3:30
MemberMember 1478695030-Mar-20 3:30 
QuestionServer CPU Serial Pin
Ali Jabbari26-Oct-18 21:57
MemberAli Jabbari26-Oct-18 21:57 
QuestionDownload doesn't work Pin
jsarnold18-Jun-17 12:36
Memberjsarnold18-Jun-17 12:36 
GeneralMy vote of 5 Pin
Member 1264943016-Mar-17 11:46
MemberMember 1264943016-Mar-17 11:46 
SuggestionVirtual Machines Pin
Thomas Markas26-Sep-16 3:40
MemberThomas Markas26-Sep-16 3:40 
QuestionProtecting Executable Software On USB Pin
Member 1262975211-Jul-16 13:06
MemberMember 1262975211-Jul-16 13:06 
QuestionHow TO Pin
Member 1127766129-May-15 4:50
MemberMember 1127766129-May-15 4:50 
AnswerRe: How TO Pin
Member 1483781421-May-20 7:29
MemberMember 1483781421-May-20 7:29 
QuestionHow to get Motherboard serial number in windows ? Pin
Murulimadhav15-Feb-15 19:15
MemberMurulimadhav15-Feb-15 19:15 
Questionone time activation Pin
Member 1071514823-Apr-14 15:06
MemberMember 1071514823-Apr-14 15:06 
SuggestionMy vote of 4 Pin
Kyaw Zin Soe29-Jan-14 18:15
MemberKyaw Zin Soe29-Jan-14 18:15 
QuestionMake activation permanent on the client computer Pin
Member 104658007-Jan-14 3:43
MemberMember 104658007-Jan-14 3:43 
Questionhow can I make diffent serial no. for different user computer ? Pin
Member 101867145-Aug-13 22:21
MemberMember 101867145-Aug-13 22:21 
Questionhow to implement this ? Pin
apank21101-Jun-13 5:01
Memberapank21101-Jun-13 5:01 
AnswerRe: how to implement this ? Pin
Member 1024078519-May-14 20:11
MemberMember 1024078519-May-14 20:11 
GeneralMy vote of 2 Pin
behzad20006-May-13 5:25
Memberbehzad20006-May-13 5:25 
GeneralMy vote of 1 Pin
behzad20006-May-13 3:58
Memberbehzad20006-May-13 3:58 
QuestionModification for .Net 4 Pin
JimSharples18-Oct-12 21:52
MemberJimSharples18-Oct-12 21:52 
AnswerRe: Modification for .Net 4 Pin
apank21102-Jun-13 16:48
Memberapank21102-Jun-13 16:48 
GeneralRe: Modification for .Net 4 Pin
JimSharples2-Jun-13 23:37
MemberJimSharples2-Jun-13 23:37 
GeneralRe: Modification for .Net 4 Pin
apank21103-Jun-13 20:16
Memberapank21103-Jun-13 20:16 
GeneralRe: Modification for .Net 4 Pin
Rangga Stephen3-Feb-14 4:25
MemberRangga Stephen3-Feb-14 4:25 
AnswerRe: Modification for .Net 4 Pin
arizane16-Jun-15 16:07
Memberarizane16-Jun-15 16:07 
QuestionThank You Pin
TonyS196829-Aug-12 2:58
MemberTonyS196829-Aug-12 2:58 
QuestionQuestion about Key Activation PinPopular
Member 86897091-Apr-12 20:11
MemberMember 86897091-Apr-12 20:11 

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.