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

提供软件解锁密钥的方法

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.76/5 (25投票s)

2009年2月23日

CPOL

4分钟阅读

viewsIcon

89554

downloadIcon

1494

这是一个非常简化的方法,可以向您的客户销售相对安全的解锁密钥。您提供的解锁密钥将仅解锁该特定客户下载的特定功能集或产品的实例。

引言

这是我发布到CodeProject的第一篇文章。

我需要一种快速的方法,可以在客户完成免费试用期后,或者在下载时购买,从我的网站上销售一个小型、廉价的WinForms应用程序。我想为客户提供以下场景:

  • 客户访问我的网站并下载安装包,作为免费的30天试用版(默认情况下,软件以试用模式运行,除非购买并输入了解锁密钥)。
  • 客户决定是否购买。
  • 如果客户决定购买,他们就会从我的网站购买一个解锁密钥,并向我提供他们软件副本的序列号(或者他们下载新副本并一次性购买密钥)。
  • 客户将通过电子邮件收到解锁密钥,该密钥将解锁以下内容:
    • 他们下载的特定软件副本(基于序列号)。
    • 他们购买的软件(基于他们的姓名)。
    • 我公司提供的软件(基于我公司的名称)。
    • 他们购买的软件功能集(基于他们购买的内容)。
  • 所有以上条件必须为真,电子邮件中的密钥才能真正解锁软件。
  • 用户现在拥有一个已解锁的软件和一个无法解锁我其他任何产品的密钥。他们可以自由地在多个机器上重新安装该软件,但该密钥只能解锁他们下载的副本。

背景

这是我们都熟悉的购买软件的一种非常常见的方式。这里介绍的方法的安全性取决于您保护公司加密密钥的能力。请参阅Microsoft的其他文章,了解如何在部署的应用程序中混淆加密密钥。99.99%的人没有时间去破解解锁密钥。在剩余的0.01%中,有多少人会向成千上万的人免费提供破解的密钥?此外,由于此方法对以下所有项目进行哈希处理,任何黑客仍需要知道这些信息才能为其他人的副本创建密钥。另外,您的软件公司可以通过更改哈希中的一个项目,轻松使市场上所有浮动的密钥无效。最有可能的是,会是产品功能。以下项目被哈希处理到解锁密钥中:

  • 公司名称
  • 客户姓名
  • 下载软件的序列号
  • 产品或产品功能代码

通过扩展类,您可以哈希更多信息,例如机器的MAC地址,如果您想将解锁限制为特定计算机。

使用代码

从演示程序中可以看到,代码非常简单,一旦打开解决方案文件,就一目了然。以下代码部署到您的网站上以创建密钥。注意btnSell_Click仅部署到您的网站,而不是您销售的产品。这是您的网站在客户为所需产品付款后创建解锁密钥所需复制代码。

为了给客户一个密钥,他们必须知道两件事:

  1. 他们的姓名。
  2. 他们想购买的功能集或产品。

您的网站必须知道这两件事:

  1. 客户刚刚下载的软件副本的序列号(可选;如果您不对下载进行序列号标记,则将其留空,但密钥的安全性会降低;否则,创建序列号并将其下载到客户的配置文件或DLL本身。)提示:environment.tickcount是一个不错的序列号。
  2. 您的公司名称(当然,您的网站代码知道这一点)。

在您创建密钥后,将其通过电子邮件发送给客户,以便他们使用该密钥解锁他们刚刚下载的软件。

Private Sub btnSell_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnSell.Click
    Try
        ' Create the key making/selling object
        Dim locKeyHandler As OMBKeySell.KeySeller = _
                          New OMBKeySell.KeySeller(_MyCompanyKey, _MyCompanyIV)
        ' Make the key which can only unlock a specific customer name,
        ' for a specific downloaded serial no, for a specific product,
        ' for your specific company name (key is a hash of all these taken together)
        txtSellKey.Text = locKeyHandler.SellKey(txtSellCompanyName.Text, _
                          txtSellSerial.Text, txtSellCustomerName.Text, _
                          CType(btnSell.Tag, Key.ProductOrProductFeatureSet))
        ' In real use the key would be emailed at this point
        ' and not simply placed in the clipboard
        My.Computer.Clipboard.SetText(txtSellKey.Text, TextDataFormat.Text)
        ' Thank the customer and inform them of what they just bought
        lblSellStatus.Text = "Thank you for purchasing our software, " & _ 
                             "your unlock key has been placed in your computer " & _ 
                             "clipboard, paste it below to activate your copy " & _ 
                             "of the software." & vbCrLf & vbCrLf & _
                             "(In real use the key would be emailed)"
        lblSellFeature.Text = "You just bought a key to unlock this " & _ 
                              "feature or product: " & CType(btnSell.Tag, _
                              Key.ProductOrProductFeatureSet).ToString
        ' Clear buy side of the demo form
        lblBuyStatus.Text = ""
        lblBuyFeature.Text = ""
        txtBuyCustomerName.Text = ""
        txtBuyKey.Text = ""
    Catch ex As Exception
        lblSellStatus.Text = ex.Message
        lblSellFeature.Text = ""
        txtSellKey.Text = ""
        txtSellCustomerName.Text = ""
    End Try
End Sub

以下代码部署到您的产品中,用于验证密钥并为您的程序提供所购买的产品或产品功能。注意btnBuy_Click仅部署在您销售的产品中,而不是在您的网站上。这是您购买的软件在产品解锁表单上需要复制代码。

客户要解锁,他们必须知道两件事:

  1. 他们购买时使用的姓名。
  2. 发送给他们的密钥。

您部署的软件必须知道这两件事(嵌入在软件或配置文件中):

  1. 您的公司名称(与您创建和销售解锁密钥时使用的网站代码名称相同,嵌入在下载包的代码或配置文件中)。
  2. 客户刚刚下载的软件副本的序列号(嵌入在软件或下载的配置文件中)。

在处理完他们的密钥后,将提供的产品功能代码放入注册表。现在,您的程序将查看该注册表项以确定如何/是否运行自身。

Private Sub btnBuy_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnBuy.Click
    Try
        ' Create the key consuming/buying object
        Dim locKeyHandler As OMBKeyBuy.KeyBuyer = _
            New OMBKeyBuy.KeyBuyer(_MyCompanyKey, _MyCompanyIV)
        ' Validate the key, if its valid then the feature set it unlocks
        ' will be returned, if invalid then various errors are thrown
        Dim locFeatureBeingBought As Key.ProductOrProductFeatureSet = _
            locKeyHandler.BuyKey(txtBuyCompanyName.Text, _
            txtBuySerial.Text, txtBuyCustomerName.Text, txtBuyKey.Text)
        ' At this point your deployed software should place the feature
        ' set code in the registry, your program then checks the registry
        ' for this enumerator to determine how/if it should run (dont place
        ' the key string itself in the registry
        ' as that should only live in the customers hands)
        '    registrykey = locFeatureBeingBought
        ' Thank the customer again and tell them
        ' what feature or product their key just unlocked
        lblBuyStatus.Text = "Thank you for purchasing our software, " & _ 
                            "your specific copy of the software is now unlocked"
        lblBuyFeature.Text = "You unlocked this feature or product: " & _
                             locFeatureBeingBought.ToString
        ' Clear sell side of the demo form
        lblSellStatus.Text = ""
        lblSellFeature.Text = ""
        txtSellCustomerName.Text = ""
        txtSellKey.Text = ""
    Catch ex As Exception
        lblBuyStatus.Text = ex.Message
        lblBuyFeature.Text = ""
        txtBuyKey.Text = ""
        txtBuyCustomerName.Text = ""
    End Try
End Sub

演示程序(在zip文件中)

要使用演示程序...在上半部分创建一个密钥,输入您的姓名。这模拟了您在网站上的购买。然后,要模拟解锁您的软件,请输入您的姓名并粘贴密钥。将返回解锁的功能。

screenshot.jpg

关注点

我很难在互联网上找到适合我目的且不过于复杂的解锁密钥代码示例。

历史

  • 首次发布。
© . All rights reserved.