MachineCoder.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Management;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. namespace Aitex.Core.RT.Key
  8. {
  9. public class MachineCoder
  10. {
  11. //步骤一: 获得CUP序列号和硬盘序列号的实现代码如下:
  12. //获得CPU的序列号
  13. bool Stupids = true;
  14. bool Cat = false;
  15. public string getCpu()
  16. {
  17. string strCpu = null;
  18. ManagementClass myCpu = new ManagementClass("win32_Processor");
  19. ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
  20. foreach( ManagementObject myObject in myCpuConnection)
  21. {
  22. strCpu = myObject.Properties["Processorid"].Value.ToString();
  23. break;
  24. }
  25. return strCpu;
  26. }
  27. //取得设备硬盘的卷标号
  28. public string GetDiskVolumeSerialNumber()
  29. {
  30. ManagementClass mc =
  31. new ManagementClass("Win32_NetworkAdapterConfiguration");
  32. ManagementObject disk =
  33. new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
  34. disk.Get();
  35. return disk.GetPropertyValue("VolumeSerialNumber").ToString();
  36. }
  37. //步骤二: 收集硬件信息生成机器码, 代码如下:
  38. //生成机器码
  39. public string CreateCode()
  40. {
  41. string temp = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
  42. string[] strid = new string[24];//
  43. for (int i = 0; i < 24; i++)//把字符赋给数组
  44. {
  45. strid[i] = temp.Substring(i, 1);
  46. }
  47. temp = "";
  48. //Random rdid = new Random();
  49. for (int i = 0; i < 24; i++)//从数组随机抽取24个字符组成新的字符生成机器三
  50. {
  51. //temp += strid[rdid.Next(0, 24)];
  52. temp += strid[i+3>=24?(i+3-24):(i+3)];
  53. }
  54. return GetMd5(temp);
  55. }
  56. //步骤三: 使用机器码生成软件注册码, 代码如下:
  57. //使用机器码生成注册码
  58. public int[] intCode = new int[127];//用于存密钥
  59. public void setIntCode()//给数组赋值个小于10的随机数
  60. {
  61. Random ra = new Random();
  62. for (int i = 1; i < intCode.Length;i++ )
  63. {
  64. intCode[i] = ra.Next(0, 9);
  65. }
  66. //for (int i = 1; i < intCode.Length; i++)
  67. //{
  68. // intCode[i] = i + 3 > 9 ? 0 : i + 3;
  69. //}
  70. }
  71. public int[] intNumber = new int[25];//用于存机器码的Ascii值
  72. public char[] Charcode = new char[25];//存储机器码字
  73. //生成注册码
  74. public string GetCode(string code)
  75. {
  76. if (code != "")
  77. {
  78. //把机器码存入数组中
  79. setIntCode();//初始化127位数组
  80. for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
  81. {
  82. Charcode[i] = Convert.ToChar(code.Substring(i - 1, 1));
  83. }//
  84. for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中.
  85. {
  86. intNumber[j] =
  87. intCode[Convert.ToInt32(Charcode[j])] +
  88. Convert.ToInt32(Charcode[j]);
  89. }
  90. string strAsciiName = null;//用于存储机器码
  91. for (int j = 1; j < intNumber.Length; j++)
  92. {
  93. //MessageBox.Show((Convert.ToChar(intNumber[j])).ToString());
  94. //判断字符ASCII值是否0-9之间
  95. if (intNumber[j] >= 48 && intNumber[j] <= 57)
  96. {
  97. strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  98. }
  99. //判断字符ASCII值是否A-Z之间
  100. else if (intNumber[j] >= 65 && intNumber[j] <= 90)
  101. {
  102. strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  103. }
  104. //判断字符ASCII值是否a-z之间
  105. else if (intNumber[j] >= 97 && intNumber[j] <= 122)
  106. {
  107. strAsciiName += Convert.ToChar(intNumber[j]).ToString();
  108. }
  109. else//判断字符ASCII值不在以上范围内
  110. {
  111. if (intNumber[j] > 122)//判断字符ASCII值是否大于z
  112. {
  113. strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
  114. }
  115. else
  116. {
  117. strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
  118. }
  119. }
  120. //label3.Text = strAsciiName;//得到注册码
  121. }
  122. return strAsciiName;
  123. }
  124. else
  125. {
  126. return "";
  127. }
  128. }
  129. //步骤四: 用户输入注册码注册软件, 演示代码如下:
  130. //注册
  131. public bool RegistIt(string currentCode,string realCode)
  132. {
  133. if (realCode != "")
  134. {
  135. if (currentCode.TrimEnd().Equals(realCode.TrimEnd()))
  136. {
  137. Microsoft.Win32.RegistryKey retkey =
  138. Microsoft.Win32.Registry.CurrentUser.
  139. OpenSubKey("software", true).CreateSubKey("StupidsCat").
  140. CreateSubKey("StupidsCat.ini").
  141. CreateSubKey(currentCode.TrimEnd());
  142. retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786");
  143. retkey = Microsoft.Win32.Registry.LocalMachine.
  144. OpenSubKey("software", true).CreateSubKey("StupidsCat").
  145. CreateSubKey("StupidsCat.ini").
  146. CreateSubKey(currentCode.TrimEnd());
  147. retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786");
  148. return Stupids;
  149. }
  150. else
  151. {
  152. return Cat;
  153. }
  154. }
  155. else { return Cat; }
  156. }
  157. public bool BoolRegist(string sn)
  158. {
  159. string[] keynames; bool flag = false;
  160. Microsoft.Win32.RegistryKey localRegKey = Microsoft.Win32.Registry.LocalMachine;
  161. Microsoft.Win32.RegistryKey userRegKey = Microsoft.Win32.Registry.CurrentUser;
  162. try
  163. {
  164. keynames = localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames();
  165. foreach (string name in keynames)
  166. {
  167. if (name == "StupidsCat")
  168. {
  169. if (localRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786")
  170. flag = true;
  171. }
  172. }
  173. keynames = userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValueNames();
  174. foreach (string name in keynames)
  175. {
  176. if (name == "StupidsCat")
  177. {
  178. if (flag && userRegKey.OpenSubKey("software\\StupidsCat\\StupidsCat.ini\\" + GetMd5(sn)).GetValue("StupidsCat").ToString() == "BBC6D58D0953F027760A046D58D52786")
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. catch
  185. {
  186. return false;
  187. }
  188. finally
  189. {
  190. localRegKey.Close();
  191. userRegKey.Close();
  192. }
  193. }
  194. public string GetMd5(object text)
  195. {
  196. string path = text.ToString();
  197. MD5CryptoServiceProvider MD5Pro = new MD5CryptoServiceProvider();
  198. Byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(text.ToString());
  199. Byte[] byteResult = MD5Pro.ComputeHash(buffer);
  200. string x = BitConverter.ToString(byteResult);
  201. string md5result = BitConverter.ToString(byteResult).Replace("-", "");
  202. return md5result;
  203. }
  204. }
  205. }