LicenseMana.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Management;
  7. using System.Reflection;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace MV485.util
  12. {
  13. //注册管理类
  14. public class LicenseMana
  15. {
  16. public static bool mIsLicensed = false; //是否已经注册
  17. public static LicenseModel mLicenseModel { get; set; }
  18. private static string _machineCode = "";
  19. //private const string LICENSE_FILE_PATH = "license.dat";
  20. private static string LICENSE_FILE_PATH = AppDomain.CurrentDomain.BaseDirectory + "\\license.dat";
  21. // 保存 licenseKey 到文件,如果文件已存在则覆盖
  22. public static void SaveLicenseKey(string licenseKey)
  23. {
  24. try
  25. {
  26. // 使用 File.WriteAllText 确保每次写入时覆盖现有内容
  27. File.WriteAllText(LICENSE_FILE_PATH, licenseKey);
  28. Console.WriteLine("许可证已成功保存。");
  29. }
  30. catch (Exception ex)
  31. {
  32. Console.WriteLine($"保存许可证时出错: {ex.Message}");
  33. }
  34. }
  35. // 加载注册信息
  36. public static bool IsLicensed()
  37. {
  38. if (File.Exists(LICENSE_FILE_PATH))
  39. {
  40. try
  41. {
  42. // 读取加密的注册信息
  43. string licenseKey = File.ReadAllText(LICENSE_FILE_PATH);
  44. // 解密注册信息
  45. //string jsonData = Decrypt(encryptedData);
  46. //// 反序列化 JSON 数据
  47. //return JsonConvert.DeserializeObject<LicenseData>(jsonData);
  48. //mLicenseModel = ValidateLicense(licenseKey);
  49. return ValidateLicense(licenseKey);
  50. }
  51. catch (Exception ex)
  52. {
  53. Console.WriteLine($"加载注册信息时出错: {ex.Message}");
  54. }
  55. }
  56. return false;
  57. }
  58. public static bool ValidateLicense(string enLicenseKey)
  59. {
  60. mLicenseModel = null;
  61. //先解密
  62. string decLicenseKey = AesEncryptionHelper.DecryptString(enLicenseKey, AesEncryptionHelper.key);
  63. string publicKey = GetPublicKeyFromResource();
  64. if (publicKey == null)
  65. {
  66. //throw new Exception("软件无法使用,请联系开发者!");
  67. return false;
  68. }
  69. // 分割注册码为签名和许可证数据
  70. //string[] parts = licenseKey.Split("::");
  71. string[] parts = decLicenseKey.Split(new string[] { "::" }, StringSplitOptions.None);
  72. if (parts.Length != 2)
  73. {
  74. Console.WriteLine("无效的注册码格式");
  75. return false;
  76. //throw new Exception("无效的注册码!");
  77. }
  78. string signatureBase64 = parts[0];
  79. string jsonData = parts[1];
  80. // 解析许可证数据
  81. var licenseData = JsonConvert.DeserializeObject<dynamic>(jsonData);
  82. string machineCode = licenseData.MachineCode;
  83. string expirationDateStr = licenseData.ExpirationDate;
  84. // 检查硬件码是否匹配
  85. string currentMachineCode = GetMachineCode();
  86. if (machineCode != currentMachineCode)
  87. {
  88. Console.WriteLine("激活码不匹配");
  89. return false;
  90. }
  91. // 检查有效期
  92. DateTime expirationDate = DateTime.Parse(expirationDateStr);
  93. if (DateTime.Now > expirationDate)
  94. {
  95. Console.WriteLine("注册码已过期");
  96. return false;
  97. }
  98. //LicenseData licenseData = new LicenseData();
  99. // 验证签名
  100. using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
  101. {
  102. rsa.FromXmlString(publicKey);
  103. // 将签名从 Base64 字符串转换为字节数组
  104. byte[] signature = Convert.FromBase64String(signatureBase64);
  105. // 对许可证数据进行哈希
  106. byte[] dataToVerify = Encoding.UTF8.GetBytes(jsonData);
  107. // 验证签名
  108. bool isValid = rsa.VerifyData(dataToVerify, CryptoConfig.MapNameToOID("SHA256"), signature);
  109. if (!isValid)
  110. {
  111. Console.WriteLine("无效的签名");
  112. return false;
  113. }
  114. Console.WriteLine("注册码验证成功");
  115. //return true;
  116. mLicenseModel = new LicenseModel
  117. {
  118. MachineCode = machineCode,
  119. ExpirationDate = expirationDate,
  120. IsPermanent = expirationDateStr.Substring(0, 4).Equals("9999"),
  121. LicenseKey = enLicenseKey
  122. };
  123. return true;
  124. }
  125. }
  126. public static string GetPublicKeyFromResource()
  127. {
  128. // 获取当前程序集
  129. Assembly assembly = Assembly.GetExecutingAssembly();
  130. // 列出所有嵌入的资源名称
  131. foreach (string resourceName1 in assembly.GetManifestResourceNames())
  132. {
  133. Console.WriteLine(resourceName1);
  134. }
  135. // 获取资源名称
  136. string resourceName = "MV485.public_key.xml"; // 注意命名空间和文件名
  137. // 读取嵌入的资源文件
  138. using (Stream stream = assembly.GetManifestResourceStream(resourceName))
  139. {
  140. if (stream == null)
  141. {
  142. return null;
  143. }
  144. //throw new FileNotFoundException("无法找到私钥资源文件");
  145. using (StreamReader reader = new StreamReader(stream))
  146. {
  147. return reader.ReadToEnd();
  148. }
  149. }
  150. }
  151. public static string GetMachineCode()
  152. {
  153. if (!string.IsNullOrWhiteSpace(_machineCode))
  154. {
  155. return _machineCode;
  156. }
  157. // 获取硬件信息
  158. //string hddSerial = GetHDDSerial();
  159. string cpuId = GetCPUId();
  160. //string macAddress = GetMACAddress();
  161. // 组合硬件信息并生成哈希
  162. using (SHA256 sha256 = SHA256.Create())
  163. {
  164. //byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(hddSerial + cpuId + macAddress));
  165. byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(cpuId));
  166. _machineCode = Convert.ToBase64String(hashBytes);
  167. return _machineCode;
  168. }
  169. }
  170. private static string GetCPUId()
  171. {
  172. try
  173. {
  174. // 查询CPU的ProcessorId
  175. using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT ProcessorId FROM Win32_Processor"))
  176. {
  177. foreach (ManagementObject cpu in searcher.Get())
  178. {
  179. if (cpu["ProcessorId"] != null)
  180. {
  181. return cpu["ProcessorId"].ToString().Trim();
  182. }
  183. }
  184. }
  185. }
  186. catch (Exception ex)
  187. {
  188. Console.WriteLine("获取CPU ID时出错: " + ex.Message);
  189. }
  190. //return "无法获取CPU ID";
  191. return "CPU_ID_ABCDEF";
  192. }
  193. //------------------------------------------------------------------
  194. }
  195. // 许可证数据类
  196. public class LicenseModel
  197. {
  198. public string MachineCode { get; set; }
  199. public DateTime ExpirationDate { get; set; }
  200. public bool IsPermanent { get; set; }
  201. public string LicenseKey { get; set; }
  202. }
  203. //----------------------------------------------------
  204. }