/SqlServer_20110520/Soccer Score Forecast/Soccer Score Forecast/Config/LicenseCheckLib.cs

http://htmlconvertsql.googlecode.com/ · C# · 223 lines · 154 code · 16 blank · 53 comment · 6 complexity · 0ad0d2008a1d25bb81a1f3b9f6bd3e33 MD5 · raw file

  1. using System.Text;
  2. using System.Web.Security;
  3. using System.IO;
  4. using System.Security.Cryptography;
  5. using System.Management;
  6. using System;
  7. namespace Soccer_Score_Forecast
  8. {
  9. public class LincenseString
  10. {
  11. public string machineNum { get; set; }//1
  12. public string regLicense { get; set; }//2
  13. public string expTimes { get; set; }//3
  14. public string expireDate { get; set; }//4
  15. public string regDateFile { get; set; }//5
  16. public string regEmail { get; set; }//6
  17. public string regDate { get; set; }//7
  18. public string licDate { get; set; }//8
  19. public string licMachine { get; set; }//9
  20. public DateTime regDateTime { get { return DateTime.Now; } }
  21. public bool bRegOK { get; set; }
  22. }
  23. public class LicenseCheck
  24. {
  25. public string keyStr { get { return "ycsoccer"; } }
  26. public string keyfile { get; set; }
  27. public string reqfile { get; set; }
  28. public LicenseCheck()
  29. {
  30. }
  31. /// <summary>
  32. /// ????????????
  33. /// </summary>
  34. /// <param name="machineCode">???</param>
  35. /// <param name="regCode">???</param>
  36. /// <returns></returns>
  37. public bool CheckRegHead(string machineCode, string regCode)
  38. {
  39. string regHead2 = GetRegHead2(machineCode);
  40. return string.Compare(regHead2, 0, regCode, 0, 9) == 0;
  41. }
  42. /// <summary>
  43. /// ?????????????????????????????????
  44. /// </summary>
  45. /// <returns></returns>
  46. public string GetRegTail3ByMac(string seriesNum)
  47. {
  48. string machineCode = GetMachineCode(seriesNum);
  49. return GetRegTail3(machineCode);
  50. }
  51. /// <summary>
  52. /// ???????????????????
  53. /// </summary>
  54. /// <param name="machineCode"></param>
  55. /// <returns></returns>
  56. public string GetRegTail3(string machineCode)
  57. {
  58. string regTail3;
  59. regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(machineCode, "MD5");
  60. regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "SHA1");
  61. regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "SHA1");
  62. regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "MD5");
  63. regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "SHA1");
  64. regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "MD5");
  65. regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "MD5");
  66. regTail3 = GetCode(regTail3, 3);
  67. return regTail3;
  68. }
  69. /// <summary>
  70. /// ??????????????????????????????????
  71. /// </summary>
  72. /// <param name="machineCode"></param>
  73. /// <returns></returns>
  74. public string GetRegHead2(string machineCode)
  75. {
  76. string regHead2 = FormsAuthentication.HashPasswordForStoringInConfigFile(machineCode, "MD5");
  77. regHead2 = GetCode(regHead2, 2);
  78. return regHead2;
  79. }
  80. /// <summary>
  81. /// ?????
  82. /// </summary>
  83. /// <param name="???"></param>
  84. /// <returns>?“-”?????</returns>
  85. string GetMachineCode(string seriesNum)
  86. {
  87. string macAdd = GetMacAddress();
  88. string md5Str = FormsAuthentication.HashPasswordForStoringInConfigFile(macAdd + seriesNum, "MD5");
  89. return GetCode(md5Str, 4);
  90. }
  91. /// <summary>
  92. /// ???“-”?????5??????
  93. /// </summary>
  94. /// <param name="src"></param>
  95. /// <param name="num"></param>
  96. /// <returns></returns>
  97. string GetCode(string src, int num)
  98. {
  99. string re = "";
  100. for (int i = 0; i < num; i++)
  101. {
  102. string tem = src.Substring(i * num, 5);
  103. re += tem;
  104. if (i < num - 1)
  105. re += "-";
  106. }
  107. return re;
  108. }
  109. string GetMacAddress()
  110. {
  111. try
  112. {
  113. string mac = "";
  114. ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
  115. ManagementObjectCollection moc = mc.GetInstances();
  116. foreach (ManagementObject mo in moc)
  117. {
  118. if ((bool)mo["IPEnabled"] == true)
  119. {
  120. mac = mo["MacAddress"].ToString();
  121. break;
  122. }
  123. }
  124. moc = null;
  125. mc = null;
  126. return mac;
  127. }
  128. catch
  129. {
  130. return "unknow";
  131. }
  132. finally
  133. {
  134. }
  135. }
  136. /// <summary>
  137. /// ?????
  138. /// </summary>
  139. /// <param name="???"></param>
  140. /// <returns>?“-”?????</returns>
  141. public string ?????(string seriesNum)
  142. {
  143. string macAdd = GetMacAddress();
  144. string md5Str = FormsAuthentication.HashPasswordForStoringInConfigFile(macAdd + seriesNum, "MD5");
  145. return GetCode(md5Str, 4);
  146. }
  147. #region ????????DES?8?key?
  148. //????
  149. public string Encrypt(string pToEncrypt, string sKey)
  150. {
  151. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  152. //??????byte???
  153. //?????UTF8??????Unicode??????
  154. byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
  155. //byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
  156. //?????????????
  157. //????ASCIIEncoding.ASCII???GetBytes??
  158. //??????????????
  159. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  160. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  161. MemoryStream ms = new MemoryStream();
  162. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  163. //Write the byte array into the crypto stream
  164. //(It will end up in the memory stream)
  165. cs.Write(inputByteArray, 0, inputByteArray.Length);
  166. cs.FlushFinalBlock();
  167. //Get the data back from the memory stream, and into a string
  168. StringBuilder ret = new StringBuilder();
  169. foreach (byte b in ms.ToArray())
  170. {
  171. //Format as hex
  172. ret.AppendFormat("{0:X2}", b);
  173. }
  174. ret.ToString();
  175. return ret.ToString();
  176. }
  177. //????
  178. public string Decrypt(string pToDecrypt, string sKey)
  179. {
  180. try
  181. {
  182. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  183. //Put the input string into the byte array
  184. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  185. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  186. {
  187. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  188. inputByteArray[x] = (byte)i;
  189. }
  190. //???????????????????????
  191. des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  192. des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  193. MemoryStream ms = new MemoryStream();
  194. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  195. //Flush the data through the crypto stream into the memory stream
  196. cs.Write(inputByteArray, 0, inputByteArray.Length);
  197. cs.FlushFinalBlock();
  198. //Get the decrypted data back from the memory stream
  199. //??StringBuild???CreateDecrypt??????????????????????
  200. StringBuilder ret = new StringBuilder();
  201. return System.Text.Encoding.Default.GetString(ms.ToArray());
  202. }
  203. catch { return pToDecrypt; }
  204. }
  205. #endregion
  206. }
  207. }