SoftAuth.cs 6.8 KB

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