Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computer specific ID?

Tags:

java

I'm trying to generate a specific computer id using Java. I've thought about things like Hard Drive Serials, or Windows Serial Keys, CPU ID's, or MAC Addresses, but other computers could have the same ones.. For instance, If someone pirates a copy of Windows 7 they could have the same serial as someone.. I was wondering if someone could give me a way to generate a computer specific ID that is never changed and is retrievable using Java?

I did some research and found some useful functions. And, I was thinking something like this. But if they change their hardware, It will change the computer ID. Anyone know of something I can use?

public String getComputerID(){
    InetAddress ip = InetAddress.getLocalHost();
    NetworkInterface network = NetworkInterface.getByInetAddress(ip);
    byte[] mac = network.getHardwareAddress();
    String sn = getSerialNumber("C");
    String cpuId = getMotherboardSN();
    return MD5(mac + sn + cpuId);
}


public String MD5(String md5) {
try {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
        sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
    }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
    }
    return null;
}

public String getSerialNumber(String drive) {
String result = "";
    try {
    File file = File.createTempFile("realhowto",".vbs");
    file.deleteOnExit();
    FileWriter fw = new java.io.FileWriter(file);

  StringBufferring vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                +"Set colDrives = objFSO.Drives\n"
                +"Set objDrive = colDrives.item(\"" + drive + "\")\n"
                +"Wscript.Echo objDrive.SerialNumber";  // see note
    fw.write(vbs);
    FileWriter.close();
      Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
      BufferedReader input =
        new BufferedReader
        (new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = input.readLine()) != null) {
        result += line;
    }
    input.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return result.trim();
}

public String getMotherboardSN() {
String result = "";
try {
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);

String vbs =
"Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n"
+ "Next \n";

fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
}
catch(Exception e){
e.printStackTrace();
}
return result.trim();
}
like image 718
Nathan F. Avatar asked Nov 16 '12 19:11

Nathan F.


People also ask

How do you find the computer ID?

On WindowsGo to the Start menu, then in the search box type “cmd” and hit Enter. In the cmd window, type “ipconfig /all”. Find the line that reads “Physical Address”. This is your Machine ID.

What is computer device ID?

A device ID is a string reported by a device's enumerator (its bus driver). A device has only one device ID. A device ID has the same format as a hardware ID. The Plug and Play (PnP) manager uses the device ID as one of the inputs into the creation of the device instance ID.

Does a computer have a unique ID?

A universally unique identifier (UUID) is a 128-bit label used for information in computer systems. The term globally unique identifier (GUID) is also used. When generated according to the standard methods, UUIDs are, for practical purposes, unique.

How do I find my computer ID MAC?

On Windows, you can find the computer ID by clicking on Start > Settings > “System” > “About“. Under “Device Specifications“, you'll see the computer ID. On a Mac, click “About This Mac” > “System Report” > “Hardware“.


Video Answer


1 Answers

I actually do believe you should use something from hardware profile.
A computer can be considered as a set of pieces of hardware, including the network interface. A typical pattern can be to have combination of a MAC address and a generated ID by a management system that manages computers over the network.
The MAC address to identify uniquely the machine during a registration process to the management system.
As a result of the registration, the management system can return a generated UniqueId,
to be stored on the computer that registered to it, and will later on be used.
After a successful registration, you can replace the network interface card, as the computer does not depend on the MAC address to be identified.
You can also consider using the linux dmidecode utility
(for linux machines,
as you provided a win-based solution,
so for our linux readers,
I would like to suggest a linux alternaties) (if the machine you want to uniquely identify has linux and dmidecoe installed).
Using dmidecoe you can get more hardware profile, and perform some hash function on it, and generate a unique ID that will identify uniquely (with high probability, to be precise) your machine.
Read more about dmidecode here.
Of course, in case you go to "get information on hardware from the operating system" approach (which is dmidecode or what you suggested at the part after getting the MAC address,
You need for a cross platform code to check what is the OS the java program runs on, you do that using this:

System.getProperty("os.name");
like image 115
Yair Zaslavsky Avatar answered Sep 23 '22 05:09

Yair Zaslavsky