123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Management;
- using System.Security.Cryptography;
- using System.Text;
- namespace Aitex.Core.RT.Key
- {
- public class MachineCoder
- {
- private string _systemName;
- public MachineCoder(string systemName)
- {
- _systemName = systemName;
- }
- public bool Generate(out string code, out string reason)
- {
- try
- {
- reason = "";
- code = RegistryManager.Read(RegistryManager.PathMachineCode, _systemName);
- if (!string.IsNullOrEmpty(code))
- return true;
- code = GetHash("CPU >> " + CpuId() + "\nBIOS >> " +
- BiosId() + "\nBASE >> " + BaseId()
- + "\nDISK >> " + DiskId() + "\nVIDEO >> " +
- VideoId() + "\nMAC >> " + GetMACAddress()
- );
- RegistryManager.Write(RegistryManager.PathMachineCode, _systemName, code);
- return true;
- }
- catch (UnauthorizedAccessException e)
- {
- code = "";
- reason = "没有操作注册表的权限,请用管理员权限打开";
- throw e;
- }
- catch (Exception ex)
- {
- code = "";
- reason = ex.Message;
- throw ex;
- }
- }
- private string GetHash(string s)
- {
- MD5CryptoServiceProvider MD5Pro = new MD5CryptoServiceProvider();
- Byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(s);
- Byte[] byteResult = MD5Pro.ComputeHash(buffer);
- string x = BitConverter.ToString(byteResult);
- string md5result = BitConverter.ToString(byteResult).Replace("-", "");
- return md5result;
- }
- //Primary video controller ID
- private string VideoId()
- {
- return GetManagementClassValue("Win32_VideoController", "DriverVersion")
- + GetManagementClassValue("Win32_VideoController", "Name");
- }
- private static string GetMacAndDescription()
- {
- ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=true");
- IEnumerable<ManagementObject> objects = searcher.Get().Cast<ManagementObject>();
- string mac = (from o in objects orderby o["IPConnectionMetric"] select o["MACAddress"].ToString()).FirstOrDefault();
- string description = (from o in objects orderby o["IPConnectionMetric"] select o["Description"].ToString()).FirstOrDefault();
- return mac + ";" + description;
- }
- private static string GetMacByDescription(string description)
- {
- ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=true");
- IEnumerable<ManagementObject> objects = searcher.Get().Cast<ManagementObject>();
- string mac = (from o in objects where o["Description"].ToString() == description select o["MACAddress"].ToString()).FirstOrDefault();
- return mac;
- }
- private string GetMACAddress()
- {
- ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=true");
- IEnumerable<ManagementObject> objects = searcher.Get().Cast<ManagementObject>();
- string mac = (from o in objects orderby o["IPConnectionMetric"] select o["MACAddress"].ToString()).FirstOrDefault();
- return mac;
- }
- //First enabled network card ID
- private string MacId()
- {
- //Hashtable hashtable = new Hashtable();
- ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get();
- foreach (ManagementBaseObject managementBaseObject in managementObjectCollection)
- {
- ManagementObject managementObject = (ManagementObject)managementBaseObject;
- try
- {
- if ((bool)managementObject["IPEnabled"])
- {
- return (string)managementObject["MacAddress"];
- }
- }
- catch
- {
- }
- }
- return "000";
- }
- //Motherboard ID
- private string BaseId()
- {
- return GetManagementClassValue("Win32_BaseBoard", "Model")
- + GetManagementClassValue("Win32_BaseBoard", "Manufacturer")
- + GetManagementClassValue("Win32_BaseBoard", "Name")
- + GetManagementClassValue("Win32_BaseBoard", "SerialNumber");
- }
- //BIOS Identifier
- private string BiosId()
- {
- return GetManagementClassValue("Win32_BIOS", "Manufacturer")
- + GetManagementClassValue("Win32_BIOS", "SMBIOSBIOSVersion")
- + GetManagementClassValue("Win32_BIOS", "IdentificationCode")
- + GetManagementClassValue("Win32_BIOS", "SerialNumber")
- + GetManagementClassValue("Win32_BIOS", "ReleaseDate")
- + GetManagementClassValue("Win32_BIOS", "Version");
- }
- //Main physical hard drive ID
- private string DiskId()
- {
- return GetManagementClassValue("Win32_DiskDrive", "Model")
- + GetManagementClassValue("Win32_DiskDrive", "Manufacturer")
- + GetManagementClassValue("Win32_DiskDrive", "Signature")
- + GetManagementClassValue("Win32_DiskDrive", "TotalHeads");
- }
- private string CpuId()
- {
- //Uses first CPU identifier available in order of preference
- //Don't get all identifiers, as it is very time consuming
- string retVal = GetManagementClassValue("Win32_Processor", "UniqueId");
- if (retVal == "") //If no UniqueID, use ProcessorID
- {
- retVal = GetManagementClassValue("Win32_Processor", "ProcessorId");
- if (retVal == "") //If no ProcessorId, use Name
- {
- retVal = GetManagementClassValue("Win32_Processor", "Name");
- if (retVal == "") //If no Name, use Manufacturer
- {
- retVal = GetManagementClassValue("Win32_Processor", "Manufacturer");
- }
- //Add clock speed for extra security
- retVal += GetManagementClassValue("Win32_Processor", "MaxClockSpeed");
- }
- }
- return retVal;
- }
- //步骤一: 获得CUP序列号和硬盘序列号的实现代码如下:
- //获得CPU的序列号
- bool Stupids = true;
- bool Cat = false;
- private string getCpu()
- {
- string strCpu = null;
- ManagementClass myCpu = new ManagementClass("win32_Processor");
- ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
- foreach (ManagementObject myObject in myCpuConnection)
- {
- strCpu = myObject.Properties["Processorid"].Value.ToString();
- break;
- }
- return strCpu;
- }
- //取得设备硬盘的卷标号
- private string GetDiskVolumeSerialNumber()
- {
- ManagementClass mc =
- new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObject disk =
- new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
- disk.Get();
- return disk.GetPropertyValue("VolumeSerialNumber").ToString();
- }
- //步骤二: 收集硬件信息生成机器码, 代码如下:
- //生成机器码
- //private string CreateCode()
- //{
- // string temp = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
- // string[] strid = new string[24];//
- // for (int i = 0; i < 24; i++)//把字符赋给数组
- // {
- // strid[i] = temp.Substring(i, 1);
- // }
- // temp = "";
- // //Random rdid = new Random();
- // for (int i = 0; i < 24; i++)//从数组随机抽取24个字符组成新的字符生成机器三
- // {
- // //temp += strid[rdid.Next(0, 24)];
- // temp += strid[i + 3 >= 24 ? (i + 3 - 24) : (i + 3)];
- // }
- // return GetMd5(temp);
- //}
- //步骤三: 使用机器码生成软件注册码, 代码如下:
- //使用机器码生成注册码
- private int[] intCode = new int[127];//用于存密钥
- private void setIntCode()//给数组赋值个小于10的随机数
- {
- Random ra = new Random();
- for (int i = 1; i < intCode.Length; i++)
- {
- intCode[i] = ra.Next(0, 9);
- }
- //for (int i = 1; i < intCode.Length; i++)
- //{
- // intCode[i] = i + 3 > 9 ? 0 : i + 3;
- //}
- }
- private int[] intNumber = new int[25];//用于存机器码的Ascii值
- private char[] Charcode = new char[25];//存储机器码字
- //生成注册码
- private string GetCode(string code)
- {
- if (code != "")
- {
- //把机器码存入数组中
- setIntCode();//初始化127位数组
- for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
- {
- Charcode[i] = Convert.ToChar(code.Substring(i - 1, 1));
- }//
- for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。
- {
- intNumber[j] =
- intCode[Convert.ToInt32(Charcode[j])] +
- Convert.ToInt32(Charcode[j]);
- }
- string strAsciiName = null;//用于存储机器码
- for (int j = 1; j < intNumber.Length; j++)
- {
- //MessageBox.Show((Convert.ToChar(intNumber[j])).ToString());
- //判断字符ASCII值是否0-9之间
- if (intNumber[j] >= 48 && intNumber[j] <= 57)
- {
- strAsciiName += Convert.ToChar(intNumber[j]).ToString();
- }
- //判断字符ASCII值是否A-Z之间
- else if (intNumber[j] >= 65 && intNumber[j] <= 90)
- {
- strAsciiName += Convert.ToChar(intNumber[j]).ToString();
- }
- //判断字符ASCII值是否a-z之间
- else if (intNumber[j] >= 97 && intNumber[j] <= 122)
- {
- strAsciiName += Convert.ToChar(intNumber[j]).ToString();
- }
- else//判断字符ASCII值不在以上范围内
- {
- if (intNumber[j] > 122)//判断字符ASCII值是否大于z
- {
- strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
- }
- else
- {
- strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
- }
- }
- //label3.Text = strAsciiName;//得到注册码
- }
- return strAsciiName;
- }
- else
- {
- return "";
- }
- }
- //步骤四: 用户输入注册码注册软件, 演示代码如下:
- //注册
- private bool RegistIt(string currentCode, string realCode)
- {
- if (realCode != "")
- {
- if (currentCode.TrimEnd().Equals(realCode.TrimEnd()))
- {
- Microsoft.Win32.RegistryKey retkey =
- Microsoft.Win32.Registry.CurrentUser.
- OpenSubKey("software", true).CreateSubKey("StupidsCat").
- CreateSubKey("StupidsCat.ini").
- CreateSubKey(currentCode.TrimEnd());
- retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786");
- retkey = Microsoft.Win32.Registry.LocalMachine.
- OpenSubKey("software", true).CreateSubKey("StupidsCat").
- CreateSubKey("StupidsCat.ini").
- CreateSubKey(currentCode.TrimEnd());
- retkey.SetValue("StupidsCat", "BBC6D58D0953F027760A046D58D52786");
- return Stupids;
- }
- else
- {
- return Cat;
- }
- }
- else { return Cat; }
- }
- //Return a hardware identifier
- private string GetManagementClassValue(string wmiClass, string wmiProperty)
- {
- string result = "";
- ManagementClass mc = new ManagementClass(wmiClass);
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- //Only get the first one
- if (result == "")
- {
- try
- {
- result = mo[wmiProperty].ToString();
- break;
- }
- catch
- {
- }
- }
- }
- return result;
- }
- }
- }
|