Esempio n. 1
0
        /// <summary>
        /// 获取本计算机唯一的机器码
        /// </summary>
        /// <returns>字符串形式的机器码</returns>
        public static string GetInfo()
        {
            //说明 这个方法所有的获取hwid的行为均在Ring3模式下获取 方法前半部分为WMI 后半部分为S.M.A.R.T.(DeviceIoControl())
            //程序依赖kernel32.dll不能运行在wince下 纯c#解决方案几乎不可能在Ring0模式下获取 如果有更高的要求建议加密狗
            string unique = "";

            //bios名称
            unique += HWID.BIOS + "|";
            //cpu信息
            unique += HWID.CPU + "|";
            //硬盘信息
            unique += HWID.HDD + "|";
            //主板信息
            unique += HWID.BaseBoard + "|";
            //mac信息 群主建议取消mac计算 这里建议放弃 mac太玄学了
            //unique += HWID.MAC + "|";
            //是否存在scsi
            if (HWID.IsServer)
            {
                unique += HWID.SCSI + "|";
            }

            //获取系统盘ID 新增 较为稳定
            string           systempath        = Environment.GetEnvironmentVariable("systemdrive");//获取当前系统盘
            string           win32_logicaldisk = "\"" + systempath + "\"";
            ManagementObject dsk = new ManagementObject(@"win32_logicaldisk.deviceid=" + win32_logicaldisk);

            dsk.Get();
            unique += dsk["VolumeSerialNumber"].ToString();
            unique += "|";

            //获取SMBIOS的id https://docs.microsoft.com/zh-cn/windows/desktop/CIMWin32Prov/win32-computersystemproduct
            ManagementClass            cimobject3 = new ManagementClass("Win32_ComputerSystemProduct");
            ManagementObjectCollection moc3       = cimobject3.GetInstances();

            foreach (ManagementObject mo in moc3)
            {
                unique += (string)mo.Properties["UUID"].Value;
                break;
            }
            unique += "|";

            WindowsIdentity  current          = WindowsIdentity.GetCurrent();
            WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
            bool             IsAdmin          = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);

            //硬盘物理id
            if (IsAdmin)
            {
                var HddInfo = GetHddInfo();
                unique += HddInfo.SerialNumber;
            }
            SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
            var md5 = SoftBasic.ByteToHexString(SHA1.ComputeHash(Encoding.Unicode.GetBytes(unique)));

            return(md5.Substring(0, 25));
        }
Esempio n. 2
0
        /// <summary>
        /// 获取本计算机唯一的机器码
        /// </summary>
        /// <returns>字符串形式的机器码</returns>
        public static string GetInfo()
        {
            string unique = "";
            // 获取处理器信息
            ManagementClass            cimobject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc       = cimobject.GetInstances();

            foreach (ManagementObject mo in moc)
            {
                unique += mo.Properties["ProcessorId"].Value.ToString();
            }
            // 获取硬盘ID
            ManagementClass            cimobject1 = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1       = cimobject1.GetInstances();

            foreach (ManagementObject mo in moc1)
            {
                unique += (string)mo.Properties["Model"].Value;
                break;
            }

            // 获取BIOS
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("Select SerialNumber From Win32_BIOS");
            ManagementObjectCollection moc2 = searcher.Get();

            if (moc2.Count > 0)
            {
                foreach (ManagementObject share in moc2)
                {
                    unique += share["SerialNumber"].ToString();
                }
            }

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            return(SoftBasic.ByteToHexString(md5.ComputeHash(Encoding.Unicode.GetBytes(unique))).Substring(0, 25));
        }