65.9K
CodeProject 正在变化。 阅读更多。
Home

使用简单的序列号/激活码对保护您的软件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.30/5 (41投票s)

2008年8月12日

CPOL

2分钟阅读

viewsIcon

329330

downloadIcon

18965

本文引导您简单而有效地阻止您的软件客户在未经您授权的情况下复制您的产品。我承认这不是一个顶级的安全解决方案,并且在没有保护您的编译代码的情况下很容易被破解,但这只是为了传达这个想法。

act.JPG

引言

在这篇简单的文章中,我将展示如何通过创建一个基于客户端机器上网络适配器的物理地址 (MAC) 的序列号/激活码对来保护您的软件免受未经授权的复制。

获取 MAC 地址并生成序列号

第一步是获取客户端机器的 MAC 地址。这可以通过使用位于 `System.Management` 程序集中的 `ManagementClass` 类来实现。我们必须将对该程序集的引用添加到我们的项目中,并将其导入到 SecurityManager.vb 中,这将是放置 `GetSerial()` 和 `CheckKey()` 函数的类。这两个函数将负责从 MAC 地址生成序列号并检查用户输入的密钥是否有效。作为第一步,我们定义 `GetSerial()` 函数如下

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

此函数将为每个 MAC 地址提供唯一的序列号(不是完全唯一,但类似于哈希函数的唯一性)。

从序列号生成激活码

第二步是创建密钥生成器,它将从给定的序列号生成激活码。这个生成器将放在一个名为 `KeyGenerator` 的类中。这个类将包含一个函数,该函数将对序列号应用一个简单的数学函数以获得激活码。在这种情况下,我将使用函数 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

回到 SecurityManager.vb,我们需要再添加一个函数,即 `CheckKey()`。这个函数将以激活码作为参数,对当前的 MAC 地址应用密钥生成函数,然后比较这两个密钥以查看它们是否匹配。

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

还有一个重要的注意事项:不要将所有这些类都放在您的客户端解决方案中!请记住,密钥生成类仅由您拥有。

现在,您可以使用这些类来保护您的软件。您还可以使用更复杂的函数来确保更高的安全性。密钥生成器可能看起来像这样

gen.JPG

如果一切正常,用户将收到以下消息。

msg.JPG

如需更多文章,请访问 我的博客(仅限阿拉伯语)。

祝您编码愉快!

© . All rights reserved.