c#获取机器唯一识别码的方法记忆

2024-01-07 13:48

本文主要是介绍c#获取机器唯一识别码的方法记忆,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

来源:


http://www.cnblogs.com/leestar54/p/4375173.html

前言

在客户端认证的过程中,我们总要获取客户机的唯一识别信息,曾经以为MAC地址是不会变的,但是现在各种改,特别是使用无线上网卡,MAC地址插一次变一次,所以这样使用MAC就没有什么意义了,怎么办,又开始求助Google,最后找到一个折中的方案

原理

通过获取主板、处理器、BIOS、mac、显卡、硬盘等的ID生成唯一识别码

建议

1、使用那些不经常更换的模块来生成识别码。

2、如果经常更换MAC,显卡,硬盘,则不要使用这些ID。

3、确保使用static变量在整个应用来保存唯一识别码。

实现

复制代码
using System;
using System.Management;
using System.Security.Cryptography;
using System.Security;
using System.Collections;
using System.Text;
namespace Security
{/// <summary>/// Generates a 16 byte Unique Identification code of a computer/// Example: 4876-8DB5-EE85-69D3-FE52-8CF7-395D-2EA9/// </summary>public class FingerPrint  {private static string fingerPrint = string.Empty;public static string Value(){if (string.IsNullOrEmpty(fingerPrint)){fingerPrint = GetHash("CPU >> " + cpuId() + "\nBIOS >> " + biosId() + "\nBASE >> " + baseId()//+"\nDISK >> "+ diskId() + "\nVIDEO >> " + videoId() +"\nMAC >> "+ macId());}return fingerPrint;}private static string GetHash(string s){MD5 sec = new MD5CryptoServiceProvider();ASCIIEncoding enc = new ASCIIEncoding();byte[] bt = enc.GetBytes(s);return GetHexString(sec.ComputeHash(bt));}private static string GetHexString(byte[] bt){string s = string.Empty;for (int i = 0; i < bt.Length; i++){byte b = bt[i];int n, n1, n2;n = (int)b;n1 = n & 15;n2 = (n >> 4) & 15;if (n2 > 9)s += ((char)(n2 - 10 + (int)'A')).ToString();elses += n2.ToString();if (n1 > 9)s += ((char)(n1 - 10 + (int)'A')).ToString();elses += n1.ToString();if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";}return s;}#region Original Device ID Getting Code//Return a hardware identifierprivate static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue){string result = "";System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);System.Management.ManagementObjectCollection moc = mc.GetInstances();foreach (System.Management.ManagementObject mo in moc){if (mo[wmiMustBeTrue].ToString() == "True"){//Only get the first oneif (result == ""){try{result = mo[wmiProperty].ToString();break;}catch{}}}}return result;}//Return a hardware identifierprivate static string identifier(string wmiClass, string wmiProperty){string result = "";System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);System.Management.ManagementObjectCollection moc = mc.GetInstances();foreach (System.Management.ManagementObject mo in moc){//Only get the first oneif (result == ""){try{result = mo[wmiProperty].ToString();break;}catch{}}}return result;}private static string cpuId(){//Uses first CPU identifier available in order of preference//Don't get all identifiers, as it is very time consumingstring retVal = identifier("Win32_Processor", "UniqueId");if (retVal == "") //If no UniqueID, use ProcessorID
            {retVal = identifier("Win32_Processor", "ProcessorId");if (retVal == "") //If no ProcessorId, use Name
                {retVal = identifier("Win32_Processor", "Name");if (retVal == "") //If no Name, use Manufacturer
                    {retVal = identifier("Win32_Processor", "Manufacturer");}//Add clock speed for extra securityretVal += identifier("Win32_Processor", "MaxClockSpeed");}}return retVal;}//BIOS Identifierprivate static string biosId(){return identifier("Win32_BIOS", "Manufacturer")+ identifier("Win32_BIOS", "SMBIOSBIOSVersion")+ identifier("Win32_BIOS", "IdentificationCode")+ identifier("Win32_BIOS", "SerialNumber")+ identifier("Win32_BIOS", "ReleaseDate")+ identifier("Win32_BIOS", "Version");}//Main physical hard drive IDprivate static string diskId(){return identifier("Win32_DiskDrive", "Model")+ identifier("Win32_DiskDrive", "Manufacturer")+ identifier("Win32_DiskDrive", "Signature")+ identifier("Win32_DiskDrive", "TotalHeads");}//Motherboard IDprivate static string baseId(){return identifier("Win32_BaseBoard", "Model")+ identifier("Win32_BaseBoard", "Manufacturer")+ identifier("Win32_BaseBoard", "Name")+ identifier("Win32_BaseBoard", "SerialNumber");}//Primary video controller IDprivate static string videoId(){return identifier("Win32_VideoController", "DriverVersion")+ identifier("Win32_VideoController", "Name");}//First enabled network card IDprivate static string macId(){return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");}#endregion}
}
复制代码

 

参考

http://www.codeproject.com/Articles/28678/Generating-Unique-Key-Finger-Print-for-a-Computer

 

补充

现在遇到一些平板等简陋的机型,竟然获取到的所有设备标识都一样(除了mac),最后只好在本地再生成一个软件自身的标识,然后每次在计算标识的时候附带上,这样不会再重复了吧。

代码如下:

复制代码
        private static string localkey(){string path=Environment.CurrentDirectory + "client.key";if (File.Exists(path)){StreamReader sr = new StreamReader(path);string key= sr.ReadToEnd();sr.Close();return key;}else{StreamWriter sw = File.CreateText(path);string key = Guid.NewGuid().ToString();sw.WriteLine(key);sw.Close();return key;}}
复制代码

可以再把该文件设为隐藏等手段,防止用户误操作。

这篇关于c#获取机器唯一识别码的方法记忆的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/580119

相关文章

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行