c#获取机器唯一识别码

前言

在客户端认证的过程中,我们总要获取客户机的唯一识别信息,曾经以为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#获取机器唯一识别码相关推荐

  1. java中获得手机唯一_Android 获取手机唯一识别码

    [实例简介]得到手机唯一识别码 [实例截图] [核心代码]private String ChuanMa() { String szImei;// imei码 String m_szDevIDShort ...

  2. android 获取手机唯一识别码

    //如果获取不到imsi号,就拼接其它信息获取唯一码: 结论是,依然有部分手机获取不到. final TelephonyManager tm = (TelephonyManager) getBaseC ...

  3. Android 获取手机系统版本号、获取手机型号、获取手机厂商、获取手机IMEI、获取手机CPU_ABI、获取手机唯一识别码

    1.先申请权限,关于如何申请权限请查看RxPermissions的使用(简单实用)_ErwinNakajima的博客-CSDN博客 2.手機唯一識別碼管理類. package com.phone.co ...

  4. python获取机器唯一标识_通过python 获取cpu和硬盘等硬件序列号组成的唯一识别码...

    import wmi c = wmi.WMI() def yingpan(): # # 硬盘序列号 cc = "" for physical_disk in c.Win32_Dis ...

  5. 获取手机唯一识别码IMEI

    前言 获取IMEI相信大家非常熟悉,但是项目中使用时,发现当手机卡为电信的时候,获取的并不是IMEI,而是MEID,什么是MEID,为什么会出现这种情况呢? IMEI国际移动设备识别码(IMEI:In ...

  6. python获取机器唯一标识_开发中常用工具 - 获取设备的唯一标识、UDID、UUID、keychain保存UUID、判断网络...

    UDID 全名:Unique Device Identifie(设备唯一标识符) 说明:UDID,即设备唯一标识符,这是除序列号之外每台iOS设备的独一无二的号码.UDID只是和设备相关的,是用来区分 ...

  7. python获取机器唯一标识_python中uuid来生成机器唯一标识

    摘要: 我们可以使用uuid1的后16位来标识一个机器. # use machine specific uuid, last 16 char will be the same if machine i ...

  8. c#获取对象的唯一标识_C#如何获取机器唯一标识符

    展开全部 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...

  9. Java:物联网终端机如何生成唯一识别码

    1.前言   最直接的思路是用CPU的序列号和主板编号来做,但是当你使用:sudo dmidecode -s baseboard-serial-number来获取主板编号时,大部分情况下会给你返回: ...

最新文章

  1. abap选择屏幕上的button
  2. 光伏产业链遭致命伤:补贴下调或结束暴利
  3. 在机器学习分类中如何处理训练集中不平衡问题
  4. 第一节《Git初始化》
  5. 圆的交点 (Python)
  6. mysql 大数据量插入遇到瓶颈 可行性方案探究
  7. Android ProGuard 还原堆栈
  8. github博客绑定个性域名
  9. 重度抑郁症患者的脑龄
  10. Pytorch中设置哪些随机数种子,才能保证实验可重复
  11. 我的世界java版如何加入hypixel_我的世界hypixel服务器怎么组队 hypixel服务器组队方法介绍...
  12. 语义分割常用指标详解(附代码)
  13. 2920集五福_支付宝集五福攻略 ▏顺便学点营销活动传播套路
  14. Qt 编译报错 error: invalid use of incomplete type 'class QXxx'
  15. 物联网开发笔记(50)- 使用Micropython开发ESP32开发板之控制HC-SR501人体红外感应传感器
  16. 进程,系统性能和计划任务
  17. 低延时应用 服务器TurboBoost不可得兼?
  18. 计算机按硬件组合及用途分为,计算机基础.doc
  19. 小学计算机flash,小学信息技术第五册  9.初识flash软件
  20. 2021桂林ccpc B. A Plus B Problem,set维护

热门文章

  1. 力扣——搜索插入位置
  2. ❤️《大前端—了解与使用NodesJS》
  3. com.mysql.cj.jdbc.Driver这个驱动类
  4. JavaScript中如何自定义属性操作
  5. 操作元素之修改元素属性
  6. 对联广告(jQuery)
  7. vue draggable 火狐拖拽搜索bug解决
  8. spring cloud简介之最好参考
  9. Android每日一记
  10. win10屏蔽自动更新方法