gpt4 book ai didi

c# - 从硬件 ID 生成激活 key

转载 作者:行者123 更新时间:2023-11-30 22:05:53 26 4
gpt4 key购买 nike

我正在尝试实现硬件锁定许可。我从生成硬件 ID(机器 ID)的代码项目中找到了以下代码

此代码生成硬件 key

    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 或 key ,我希望生成一个激活 key ,以便它是唯一的并且只能在一台机器上使用,因为激活 key 将从该机器的 MachineID 生成如何实现?

下图为enter image description here

我希望我的疑问是清楚的。如果不是请告诉我,我会用更多信息更新问题

最佳答案

简单的解决方案

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

收获

话虽这么说……按照您当前的方案设置方式,您可能会冒着让客户非常不高兴的风险。例如,如果我购买了您的软件,但我的硬盘坏了,使用您当前的设置,我将无法恢复我的软件。如果您打算进行基于硬件签名的许可,您应该尝试将其限制在最不可能更改的功能上。 BIOS ...好吧,也许你是安全的。但是硬盘、网卡、显卡更容易发生变化。

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

结论

在所有这一切中,关键是让事情对您的客户尽可能简单。安全性和易用性之间显然存在权衡,但您不想将合法客户拒之门外并冒着疏远他们的风险。

综上所述,还有许多用于管理许可证的现有商业选择。 QLM挺好的就是你舍得掏钱。在所有方面,只需考虑保护软件的成本与保护软件的值(value)。

关于c# - 从硬件 ID 生成激活 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24022130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com