Preface
During the client authentication process, we always need to obtain the unique identification information of the client. We once thought that the MAC address would not change, but now we have various changes, especially when using wireless network cards, the MAC address is plugged in and changed once. So there is no point in using MAC in this way. What should I do? I started asking for help from Google again and finally found a compromise solution.
principle
Generate a unique identification code by obtaining the IDs of motherboard, processor, BIOS, mac, graphics card, hard disk, etc.
suggestion
1. Use modules that are not frequently replaced to generate identification codes.
2. If you change MAC, graphics card, or hard disk frequently, do not use these IDs.
3. Make sure to use static variables to save the unique identification code throughout the application.
accomplish
Pay attention to citations
using System; using ; using ; using ; using ; using ; 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 = ; public static string Value() { if ((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 = (s); return GetHexString((bt)); } private static string GetHexString(byte[] bt) { string s = ; for (int i = 0; i < ; 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(); else s += (); if (n1 > 9) s += ((char)(n1 - 10 + (int)'A')).ToString(); else s += (); if ((i + 1) != && (i + 1) % 2 == 0) s += "-"; } return s; } #region Original Device ID Getting Code //Return a hardware identifier private static string identifier (string wmiClass, string wmiProperty, string wmiMustBeTrue) { string result = ""; mc = new (wmiClass); moc = (); foreach ( mo in moc) { if (mo[wmiMustBeTrue].ToString() == "True") { //Only get the first one if (result == "") { try { result = mo[wmiProperty].ToString(); break; } catch { } } } } return result; } //Return a hardware identifier private static string identifier(string wmiClass, string wmiProperty) { string result = ""; mc = new (wmiClass); moc = (); foreach ( mo in moc) { //Only get the first one if (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 consuming string 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 security retVal += identifier("Win32_Processor", "MaxClockSpeed"); } } return retVal; } //BIOS Identifier private 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 ID private static string diskId() { return identifier("Win32_DiskDrive", "Model") + identifier("Win32_DiskDrive", "Manufacturer") + identifier("Win32_DiskDrive", "Signature") + identifier("Win32_DiskDrive", "TotalHeads"); } //Motherboard ID private static string baseId() { return identifier("Win32_BaseBoard", "Model") + identifier("Win32_BaseBoard", "Manufacturer") + identifier("Win32_BaseBoard", "Name") + identifier("Win32_BaseBoard", "SerialNumber"); } //Primary video controller ID private static string videoId() { return identifier("Win32_VideoController", "DriverVersion") + identifier("Win32_VideoController", "Name"); } //First enabled network card ID private static string macId() { return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled"); } #endregion } }
Replenish
Now that we encounter some simple models such as tablets, all the device logos obtained are the same (except for mac). In the end, we have to generate a software's own logo locally, and then attach it every time when calculating the logo, so that it will not be repeated again.
The code is as follows:
private static string localkey() { string path= + ""; if ((path)) { StreamReader sr = new StreamReader(path); string key= (); (); return key; } else { StreamWriter sw = (path); string key = ().ToString(); (key); (); return key; } }
You can then set the file to hide and other means to prevent users from operating incorrectly.
Supplement 2
Files are easily deleted by mistake and can also be written to the registry unless the system is reinstalled, but they need to run with administrator privileges.
class RegistryHelper { const string _uriDeviecId = "SOFTWARE\\YourCompany\\YouApp"; public static string GetDeviceId() { string ret = ; using (var obj = (_uriDeviecId, false)) { if (obj != null) { var value = ("DeviceId"); if (value != null) ret = (value); } } return ret; } public static void SetDeviceId() { using (MD5 md5Hash = ()) { byte[] data = (Encoding.(())); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < ; i++) { (data[i].ToString("x2")); } string id = (); using (var tempk = (_uriDeviecId)) { ("DeviceId", id); } } } }
The above is the detailed content of the example of obtaining machine unique identification code in C#. For more information about obtaining machine identification code in C#, please pay attention to my other related articles!