RegistrationHelper.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace MeterVision.Util
  10. {
  11. public class RegistrationHelper
  12. {
  13. // 生成机器码
  14. public static string GenerateMachineCode()
  15. {
  16. // 获取硬件信息
  17. //string hddSerial = GetHDDSerial();
  18. string cpuId = GetCPUId();
  19. //string macAddress = GetMACAddress();
  20. // 组合硬件信息并生成哈希
  21. using (SHA256 sha256 = SHA256.Create())
  22. {
  23. //byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(hddSerial + cpuId + macAddress));
  24. byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(cpuId));
  25. return Convert.ToBase64String(hashBytes);
  26. }
  27. }
  28. // 生成注册码(使用私钥签名)
  29. //public static string GenerateRegistrationCode(string machineCode, DateTime expirationDate, RSA privateKey)
  30. //{
  31. // // 将机器码和有效期组合成明文
  32. // string plainText = $"{machineCode}|{expirationDate:yyyy-MM-dd}";
  33. // // 使用私钥对明文进行签名
  34. // byte[] dataToSign = Encoding.UTF8.GetBytes(plainText);
  35. // byte[] signature = privateKey.SignData(dataToSign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  36. // // 将签名和明文组合成注册码
  37. // return Convert.ToBase64String(signature) + "|" + plainText;
  38. //}
  39. public static bool ValidateRegistrationCode(string registrationCode, RSA publicKey, out DateTime expirationDate)
  40. {
  41. try
  42. {
  43. string[] parts = registrationCode.Split('|');
  44. byte[] signature = Convert.FromBase64String(parts[0]);
  45. string plainText = parts[1];
  46. // 验证签名
  47. byte[] dataToVerify = Encoding.UTF8.GetBytes(plainText);
  48. bool isValid = publicKey.VerifyData(dataToVerify, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  49. if (isValid)
  50. {
  51. string[] dataParts = plainText.Split('|');
  52. string machineCode = dataParts[0];
  53. expirationDate = DateTime.Parse(dataParts[1]);
  54. // 验证机器码是否匹配
  55. if (machineCode == GenerateMachineCode())
  56. {
  57. // 验证有效期
  58. if (DateTime.Now <= expirationDate)
  59. {
  60. return true;
  61. }
  62. }
  63. }
  64. }
  65. catch
  66. {
  67. // 捕获任何异常,表示验证失败
  68. }
  69. expirationDate = DateTime.MinValue;
  70. return false;
  71. }
  72. // 获取硬盘序列号
  73. private static string GetHDDSerial()
  74. {
  75. // 实现获取硬盘序列号的逻辑
  76. return "HDD_SERIAL_12345";
  77. }
  78. // 获取MAC地址
  79. private static string GetMACAddress()
  80. {
  81. // 实现获取MAC地址的逻辑
  82. return "00-1A-2B-3C-4D-5E";
  83. }
  84. public static string GetCPUId()
  85. {
  86. try
  87. {
  88. // 查询CPU的ProcessorId
  89. using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"))
  90. {
  91. foreach (ManagementObject cpu in searcher.Get())
  92. {
  93. if (cpu["ProcessorId"] != null)
  94. {
  95. return cpu["ProcessorId"].ToString().Trim();
  96. }
  97. }
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. Console.WriteLine("获取CPU ID时出错: " + ex.Message);
  103. }
  104. //return "无法获取CPU ID";
  105. return "CPU_ID_ABCDEF";
  106. }
  107. //private string GetMachineCode()
  108. //{
  109. // string cpuId = string.Empty;
  110. // using (var searcher = new ManagementObjectSearcher("SELECT ProcessorId FROM Win32_Processor"))
  111. // {
  112. // foreach (ManagementObject obj in searcher.Get())
  113. // {
  114. // if (!string.IsNullOrEmpty(cpuId)) break; // 只取第一个CPU ID
  115. // cpuId = obj["ProcessorId"]?.ToString();
  116. // }
  117. // }
  118. // string diskSerial = string.Empty;
  119. // using (var searcher = new ManagementObjectSearcher("SELECT VolumeSerialNumber FROM Win32_LogicalDisk WHERE DeviceID='C:'"))
  120. // {
  121. // foreach (ManagementObject obj in searcher.Get())
  122. // {
  123. // diskSerial = obj["VolumeSerialNumber"]?.ToString();
  124. // break; // C盘的序列号
  125. // }
  126. // }
  127. // return $"{cpuId}{diskSerial}";
  128. //}
  129. //------------------------------------------------------
  130. }
  131. }