从硬件ID生成激活密钥

时间:2014-06-03 18:13:42

标签: c# hardware-id

我正在尝试实现硬件锁定许可。我在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 个答案:

答案 0 :(得分:3)

简单的解决方案

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

The Catch

话虽如此......您目前的方案设置方式,您将面临让您的客户非常沮丧的风险。例如,如果我购买你的软件并且我的硬盘驱动器死在我身上,那么使用你当前的设置我将无法恢复我的软件。如果您要执行基于硬件签名的许可,则应尝试将其限制为最不可能更改的功能。 BIOS ...好吧也许你很安全。但硬盘,网卡,视频卡更容易改变。

此外,您可能希望为客户提供将许可证转移到另一台计算机的方法。执行此操作的方式是在卸载程序中执行自定义操作,以撤消许可证的激活码,以便它不再与该硬件ID绑定。

结论

在所有这些方面,关键是要为您的客户 尽可能简单 。显然在安全性和易用性之间存在权衡,但是您不希望锁定合法客户并冒险疏远他们。

尽管如此,还有许多现有商业广告选项用于管理许可证。 QLM非常好,你是否愿意掏钱。在所有方面,只需考虑保护软件的成本与保护软件的价值。