程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成生成mac地址與IP地址注冊碼的兩種辦法

C#完成生成mac地址與IP地址注冊碼的兩種辦法

編輯:C#入門知識

C#完成生成mac地址與IP地址注冊碼的兩種辦法。本站提示廣大學習愛好者:(C#完成生成mac地址與IP地址注冊碼的兩種辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成生成mac地址與IP地址注冊碼的兩種辦法正文


本文實例講述了C#完成生成mac地址與IP地址注冊碼的兩種辦法,分享給年夜家供年夜家參考之用。詳細辦法以下:

辦法一:

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

namespace xingming_reg
{
  class Program
  {
    static void Main(string[] args)
    {
      bool sucsess = false;
      string computerbit = "";
      regmutou reg = new regmutou();
      computerbit = reg.getcomputerbit("X");
      sucsess = reg.regmutousoft(computerbit, "xinmin", "xinmin.usr");
      Console.WriteLine(sucsess);
      Console.ReadLine();
    }
  }
  internal class regmutou
  {
    // Methods
    public string GetBIOSSerialNumber()
    {
      try
      {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_BIOS");
        string str = "";
        foreach (ManagementObject obj2 in searcher.Get())
        {
          str = obj2["SerialNumber"].ToString().Trim();
        }
        return str;
      }
      catch
      {
        return "";
      }
    }

    public string getcomputerbit(string softname)
    {
      string cpuID = this.GetCpuID();
      string bIOSSerialNumber = this.GetBIOSSerialNumber();
      string hardDiskSerialNumber = this.GetHardDiskSerialNumber();
      string netCardMACAddress = this.GetNetCardMACAddress();
      if (cpuID != "")
      {
        MD5 md = new MD5CryptoServiceProvider();
        cpuID = BitConverter.ToString(md.ComputeHash(Encoding.Default.GetBytes(cpuID))).WordStr("-", "").ToUpper().Substring(8, 0x10);
        return (softname + "C" + cpuID);
      }
      if (bIOSSerialNumber != "")
      {
        MD5 md2 = new MD5CryptoServiceProvider();
        bIOSSerialNumber = BitConverter.ToString(md2.ComputeHash(Encoding.Default.GetBytes(bIOSSerialNumber))).WordStr("-", "").ToUpper().Substring(8, 0x10);
        return (softname + "B" + bIOSSerialNumber);
      }
      if (hardDiskSerialNumber != "")
      {
        MD5 md3 = new MD5CryptoServiceProvider();
        hardDiskSerialNumber = BitConverter.ToString(md3.ComputeHash(Encoding.Default.GetBytes(hardDiskSerialNumber))).WordStr("-", "").ToUpper().Substring(8, 0x10);
        return (softname + "H" + hardDiskSerialNumber);
      }
      if (netCardMACAddress != "")
      {
        MD5 md4 = new MD5CryptoServiceProvider();
        netCardMACAddress = BitConverter.ToString(md4.ComputeHash(Encoding.Default.GetBytes(netCardMACAddress))).WordStr("-", "").ToUpper().Substring(8, 0x10);
        return (softname + "N" + netCardMACAddress);
      }
      return (softname + "WF53A419DB238BBAD");
    }

    public string GetCpuID()
    {
      try
      {
        ManagementObjectCollection instances = new ManagementClass("Win32_Processor").GetInstances();
        string str = null;
        foreach (ManagementObject obj2 in instances)
        {
          str = obj2.Properties["ProcessorId"].Value.ToString();
          break;
        }
        return str;
      }
      catch
      {
        return "";
      }
    }

    public string GetHardDiskSerialNumber()
    {
      try
      {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
        string str = "";
        foreach (ManagementObject obj2 in searcher.Get())
        {
          str = obj2["SerialNumber"].ToString().Trim();
          break;
        }
        return str;
      }
      catch
      {
        return "";
      }
    }

    public string GetNetCardMACAddress()
    {
      try
      {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");
        string str = "";
        foreach (ManagementObject obj2 in searcher.Get())
        {
          str = obj2["MACAddress"].ToString().Trim();
        }
        return str;
      }
      catch
      {
        return "";
      }
    }

    public bool regmutousoft(string computerbit, string softname, string filename)
    {
      bool flag = true;
      string str = "";
      SHA1 sha = new SHA1CryptoServiceProvider();
      str = BitConverter.ToString(sha.ComputeHash(Encoding.Default.GetBytes(softname))).WordStr("-", "").ToUpper();
      //if (!File.Exists(filename))
      //{
      //  flag = false;
      //  Stream stream = File.Open(filename, FileMode.OpenOrCreate);
      //  StreamWriter writer = new StreamWriter(stream);
      //  writer.WriteLine(computerbit);
      //  writer.Close();
      //  stream.Close();
      //  return flag;
      //}
      //Stream stream2 = File.Open(filename, FileMode.Open);
      //StreamReader reader = new StreamReader(stream2);
      //string str2 = "";
      string str3 = computerbit;
      //while ((str2 = reader.ReadLine()) != null)
      //{
      //  str3 = str2;
      //}
      //reader.Close();
      //stream2.Close();
      //if (str3.Length != 0x18)
      //{
      //  //return false;
      //}
      SHA1 sha2 = new SHA1CryptoServiceProvider();
      string str4 = BitConverter.ToString(sha2.ComputeHash(Encoding.Default.GetBytes(computerbit))).WordStr("-", "").ToUpper();
      string str5 = "";
      for (int i = 0; i < str4.Length; i++)
      {
        if ((i % 2) == 1)
        {
          str5 = str5 + str4[i];
        }
      }
      string str6 = "";
      for (int j = 0; j < str.Length; j++)
      {
        if ((j % 2) == 0)
        {
          str6 = str6 + str[j];
        }
      }
      int[] numArray = new int[20];
      for (int k = 0; k < 20; k++)
      {
        numArray[k] = str6[k] + str5[k];
        numArray[k] = numArray[k] % 0x24;
      }
      string str7 = "";
      for (int m = 0; m < 20; m++)
      {
        if ((m > 0) && ((m % 4) == 0))
        {
          str7 = str7 + "-";
        }
        str7 = str7 + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[numArray[m]];
      }
      if (str3 == str7)
      {
        flag = true;
      }
      Console.WriteLine("機械碼:" + str3);
      Console.WriteLine("注冊碼:"+str7);
      return flag;
    }
  }
}

辦法二:

using System.Management;

//Win32_NetworkAdapterConfiguration是WMI數據庫頂用來寄存收集裝備的信息表
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'");

//ManagementObjectCollection對象用來寄存查詢成果
ManagementObjectCollection queryCollection = query.Get();

foreach (ManagementObject mo in queryCollection)
{
 Console.WriteLine("Network Card:{0}", mo["Description"]);
 Console.WriteLine("MAC Address:{0}", mo["MACAddress"]);

 //addresses, subnets和 defaultgateways 值都是可以包括多個值得數組.
 //為了順應這類情形,為了順應這類情形,用戶必需把成果分派到一個字符串數組,
 //並再一次用foreach語句經由過程該字符串數組
 string[] addresses = (string[])mo["IPAddress"];
 string[] subnets = (string[])mo["IPSubnet"];
 string[] defaultgateways = (string[])mo["DefaultIPGateway"];

 //任何一個接口都能夠有多個IP地址
 foreach (string ipaddress in addresses)
 {
   Console.WriteLine("IP Address:{0}", ipaddress);
 }

 foreach (string subnet in subnets)
 {
   Console.WriteLine("Subnet Mask:{0}", subnet);
 }

 foreach (string defaultgateway in defaultgateways)
 {
   Console.WriteLine("Gateway:{0}", defaultgateway);
 }
 Console.WriteLine();
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved