/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
- using System.Text;
- using System.Web.Security;
- using System.IO;
- using System.Security.Cryptography;
- using System.Management;
- using System;
-
-
- namespace Soccer_Score_Forecast
- {
- public class LincenseString
- {
- public string machineNum { get; set; }//1
- public string regLicense { get; set; }//2
- public string expTimes { get; set; }//3
- public string expireDate { get; set; }//4
- public string regDateFile { get; set; }//5
- public string regEmail { get; set; }//6
- public string regDate { get; set; }//7
- public string licDate { get; set; }//8
- public string licMachine { get; set; }//9
-
- public DateTime regDateTime { get { return DateTime.Now; } }
- public bool bRegOK { get; set; }
- }
- public class LicenseCheck
- {
- public string keyStr { get { return "ycsoccer"; } }
- public string keyfile { get; set; }
- public string reqfile { get; set; }
- public LicenseCheck()
- {
- }
- /// <summary>
- /// ????????????
- /// </summary>
- /// <param name="machineCode">???</param>
- /// <param name="regCode">???</param>
- /// <returns></returns>
- public bool CheckRegHead(string machineCode, string regCode)
- {
- string regHead2 = GetRegHead2(machineCode);
- return string.Compare(regHead2, 0, regCode, 0, 9) == 0;
- }
-
- /// <summary>
- /// ?????????????????????????????????
- /// </summary>
- /// <returns></returns>
- public string GetRegTail3ByMac(string seriesNum)
- {
- string machineCode = GetMachineCode(seriesNum);
- return GetRegTail3(machineCode);
- }
-
- /// <summary>
- /// ???????????????????
- /// </summary>
- /// <param name="machineCode"></param>
- /// <returns></returns>
- public string GetRegTail3(string machineCode)
- {
- string regTail3;
- regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(machineCode, "MD5");
- regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "SHA1");
- regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "SHA1");
- regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "MD5");
- regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "SHA1");
- regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "MD5");
- regTail3 = FormsAuthentication.HashPasswordForStoringInConfigFile(regTail3, "MD5");
-
- regTail3 = GetCode(regTail3, 3);
- return regTail3;
- }
-
- /// <summary>
- /// ??????????????????????????????????
- /// </summary>
- /// <param name="machineCode"></param>
- /// <returns></returns>
- public string GetRegHead2(string machineCode)
- {
- string regHead2 = FormsAuthentication.HashPasswordForStoringInConfigFile(machineCode, "MD5");
- regHead2 = GetCode(regHead2, 2);
- return regHead2;
- }
-
- /// <summary>
- /// ?????
- /// </summary>
- /// <param name="???"></param>
- /// <returns>?-?????</returns>
- string GetMachineCode(string seriesNum)
- {
- string macAdd = GetMacAddress();
- string md5Str = FormsAuthentication.HashPasswordForStoringInConfigFile(macAdd + seriesNum, "MD5");
- return GetCode(md5Str, 4);
- }
-
- /// <summary>
- /// ???-?????5??????
- /// </summary>
- /// <param name="src"></param>
- /// <param name="num"></param>
- /// <returns></returns>
- string GetCode(string src, int num)
- {
- string re = "";
- for (int i = 0; i < num; i++)
- {
- string tem = src.Substring(i * num, 5);
- re += tem;
- if (i < num - 1)
- re += "-";
- }
- return re;
- }
- string GetMacAddress()
- {
- try
- {
- string mac = "";
- ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- if ((bool)mo["IPEnabled"] == true)
- {
- mac = mo["MacAddress"].ToString();
- break;
- }
- }
- moc = null;
- mc = null;
- return mac;
- }
- catch
- {
- return "unknow";
- }
- finally
- {
- }
- }
-
-
- /// <summary>
- /// ?????
- /// </summary>
- /// <param name="???"></param>
- /// <returns>?-?????</returns>
- public string ?????(string seriesNum)
- {
- string macAdd = GetMacAddress();
- string md5Str = FormsAuthentication.HashPasswordForStoringInConfigFile(macAdd + seriesNum, "MD5");
- return GetCode(md5Str, 4);
- }
- #region ????????DES?8?key?
- //????
- public string Encrypt(string pToEncrypt, string sKey)
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- //??????byte???
- //?????UTF8??????Unicode??????
- byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
- //byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);
-
- //?????????????
- //????ASCIIEncoding.ASCII???GetBytes??
- //??????????????
- des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
- des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
- //Write the byte array into the crypto stream
- //(It will end up in the memory stream)
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- //Get the data back from the memory stream, and into a string
- StringBuilder ret = new StringBuilder();
- foreach (byte b in ms.ToArray())
- {
- //Format as hex
- ret.AppendFormat("{0:X2}", b);
- }
- ret.ToString();
- return ret.ToString();
- }
- //????
- public string Decrypt(string pToDecrypt, string sKey)
- {
- try
- {
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
-
- //Put the input string into the byte array
- byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
- for (int x = 0; x < pToDecrypt.Length / 2; x++)
- {
- int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
- inputByteArray[x] = (byte)i;
- }
-
- //???????????????????????
- des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
- des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
- MemoryStream ms = new MemoryStream();
- CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
- //Flush the data through the crypto stream into the memory stream
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
-
- //Get the decrypted data back from the memory stream
- //??StringBuild???CreateDecrypt??????????????????????
- StringBuilder ret = new StringBuilder();
-
- return System.Text.Encoding.Default.GetString(ms.ToArray());
- }
- catch { return pToDecrypt; }
- }
- #endregion
- }
- }