C# WIN 生成机器码

内容归档,不多赘述。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;

namespace MachineCode
{
    public class MachineCode
    {

        public static string GetMachineCode()
        {
            try
            {
                string rs = ToReverse(To_md5(GetCpuInfo() + GetMoAddress() + GetHDid()));
                return rs;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        ///   <summary>
        ///   获取cpu序列号    
        ///   </summary>
        ///   <returns> string </returns>
        private static string GetCpuInfo()
        {
            string cpuInfo = " ";
            using (ManagementClass cimobject = new ManagementClass("Win32_Processor"))
            {
                ManagementObjectCollection moc = cimobject.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                    mo.Dispose();
                }
            }
            return cpuInfo.ToString();
        }

        ///   <summary>
        ///   获取硬盘ID    
        ///   </summary>
        ///   <returns> string </returns>
        private static string GetHDid()
        {
            string HDid = " ";
            using (ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"))
            {
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                    mo.Dispose();
                }
            }
            return HDid.ToString();
        }

        ///   <summary>
        ///   获取网卡硬件地址
        ///   </summary>
        ///   <returns> string </returns>
        private static string GetMoAddress()
        {
            string MoAddress = " ";
            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
            {
                ManagementObjectCollection moc2 = mc.GetInstances();
                foreach (ManagementObject mo in moc2)
                {
                    if ((bool)mo["IPEnabled"] == true)
                        MoAddress = mo["MacAddress"].ToString();
                    mo.Dispose();
                }
            }
            return MoAddress.ToString();
        }




        /// <summary>
        /// 根据字符串生成MD5
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="up">是否大小写</param>
        /// <returns>生成后的MD5</returns>
        public static string To_md5(string str, bool up = false)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 =
            BitConverter.ToString(md5.ComputeHash(
            UTF8Encoding.Default.GetBytes(str)));
            t2 = t2.Replace("-", "");
            if (!up)
                return t2.ToLower();
            else
                return t2.ToUpper();
        }


        public static string ToReverse(string str)
        {
            return new string(str.ToCharArray().Reverse().ToArray());
        }
    }
}

 

posted @ 2020-10-29 13:59  码荣  阅读(443)  评论(0编辑  收藏  举报