Support
Joined: 18 Jul 2005 Posts: 731
|
Posted: Tue Aug 11, 2009 9:10 am Post subject: HOWTO: Use custom machine codes with a license |
|
|
LogicNP CryptoLicensing For .Net uses a simple yet relatively foolproof way to uniquely identify machines. Each machine is identified by its computer name. To provide your own machine codes, derive your own class from CryptoLicense and override the GetLocalMachineCode method as follows:
[C#]
Code: | public class CustomCryptoLicense : CryptoLicense
{
// Override to provide custom machine code
// This example uses the CPU-ID as the machine code
public override byte[] GetLocalMachineCode()
{
byte[] ret = null;
try
{
ret= GetCPUId();
}
catch { }
// Fall back to base implementation if failed
if(ret==null || ret.Length==0)
ret = base.GetLocalMachineCode();
return ret;
}
// Gets the CPU-ID of the local machine
static internal byte[] GetCPUId()
{
ManagementClass mgmt = new ManagementClass("Win32_Processor");
ManagementObjectCollection objCol = mgmt.GetInstances();
foreach (ManagementObject obj in objCol)
{
// Only use CPU-ID from the first CPU
string cpuInfo = obj.Properties["ProcessorId"].Value.ToString();
if (cpuInfo != null && cpuInfo.Length > 0)
{
byte[] ret = Encoding.UTF8.GetBytes(cpuInfo);
return ret;
}
}
return null;
}
}
...
...
...
// Validating a license...
CustomCryptoLicense lic = new CustomCryptoLicense();
lic.ValidationKey = "<<validation key for your project>>";
lic.LicenseCode = "<<License code with custom machine code embedded>>";
// Validate license
MessageBox.Show(lic.Status.ToString());
|
[VB.Net]
Code: | Public Class CustomCryptoLicense
Inherits CryptoLicense
' Override to provide custom machine code
' This example uses the CPU-ID as the machine code
Public Overrides Function GetLocalMachineCode() As Byte()
Dim ret As Byte() = Nothing
Try
ret = GetCPUId()
Catch ex As Exception
End Try
' Fall back to base implementation if failed
If ret Is Nothing = True Or ret.Length = 0 Then
ret = MyBase.GetLocalMachineCode()
End If
Return ret
End Function
' Gets the CPU-ID of the local machine
Protected Shared Function GetCPUId() As Byte()
Dim mgmt As ManagementClass = New ManagementClass("Win32_Processor")
Dim objCol As ManagementObjectCollection = mgmt.GetInstances()
Dim obj As ManagementObject
For Each obj In objCol
' Only use CPU-ID from the first CPU
Dim cpuInfo As String = obj.Properties("ProcessorId").Value.ToString()
If (cpuInfo Is Nothing = False And cpuInfo.Length > 0) Then
Dim ret As Byte() = Encoding.UTF8.GetBytes(cpuInfo)
Return ret
End If
Next
Return Nothing
End Function
End Class
...
...
...
' Validating a license...
Dim lic As CustomCryptoLicense = New CustomCryptoLicense()
lic.ValidationKey = "<<validation key for your project>>"
lic.LicenseCode = "<<License code with custom machine code embedded>>"
' Validate license
MessageBox.Show(lic.Status.ToString()) |
Notes
When a machine-locked is being validated, it byte-compares the machine code embedded in the license with the code returned by the GetLocalMachineCode method. To have more control over this, override the CryptoLicense.IsMachineCodeValid method. This can be useful when you want to implement a somewhat lenient machine code comparison method instead of the default strict byte-comparison. For example, your machine code could consist of a combination of the CPU-ID, BIOS serial number, amount of memory, hard-disk serial number and the MAC address. You may want to allow one or more components of the machine code to change so that every small hardware change does not make the license invalid. See http://www.ssware.com/support/viewtopic.php?p=638 for sample code |
|