欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现生成mac地址与IP地址注册码的两种方法

程序员文章站 2023-12-20 13:36:10
本文实例讲述了c#实现生成mac地址与ip地址注册码的两种方法,分享给大家供大家参考之用。具体方法如下: 方法一: using system; using s...

本文实例讲述了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))).replace("-", "").toupper().substring(8, 0x10);
        return (softname + "c" + cpuid);
      }
      if (biosserialnumber != "")
      {
        md5 md2 = new md5cryptoserviceprovider();
        biosserialnumber = bitconverter.tostring(md2.computehash(encoding.default.getbytes(biosserialnumber))).replace("-", "").toupper().substring(8, 0x10);
        return (softname + "b" + biosserialnumber);
      }
      if (harddiskserialnumber != "")
      {
        md5 md3 = new md5cryptoserviceprovider();
        harddiskserialnumber = bitconverter.tostring(md3.computehash(encoding.default.getbytes(harddiskserialnumber))).replace("-", "").toupper().substring(8, 0x10);
        return (softname + "h" + harddiskserialnumber);
      }
      if (netcardmacaddress != "")
      {
        md5 md4 = new md5cryptoserviceprovider();
        netcardmacaddress = bitconverter.tostring(md4.computehash(encoding.default.getbytes(netcardmacaddress))).replace("-", "").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))).replace("-", "").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))).replace("-", "").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#程序设计有所帮助。

上一篇:

下一篇: