LockLicenseHandle.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:LockLicenseHandle.cs
  5. * 2.功能描述:License文件管理。
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 张国印 2014/09/16 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Data;
  12. using System.IO;
  13. using System.Management;
  14. using System.Runtime.Serialization.Formatters.Binary;
  15. using System.Security.Cryptography;
  16. using System.Text;
  17. using Dongke.IBOSS.PRD.Basics.Library;
  18. namespace Dongke.IBOSS.PRD.Service.DKIBOSSPRDLogic
  19. {
  20. /// <summary>
  21. /// License文件管理
  22. /// </summary>
  23. public class LockLicenseHandle
  24. {
  25. //private const uint ELEP_SUCCESS = 0x80000000; // 成功
  26. //private const uint ELEP_NO_MORE_DEVICE = 0x80000009; // 没有符合条件的设备
  27. //private const uint ELEP_ERR_INVALID_PASSWORD = 0x80010002; // 有锁,但PIN密码错
  28. //private const uint ELEP_INVALID_DEVICE_HANDLE = 0x80000004; // 传入的设备句柄无效
  29. //private const uint ELEP_ERROR_HW_ERROR = 0x80010001; // 硬件错误,可能由硬件损坏引起。
  30. //private const uint ELEP_ERR_INVALID_STATE = 0x80010009; // 无效状态,升级失败将会将设备置为无 效状态,升级成功可以解除无效状态。
  31. //private const uint ELEP_ERR_NO_PRIVILEGE = 0x80010006; // 无开发商权限。
  32. //private const uint ELEP_NOT_USER_DEVICE = 0x80000026; //设备不是用户设备。
  33. //private const uint ELEP_INVALID_PARAMETER = 0x80000001; //参数错误,可能因为传入了空指针。
  34. //private const uint ELEP_ERR_OPT = 0x80010003; //用户信息区已经写过一次。
  35. //private const uint ELEP_ERR_INVALID_ADDR_OR_SIZE = 0x80010004; //读写偏移量或长度错误。
  36. //private const ushort CUSTOMER_CODE_LENGTH = 100; // 写入锁内最长的客户编码字节数
  37. //private const ushort CUSTOMER_NAME_LENGTH = 600; // 写入锁内最长的客户名称字节数
  38. //private const ushort CUSTOMER_START_POINT = 1152; // 开始写锁区域
  39. //private const uint OLD_PIN_1 = 0x30303030;
  40. //private const uint OLD_PIN_2 = 0x30303030;
  41. //private const uint OLD_PIN_3 = 0x30303030;
  42. //private const uint OLD_PIN_4 = 0x30303030;
  43. private static string _uuid = null;
  44. /// <summary>
  45. /// 读取License文件内容
  46. /// </summary>
  47. /// <param name="lisenceFilePath">license文件路径</param>
  48. /// <param name="customer">读取的license内容</param>
  49. /// <returns>
  50. /// 0:读取成功
  51. /// -1:license文件不存在
  52. /// -2:license文件错误
  53. /// -3:license过期
  54. /// </returns>
  55. public static int ReadLisenceFile(string listCode, ref DataSet customer, out string licString)
  56. {
  57. licString = null;
  58. try
  59. {
  60. string sid = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "SID").ToUpper();
  61. string path = AppDomain.CurrentDomain.BaseDirectory + "LIC\\LIC_" + listCode + "_" + sid + ".lic";
  62. if (!File.Exists(path))
  63. {
  64. return -1;
  65. }
  66. string[] lics = File.ReadAllLines(path, Encoding.UTF32);
  67. string l1 = Encryption.DecryptDES(lics[0], "DK#LIC#CODE#SID");
  68. string[] keys = l1.Split('#');
  69. if (listCode != keys[1] || sid != keys[2].ToUpper())
  70. {
  71. return -2;
  72. }
  73. //licString = lics[1];
  74. string key = Encryption.DecryptDES(lics[1], "DK#CODE#" + keys[1].ToLower());
  75. string iv = Encryption.DecryptDES(lics[2], "DK#SID#" + keys[2].ToLower());
  76. string lic = Encryption.DecryptDES(lics[3], "DK#" + l1.ToUpper());
  77. MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(lic));
  78. memoryStream.Position = 0;
  79. BinaryFormatter formatter = new BinaryFormatter();
  80. Rijndael age = Rijndael.Create();
  81. CryptoStream cStream = new CryptoStream(memoryStream,
  82. age.CreateDecryptor(Convert.FromBase64String(key), Convert.FromBase64String(iv)),
  83. CryptoStreamMode.Read);
  84. DataSet data = (DataSet)formatter.Deserialize(cStream);
  85. if (listCode != data.Tables["Info"].Rows[0]["ListCode"]+"" ||
  86. sid != data.Tables["Info"].Rows[0]["SID"].ToString().ToUpper())
  87. {
  88. return -2;
  89. }
  90. if (data.Tables["UUID"].Rows.Count > 0)
  91. {
  92. string uuid = Encryption.EncryptDES(UUID + "_" + sid, sid);
  93. DataRow[] drs = data.Tables["UUID"].Select("UUID = '" + uuid + "'");
  94. if (drs == null || drs.Length == 0)
  95. {
  96. return -2;
  97. }
  98. }
  99. DateTime vBegin = Convert.ToDateTime(data.Tables["Info"].Rows[0]["ValidityBegin"]);
  100. DateTime vEnd = Convert.ToDateTime(data.Tables["Info"].Rows[0]["ValidityEnd"]);
  101. DateTime n = DateTime.Now.Date;
  102. if (n < vBegin || n > vEnd)
  103. {
  104. return -3;
  105. }
  106. licString = Convert.ToDateTime(data.Tables["Info"].Rows[0]["LicCreateTime"]).Ticks.ToString();
  107. customer = data;
  108. return 0;
  109. }
  110. catch (Exception)
  111. {
  112. return -2;
  113. }
  114. }
  115. public static string UUID
  116. {
  117. get
  118. {
  119. if (_uuid != null)
  120. {
  121. return _uuid;
  122. }
  123. try
  124. {
  125. string code = null;
  126. SelectQuery query = new SelectQuery("select * from Win32_ComputerSystemProduct");
  127. using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
  128. {
  129. foreach (var item in searcher.Get())
  130. {
  131. using (item)
  132. {
  133. code = item["UUID"].ToString();
  134. }
  135. }
  136. }
  137. _uuid = code;
  138. }
  139. catch
  140. {
  141. _uuid = "";
  142. }
  143. return _uuid;
  144. }
  145. }
  146. }
  147. }