Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / Win32
Article

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 318.8K   18.9K   145   46
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:

VB
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).

VB
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.

VB
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)


Written By
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
Oshtri Deka28-Nov-11 2:38
professionalOshtri Deka28-Nov-11 2:38 
GeneralMy vote of 5 Pin
Manfred Rudolf Bihy9-May-11 2:42
professionalManfred Rudolf Bihy9-May-11 2:42 
GeneralCode Change Pin
enexooone21-Sep-10 21:32
enexooone21-Sep-10 21:32 
GeneralMy vote of 5 Pin
Awadh Abdullah13-Sep-10 23:28
Awadh Abdullah13-Sep-10 23:28 
Generalwhere key will be store Pin
el3ashe216-Feb-10 1:18
el3ashe216-Feb-10 1:18 
SuggestionRe: where key will be store Pin
Ahmad Al-Fagih24-Nov-13 23:17
Ahmad Al-Fagih24-Nov-13 23:17 
GeneralRe: where key will be store Pin
Member 1483781421-May-20 7:26
Member 1483781421-May-20 7:26 
Generalneed help Pin
dudeman2222-Aug-09 4:49
dudeman2222-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
GeneralMultiple NICs, such as laptops Pin
Claudio Nicora26-Aug-08 1:24
Claudio Nicora26-Aug-08 1:24 
QuestionRe: Multiple NICs, such as laptops Pin
Ahmad Al-Fagih25-Nov-13 20:23
Ahmad Al-Fagih25-Nov-13 20:23 
GeneralHexadecimal numbers Pin
TobiasP19-Aug-08 3:54
TobiasP19-Aug-08 3:54 
GeneralToo simple for any copy protection Pin
GurliGebis14-Aug-08 9:34
GurliGebis14-Aug-08 9:34 
GeneralRe: Too simple for any copy protection [modified] Pin
Yasser M. Jaffal14-Aug-08 9:55
Yasser M. Jaffal14-Aug-08 9:55 
GeneralRe: Too simple for any copy protection Pin
mr_ber18-Aug-08 20:24
mr_ber18-Aug-08 20:24 
GeneralRe: Too simple for any copy protection Pin
Yasser M. Jaffal19-Aug-08 5:00
Yasser M. Jaffal19-Aug-08 5:00 
GeneralRe: Too simple for any copy protection Pin
google198219-Jul-11 23:47
google198219-Jul-11 23:47 
GeneralVery Usefull Pin
DBSSoftware Inc. (Business)14-Aug-08 6:17
DBSSoftware Inc. (Business)14-Aug-08 6:17 

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.