SoftAuth.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using Microsoft.Win32;
  2. using System;
  3. using System.IO;
  4. using System.Management;
  5. using System.Reflection;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. namespace SWRIS.Core
  9. {
  10. /// <summary>
  11. /// 软件授权类
  12. /// </summary>
  13. public class SoftAuth
  14. {
  15. /// <summary>
  16. /// 获取本计算机唯一的机器码(长度取前12个字符)
  17. /// </summary>
  18. /// <returns>字符串形式的机器码</returns>
  19. [Obfuscation(Feature = "virtualization", Exclude = false)]
  20. public static string GetMachineCode()
  21. {
  22. string unique = "";
  23. //bios名称
  24. unique += HWID.BIOS + "|";
  25. //cpu信息
  26. unique += HWID.CPU + "|";
  27. //主板信息
  28. unique += HWID.BaseBoard + "|";
  29. //获取SMBIOS的Id
  30. ManagementClass cimobject3 = new ManagementClass("Win32_ComputerSystemProduct");
  31. ManagementObjectCollection moc3 = cimobject3.GetInstances();
  32. foreach (ManagementObject mo in moc3)
  33. {
  34. unique += (string)mo.Properties["UUID"].Value;
  35. break;
  36. }
  37. SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
  38. var md5 = ByteToHexString(SHA1.ComputeHash(Encoding.Unicode.GetBytes(unique)), (char)0, 0);
  39. return md5.Substring(0, 12);
  40. }
  41. /// <summary>
  42. /// 获取本机激活码
  43. /// </summary>
  44. /// <returns></returns>
  45. [Obfuscation(Feature = "virtualization", Exclude = false)]
  46. public static string GetFinalCode()
  47. {
  48. return MD5Encrypt(GetMachineCode()).Substring(0, 16);
  49. }
  50. /// <summary>
  51. /// 判断是否已激活
  52. /// </summary>
  53. /// <returns></returns>
  54. [Obfuscation(Feature = "virtualization", Exclude = false)]
  55. public static bool IsAuthorizeSuccess()
  56. {
  57. if (GetFinalCode() == ReadRegistry())
  58. {
  59. return true;
  60. }
  61. else
  62. {
  63. return false;
  64. }
  65. }
  66. /// <summary>
  67. /// 验证激活码是否正确
  68. /// </summary>
  69. /// <param name="finalCode">用户输入的注册码</param>
  70. /// <returns></returns>
  71. [Obfuscation(Feature = "virtualization", Exclude = false)]
  72. public static bool CheckFinalCode(string finalCode)
  73. {
  74. if (GetFinalCode() == finalCode.Trim().ToUpper())
  75. {
  76. return true;
  77. }
  78. else
  79. {
  80. return false;
  81. }
  82. }
  83. /// <summary>
  84. /// 字节数据转化成16进制表示的字符串
  85. /// </summary>
  86. /// <param name="InBytes">字节数组</param>
  87. /// <param name="segment">分割符</param>
  88. /// <param name="newLineCount">每隔指定数量的时候进行换行</param>
  89. /// <returns>返回的字符串</returns>
  90. /// <example>
  91. /// </example>
  92. private static string ByteToHexString(byte[] InBytes, char segment, int newLineCount)
  93. {
  94. if (InBytes == null) return string.Empty;
  95. StringBuilder sb = new StringBuilder();
  96. long tick = 0;
  97. foreach (byte InByte in InBytes)
  98. {
  99. if (segment == 0) sb.Append(string.Format("{0:X2}", InByte));
  100. else sb.Append(string.Format("{0:X2}{1}", InByte, segment));
  101. tick++;
  102. if (newLineCount > 0 && tick >= newLineCount)
  103. {
  104. sb.Append(Environment.NewLine);
  105. tick = 0;
  106. }
  107. }
  108. if (segment != 0 && sb.Length > 1 && sb[sb.Length - 1] == segment)
  109. {
  110. sb.Remove(sb.Length - 1, 1);
  111. }
  112. return sb.ToString();
  113. }
  114. /// <summary>
  115. /// 加密数据,采用DES对称加密的方式
  116. /// </summary>
  117. /// <param name="pToEncrypt">待加密的数据</param>
  118. /// <param name="password">密钥,长度为8,英文或数字</param>
  119. /// <returns>加密后的数据</returns>
  120. [Obfuscation(Feature = "virtualization", Exclude = false)]
  121. public static string MD5Encrypt(string pToEncrypt, string password = "!234Qwer")
  122. {
  123. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  124. byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
  125. des.Key = Encoding.ASCII.GetBytes(password);
  126. des.IV = Encoding.ASCII.GetBytes(password);
  127. MemoryStream ms = new MemoryStream();
  128. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  129. cs.Write(inputByteArray, 0, inputByteArray.Length);
  130. cs.FlushFinalBlock();
  131. StringBuilder ret = new StringBuilder();
  132. foreach (byte b in ms.ToArray())
  133. {
  134. ret.AppendFormat("{0:X2}", b);
  135. }
  136. ret.ToString();
  137. return ret.ToString();
  138. }
  139. /// <summary>
  140. /// 激活码写入注册表
  141. /// </summary>
  142. /// <param name="code"></param>
  143. /// <returns></returns>
  144. [Obfuscation(Feature = "virtualization", Exclude = false)]
  145. public static bool WriteRegistry(string code)
  146. {
  147. try
  148. {
  149. RegistryKey regkeySetKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\HenDai\SoftAuthorize\Common", true);
  150. regkeySetKey.SetValue("Code", code);
  151. regkeySetKey.SetValue("Time", DateTime.Now.ToString());
  152. return true;
  153. }
  154. catch { return false; }
  155. }
  156. /// <summary>
  157. /// 读取注册表内激活码
  158. /// </summary>
  159. /// <returns></returns>
  160. [Obfuscation(Feature = "virtualization", Exclude = false)]
  161. public static string ReadRegistry()
  162. {
  163. try
  164. {
  165. RegistryKey regkey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\HenDai\SoftAuthorize\Common");
  166. return regkey?.GetValue("Code", string.Empty).ToString();
  167. }
  168. catch
  169. {
  170. return string.Empty;
  171. }
  172. }
  173. private class HWID
  174. {
  175. public static string BIOS { get { return GetWMIIdent("Win32_BIOS", "Manufacturer", "SerialNumber", "SMBIOSBIOSVersion"); } }
  176. public static string CPU { get { return GetWMIIdent("Win32_Processor", "ProcessorId"); } }
  177. public static string HDD { get { return GetWMIIdent("Win32_DiskDrive", "Model"); } }
  178. public static string BaseBoard { get { return GetWMIIdent("Win32_BaseBoard", "SerialNumber"); } }
  179. }
  180. private static string GetWMIIdent(string Class, string Property)
  181. {
  182. var ident = "";
  183. var objCol = new ManagementClass(Class).GetInstances();
  184. foreach (var obj in objCol)
  185. {
  186. if ((ident = obj.GetPropertyValue(Property) as string) != "")
  187. break;
  188. }
  189. return ident;
  190. }
  191. private static string GetWMIIdent(string Class, params string[] Propertys)
  192. {
  193. var ident = "";
  194. Array.ForEach(Propertys, prop => ident += GetWMIIdent(Class, prop) + " ");
  195. return ident;
  196. }
  197. }
  198. }