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

作者:shichen2014 时间:2022-07-14 20:14:58 

本文实例讲述了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#程序设计有所帮助。

标签:C#,生成,注册码
0
投稿

猜你喜欢

  • C# 对XML基本操作代码总结

    2022-08-21 16:09:09
  • Android中访问证书有问题的SSL网页的方法

    2023-11-20 10:22:00
  • Spring Boot使用profile如何配置不同环境的配置文件

    2023-11-25 12:52:36
  • Java date format时间格式化操作示例

    2021-10-28 19:12:24
  • Spring内存缓存Caffeine的基本使用教程分享

    2023-05-26 00:30:33
  • HashMap的get()方法的NullPointerException问题

    2023-09-23 07:24:12
  • Spring 容器初始化 register 与 refresh方法

    2021-09-19 21:20:56
  • Android蓝牙的开启和搜索设备功能开发实例

    2022-11-07 20:42:17
  • 详解Java回环屏障CyclicBarrier

    2022-08-30 15:02:09
  • Java实现文件读取和写入过程解析

    2023-06-28 11:35:36
  • Spring RestTemplate的使用与踩坑

    2022-01-12 21:43:14
  • 聊聊在Servlet中怎么上传文件

    2022-03-07 17:56:44
  • Sharding-Proxy基本功能用法介绍

    2021-10-17 06:41:03
  • 实例讲解Java 自旋锁

    2021-09-17 09:38:50
  • jdk动态代理源码分析过程

    2023-03-01 05:05:59
  • 详解WPF如何使用必应地图控件

    2021-07-29 19:01:27
  • redisson 实现分布式锁的源码解析

    2022-06-05 05:38:47
  • C#实现的算24点游戏算法实例分析

    2021-12-01 04:13:21
  • Java Document生成和解析XML操作

    2021-11-10 13:17:46
  • Spring session 获取当前账户登录数的实例代码

    2022-10-17 10:02:05
  • asp之家 软件编程 m.aspxhome.com