LicenseValidator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace MeterVision.Util
  11. {
  12. public class LicenseValidator
  13. {
  14. public static bool ValidateLicense(string licenseKey, string publicKey)
  15. {
  16. // 分割注册码为签名和许可证数据
  17. //string[] parts = licenseKey.Split("::");
  18. string[] parts = licenseKey.Split(new string[] { "::" }, StringSplitOptions.None);
  19. if (parts.Length != 2)
  20. {
  21. Console.WriteLine("无效的注册码格式");
  22. return false;
  23. }
  24. string signatureBase64 = parts[0];
  25. string jsonData = parts[1];
  26. // 解析许可证数据
  27. var licenseData = JsonConvert.DeserializeObject<dynamic>(jsonData);
  28. string machineCode = licenseData.MachineCode;
  29. string expirationDateStr = licenseData.ExpirationDate;
  30. // 检查硬件码是否匹配
  31. string currentMachineCode = RegistrationHelper.GenerateMachineCode();
  32. //HardwareInfo.GenerateMachineCode();
  33. if (machineCode != currentMachineCode)
  34. {
  35. Console.WriteLine("激活码不匹配");
  36. return false;
  37. }
  38. // 检查有效期
  39. DateTime expirationDate = DateTime.Parse(expirationDateStr);
  40. if (DateTime.Now > expirationDate)
  41. {
  42. Console.WriteLine("注册码已过期");
  43. return false;
  44. }
  45. // 验证签名
  46. using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
  47. {
  48. rsa.FromXmlString(publicKey);
  49. // 将签名从 Base64 字符串转换为字节数组
  50. byte[] signature = Convert.FromBase64String(signatureBase64);
  51. // 对许可证数据进行哈希
  52. byte[] dataToVerify = Encoding.UTF8.GetBytes(jsonData);
  53. // 验证签名
  54. bool isValid = rsa.VerifyData(dataToVerify, CryptoConfig.MapNameToOID("SHA256"), signature);
  55. if (!isValid)
  56. {
  57. Console.WriteLine("无效的签名");
  58. return false;
  59. }
  60. Console.WriteLine("注册码验证成功");
  61. return true;
  62. }
  63. }
  64. public static string GetPublicKeyFromResource()
  65. {
  66. // 获取当前程序集
  67. Assembly assembly = Assembly.GetExecutingAssembly();
  68. // 列出所有嵌入的资源名称
  69. foreach (string resourceName1 in assembly.GetManifestResourceNames())
  70. {
  71. Console.WriteLine(resourceName1);
  72. }
  73. // 获取资源名称
  74. string resourceName = "MeterVision.public_key.xml"; // 注意命名空间和文件名
  75. // 读取嵌入的资源文件
  76. using (Stream stream = assembly.GetManifestResourceStream(resourceName))
  77. {
  78. if (stream == null)
  79. {
  80. return null;
  81. }
  82. //throw new FileNotFoundException("无法找到私钥资源文件");
  83. using (StreamReader reader = new StreamReader(stream))
  84. {
  85. return reader.ReadToEnd();
  86. }
  87. }
  88. }
  89. //--------------------------------------------------
  90. }
  91. }