Using serial numbers is the most common way to unlock applications in the market today. Microsoft made that part of our life (us developers and isv) easier using serial keys all over their product lines. This has made final users and customer familiar with the term and how the should use them to activate their applications and that’s why we use them in our .net licensing product LicenseSpot.
One aspect to keep in mind about generating serial numbers is to keep them unique. In the function below we’re using GUIDs to generate serial numbers as we know for sure that they’re unique.
The function basically takes a guid string and just take the desired length for the key. It returns a serial number with the format: XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX.
public string GetSerialNumber() { Guid serialGuid = Guid.NewGuid(); string uniqueSerial = serialGuid.ToString("N"); string uniqueSerialLength = uniqueSerial.Substring(0, 28).ToUpper(); char[] serialArray = uniqueSerialLength.ToCharArray(); string finalSerialNumber = ""; int j= 0; for (int i = 0; i < 28; i++) { for (j = i; j < 4 + i; j++) { finalSerialNumber += serialArray[j]; } if (j == 28) { break; } else { i = (j) - 1; finalSerialNumber += "-"; } } return finalSerialNumber; }
Below the VB.NET code:
Public Function GetSerialNumber() As String Dim serialGuid As Guid = Guid.NewGuid() Dim uniqueSerial As String = serialGuid.ToString("N") Dim uniqueSerialLength As String = uniqueSerial.Substring(0, 28).ToUpper() Dim serialArray As Char() = uniqueSerialLength.ToCharArray() Dim finalSerialNumber As String = "" Dim j As Integer = 0 For i As Integer = 0 To 27 For j = i To 4 + (i - 1) finalSerialNumber += serialArray(j) Next If j = 28 Then Exit For Else i = (j) - 1 finalSerialNumber += "-" End If Next Return finalSerialNumber End Function