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

相关文章

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

电脑死机无反应怎么强制重启? 一文读懂方法及注意事项

《电脑死机无反应怎么强制重启?一文读懂方法及注意事项》在日常使用电脑的过程中,我们难免会遇到电脑无法正常启动的情况,本文将详细介绍几种常见的电脑强制开机方法,并探讨在强制开机后应注意的事项,以及如何... 在日常生活和工作中,我们经常会遇到电脑突然无反应的情况,这时候强制重启就成了解决问题的“救命稻草”。那

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

C#中DrawCurve的用法小结

《C#中DrawCurve的用法小结》本文主要介绍了C#中DrawCurve的用法小结,通常用于绘制一条平滑的曲线通过一系列给定的点,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 如何使用 DrawCurve 方法(不带弯曲程度)2. 如何使用 DrawCurve 方法(带弯曲程度)3.使用Dr

Python中的魔术方法__new__详解

《Python中的魔术方法__new__详解》:本文主要介绍Python中的魔术方法__new__的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、核心意义与机制1.1 构造过程原理1.2 与 __init__ 对比二、核心功能解析2.1 核心能力2.2

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

关于pandas的read_csv方法使用解读

《关于pandas的read_csv方法使用解读》:本文主要介绍关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录pandas的read_csv方法解读read_csv中的参数基本参数通用解析参数空值处理相关参数时间处理相关