Click here to Skip to main content
16,018,318 members
Articles / Programming Languages / C#

Generating Unique Key (Finger Print) for a Computer for Licensing Purposes

Rate me:
Please Sign up or sign in to vote.
4.75/5 (54 votes)
24 Jan 2011CPOL2 min read 442.2K   294   73
Generating Unique Key(Finger Print) for a Computer for Licensing Purposes

Introduction

For licensing purposes, according to me, the best and secure way is to generate a unique key for the client's machine and provide a corresponding license key for that key. For this purpose, you can take help of the unique id of the client's computer motherboard, BIOS and processor. When you get these IDs, you can generate any key of your preferable format.

Year ago, I found a very handy and useful code in C# by searching the Internet to get these IDs. And it's serving me perfectly so far. Thanks to the original author of the code.

I added some additional code to generate a 128 bit key of a machine. The output is a nice looking key in hexadecimal format (eg. 4876-8DB5-EE85-69D3-FE52-8CF7-395D-2EA9).

Suggestions

I have a few suggestions in this regard:

  • Generate a key from only the Motherboard, Processor and BIOS since the user normally doesn't change these parts.
  • Don't use MAC ID, Graphics Card ID AND Disk ID since it's very common to change these devices.
  • It takes significant time to get IDs of devices. So make the finger print generating function static and save it in a static variable so that it generates the key only once in the whole application.

The Code

Here is the class. The code in the region "Original Device ID Getting Code" is from the original author.

C#
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();
                else
                    s += n2.ToString();
                if (n1 > 9)
                    s += ((char)(n1 - 10 + (int)'A')).ToString();
                else
                    s += n1.ToString();
                if ((i + 1) != bt.Length && (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 = "";
            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 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 = "";
            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 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
    }
}

Upcoming Code

Later I'll show how I generated the license key from the unique machine key. The license key contains a lot of information like the Product Name, License Start Date, End Date and the Key.

Keep eyes on my blog (sowkot.blogspot.com).

History

  • 19th August, 2008: Initial post

My Other Articles

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
-7 years of extensive experience in devloping both desktop and web applications in C#.NET, VB.NET and ASP.NET, ASP.NET MVC 3.0/4.0
-5 years of experience in developing web applications in J2EE, Free Marker, JSP, Spring MVC, Spring Webflow.
-Expertise in Software Architecture and Framework design.
Blog: http://sowkot.blogspot.com


Comments and Discussions

 
QuestionVirtual Machines Pin
tracker.19836-May-22 2:47
tracker.19836-May-22 2:47 
Questionhow to use this source code Pin
Modz5322-Jan-22 3:51
Modz5322-Jan-22 3:51 
AnswerRe: how to use this source code Pin
Member 1515026629-Mar-22 2:29
Member 1515026629-Mar-22 2:29 
QuestionThis process is not fully correct . Key changed after windows updates Pin
Santosh Rautaray11-Nov-19 22:25
Santosh Rautaray11-Nov-19 22:25 
QuestionNOT WORKING - Windows 8 & 10 - do not use Disk Signature Pin
Tom Zürich14-Jul-19 1:16
Tom Zürich14-Jul-19 1:16 
QuestionCode not working when build as .Net Core application in several build versions(ex :- Win 10, build 10240, Win 10 pro build 17763.503) Pin
Tirumaleswara Reddy.K30-May-19 0:02
Tirumaleswara Reddy.K30-May-19 0:02 
AnswerRe: Code not working when build as .Net Core application in several build versions(ex :- Win 10, build 10240, Win 10 pro build 17763.503) Pin
Member 1499463516-Nov-20 19:51
Member 1499463516-Nov-20 19:51 
QuestionWhy it generates same UniqueID for 2 different machines? Pin
Pritam Upadhyay28-Oct-16 2:26
Pritam Upadhyay28-Oct-16 2:26 
AnswerRe: Why it generates same UniqueID for 2 different machines? Pin
SonicMouse10-May-17 18:43
SonicMouse10-May-17 18:43 
PraiseWhich is best option to pass computer Id as computer signature to licensing Software? Pin
Biinny1-Apr-16 21:26
Biinny1-Apr-16 21:26 
QuestionFormat Issue Pin
vinodJohn200629-Mar-16 0:17
vinodJohn200629-Mar-16 0:17 
QuestionGenerating Unique Key (Finger Print) for a Linux and Mac Computer for Licensing Purposes Pin
Razique Anjum13-Jan-16 23:33
Razique Anjum13-Jan-16 23:33 
QuestionThanks for this , its work on localhost perfect but... Pin
hemantsaini18-Nov-15 19:20
hemantsaini18-Nov-15 19:20 
Questionhow to make use of this? Pin
Member 117989128-Nov-15 20:06
Member 117989128-Nov-15 20:06 
AnswerRe: how to make use of this? Pin
Member 1396057124-Aug-18 19:18
Member 1396057124-Aug-18 19:18 
PraiseGreat article! Pin
Mohit Arora28-Oct-15 14:24
Mohit Arora28-Oct-15 14:24 
BugProcessor no more unique now. BIOS too. Pin
qakmak25-Apr-15 19:53
qakmak25-Apr-15 19:53 
GeneralMy vote of 5 Pin
Guy__Levin13-Apr-15 6:08
Guy__Levin13-Apr-15 6:08 
Bugwin 8 problem Pin
umut_sayar23-Mar-15 22:48
umut_sayar23-Mar-15 22:48 
GeneralRe: win 8 problem Pin
Member 1019765031-Mar-15 3:02
Member 1019765031-Mar-15 3:02 
GeneralRe: win 8 problem Pin
qakmak21-Apr-15 16:36
qakmak21-Apr-15 16:36 
GeneralRe: win 8 problem Pin
omar_chavez21-Jul-15 6:51
omar_chavez21-Jul-15 6:51 
GeneralRe: win 8 problem Pin
Member 1053683728-Aug-16 19:54
Member 1053683728-Aug-16 19:54 
GeneralRe: win 8 problem Pin
Tom Zürich12-Jul-19 3:29
Tom Zürich12-Jul-19 3:29 
BugProblem with disk fingerprint and USB drives Pin
Paul 123459-Mar-15 23:56
Paul 123459-Mar-15 23:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.