Example #1
0
        /// <summary>
        /// 获取本计算机唯一的机器码
        /// </summary>
        /// <returns>字符串形式的机器码</returns>
        public static string GetInfo(bool UseAdmin)
        {
            //说明 这个方法所有的获取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 += "|";
            //如果启用了管理员模式 则读取hwid
            if (UseAdmin)
            {
                WindowsIdentity  current          = WindowsIdentity.GetCurrent();
                WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
                bool             IsAdmin          = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
                //硬盘物理id
                if (IsAdmin)
                {
                    var HddInfo = GetHddInfo();
                    unique += HddInfo.SerialNumber;
                }
                else
                {
                    //创建启动对象
                    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                    //设置运行文件
                    startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
                    //设置启动动作,确保以管理员身份运行
                    startInfo.Verb = "runas";
                    //如果不是管理员,则启动UAC
                    System.Diagnostics.Process.Start(startInfo);
                    //退出
                    System.Windows.Forms.Application.Exit();
                }
            }

            SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
            var md5 = SoftBasic.ByteToHexString(SHA1.ComputeHash(Encoding.Unicode.GetBytes(unique)));

            return(md5.Substring(0, 25));
        }