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 = ""
Dim moc As ManagementObjectCollection = mc.GetInstances
For Each mo As ManagementObject In moc
If mo.Item("IPEnabled") Then
mac = mo.Item("MacAddress").ToString
Exit For
End If
Next
mc.Dispose()
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:
If everything is alright, the user would get the following message.
For more articles, please visit my blog (in Arabic only).
Happy coding!