从硬件ID生成激活密钥

3
我将尝试实现硬件锁定许可。我在codeproject上找到了以下代码,用于生成硬件ID(机器ID)。
这段代码可以生成硬件密钥。
    using System;
    using System.Management;
    using System.Security.Cryptography;
    using System.Security;
    using System.Collections;
    using System.Text;
namespace Security
{
    /// <summary>
    /// Generates a 16 byte Unique Identification code of a computer
    /// Example: 4876-8DB5-EE85-69D3-FE52-8CF7-395D-2EA9
    /// </summary>
    public class FingerPrint  
    {
        private static string fingerPrint = string.Empty;
        public static string Value()
        {
            if (string.IsNullOrEmpty(fingerPrint))
            {
                fingerPrint = GetHash("CPU >> " + cpuId() + "\nBIOS >> " + 
            biosId() + "\nBASE >> " + baseId()
                            //+"\nDISK >> "+ diskId() + "\nVIDEO >> " + 
            videoId() +"\nMAC >> "+ macId()
                                     );
            }
            return fingerPrint;
        }
        private static string GetHash(string s)
        {
            MD5 sec = new MD5CryptoServiceProvider();
            ASCIIEncoding enc = new ASCIIEncoding();
            byte[] bt = enc.GetBytes(s);
            return GetHexString(sec.ComputeHash(bt));
        }
        private static string GetHexString(byte[] bt)
        {
            string s = string.Empty;
            for (int i = 0; i < bt.Length; i++)
            {
                byte b = bt[i];
                int n, n1, n2;
                n = (int)b;
                n1 = n & 15;
                n2 = (n >> 4) & 15;
                if (n2 > 9)
                    s += ((char)(n2 - 10 + (int)'A')).ToString();
                else
                    s += n2.ToString();
                if (n1 > 9)
                    s += ((char)(n1 - 10 + (int)'A')).ToString();
                else
                    s += n1.ToString();
                if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
            }
            return s;
        }
        #region Original Device ID Getting Code
        //Return a hardware identifier
        private static string identifier
        (string wmiClass, string wmiProperty, string wmiMustBeTrue)
        {
            string result = "";
            System.Management.ManagementClass mc = 
        new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                if (mo[wmiMustBeTrue].ToString() == "True")
                {
                    //Only get the first one
                    if (result == "")
                    {
                        try
                        {
                            result = mo[wmiProperty].ToString();
                            break;
                        }
                        catch
                        {
                        }
                    }
                }
            }
            return result;
        }
        //Return a hardware identifier
        private static string identifier(string wmiClass, string wmiProperty)
        {
            string result = "";
            System.Management.ManagementClass mc = 
        new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                //Only get the first one
                if (result == "")
                {
                    try
                    {
                        result = mo[wmiProperty].ToString();
                        break;
                    }
                    catch
                    {
                    }
                }
            }
            return result;
        }
        private static string cpuId()
        {
            //Uses first CPU identifier available in order of preference
            //Don't get all identifiers, as it is very time consuming
            string retVal = identifier("Win32_Processor", "UniqueId");
            if (retVal == "") //If no UniqueID, use ProcessorID
            {
                retVal = identifier("Win32_Processor", "ProcessorId");
                if (retVal == "") //If no ProcessorId, use Name
                {
                    retVal = identifier("Win32_Processor", "Name");
                    if (retVal == "") //If no Name, use Manufacturer
                    {
                        retVal = identifier("Win32_Processor", "Manufacturer");
                    }
                    //Add clock speed for extra security
                    retVal += identifier("Win32_Processor", "MaxClockSpeed");
                }
            }
            return retVal;
        }
        //BIOS Identifier
        private static string biosId()
        {
            return identifier("Win32_BIOS", "Manufacturer")
            + identifier("Win32_BIOS", "SMBIOSBIOSVersion")
            + identifier("Win32_BIOS", "IdentificationCode")
            + identifier("Win32_BIOS", "SerialNumber")
            + identifier("Win32_BIOS", "ReleaseDate")
            + identifier("Win32_BIOS", "Version");
        }
        //Main physical hard drive ID
        private static string diskId()
        {
            return identifier("Win32_DiskDrive", "Model")
            + identifier("Win32_DiskDrive", "Manufacturer")
            + identifier("Win32_DiskDrive", "Signature")
            + identifier("Win32_DiskDrive", "TotalHeads");
        }
        //Motherboard ID
        private static string baseId()
        {
            return identifier("Win32_BaseBoard", "Model")
            + identifier("Win32_BaseBoard", "Manufacturer")
            + identifier("Win32_BaseBoard", "Name")
            + identifier("Win32_BaseBoard", "SerialNumber");
        }
        //Primary video controller ID
        private static string videoId()
        {
            return identifier("Win32_VideoController", "DriverVersion")
            + identifier("Win32_VideoController", "Name");
        }
        //First enabled network card ID
        private static string macId()
        {
            return identifier("Win32_NetworkAdapterConfiguration", 
                "MACAddress", "IPEnabled");
        }
        #endregion
    }
    }

我的目标是实施严格的许可方案。如下图所示,我能够使用上述代码生成唯一的机器或硬件ID。
基于机器ID或密钥,我希望生成一个激活密钥,以便它是唯一的,并且只能在一台机器上使用,因为激活密钥将从该机器的机器ID生成。如何实现这一点?
以下是图片enter image description here 我希望我的疑问已经清楚。如果不清楚,请让我知道,我会更新问题并提供更多信息。

1
您可以通过发布一个问题来更新您的问题。 - undefined
2
@user2614235,你的问题可能被投票下降是因为它们缺乏你自己的调查,并且缺乏你试图解决的问题的清晰描述。我的评论旨在开玩笑,以便你可以更新你的问题,包括一个实际的问题,有人可以回答。从你的帖子中看,不可能知道你遇到了什么潜在的问题(如果有的话)。如果你有具体的问题,我们可以为你提供具体的解决方案(或者至少指出正确的方向)。 - undefined
@Matthew,你还需要什么更多的信息,请告诉我。我想我已经清楚地提到了所有的事情。如果有什么没有被理解的部分,请告诉我是哪一部分? - undefined
1
对哈希算法进行一些研究,我相信这会指引你朝着正确的方向前进。 - undefined
1个回答

4

简单的解决方案

简单的解决方案如下。当用户购买软件时,为许可证生成一个唯一的GUID。我们称之为许可证密钥。当用户安装软件时,将您的唯一硬件密钥许可证密钥拼接起来,并对结果进行哈希处理。您生成的哈希将是您正在寻找的激活代码。将该值存储在您的服务器上。如果相同的许可证密钥被用于在另一台计算机上安装软件,则会将该安装的计算出的激活代码与您在服务器上存储的代码进行比较,如果代码不匹配,则拒绝该安装。

注意事项

也就是说......按照您当前的方案设置,您将冒着让客户非常不满意的风险。例如,如果我购买了您的软件,并且我的硬盘故障了,在当前的设置下,我将无法恢复我的软件。如果您要基于硬件签名进行许可证,应尽量限制最不可能更改的功能。BIOS...好吧,也许您是安全的。但硬盘、网络卡、视频卡更容易更改。

此外,您可能希望为客户提供将许可证转移到另一台计算机的方法。方法是在卸载程序中添加一个自定义操作,以撤销该许可证的激活代码,使其不再绑定到该硬件ID。

结论

在所有这些操作中,关键是让事情对于您的客户尽可能地简单。安全性和易用性之间显然存在权衡,但您不想锁定合法的客户并冒着疏远他们的风险。

话虽如此,也有很多现有的商业选项来管理许可证。QLM还不错,如果您愿意付钱的话。在所有操作中,请考虑保护软件的成本与价值之间的平衡。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接