0

i want to get a unique serial number in every computer for a java program what can i use ? is Mac address good hat is the code for my problem ? a already have these two which one is better ?

   public static void main(String[] args){

InetAddress ip;
try {

    ip = InetAddress.getLocalHost();
    System.out.println("Current IP address : " + ip.getHostAddress());

    NetworkInterface network = NetworkInterface.getByInetAddress(ip);

    byte[] mac = network.getHardwareAddress();

    System.out.print("Current MAC address : ");

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
    }
    System.out.println(sb.toString());

} catch (UnknownHostException e) {

    e.printStackTrace();

} catch (SocketException e){

    e.printStackTrace();

}

}

}

public static void main(String[] args) throws Throwable {
    // wmic command for diskdrive id: wmic DISKDRIVE GET SerialNumber
    // wmic command for cpu id : wmic cpu get ProcessorId
    //wmic command for bios :wmic bios get serialnumber
    Process process = Runtime.getRuntime().exec(new String[] { "wmic", "csproduct", "get", "uuid" });


    process.getOutputStream().close();
    Scanner sc = new Scanner(process.getInputStream());
    String property = sc.next();
    String serial = sc.next();
    System.out.println(property + ": " + serial);
}

is it a good way too use ProcessorId and uuid together?

M_rey3000
  • 176
  • 3
  • 14
  • java.util.UUID? – kingkupps Jul 16 '18 at 00:29
  • 2
    A MAC address is a decent unique identifier for simple network applications. It isn't perfect (for example, a single system can have a board swapped out and will get a new different MAC address) but a lot of things don't require perfection. If you have more than one MAC, then just pick the first one. They're all equivalent as identifiers. – markspace Jul 16 '18 at 00:32
  • Note that UUID is not guaranteed to be unique. It's a random number, and could be duplicated. MAC addresses have number ranges assigned by a controlling authority to different manufacturers, so it should really be unique (but there's nothing that prevents a manufacturer from just making up numbers too). – markspace Jul 16 '18 at 00:34
  • Also note that MAC addresses can be overridden in network configuration, making them not guaranteed to globally unique. But as markspace said, it's a *decent* unique identifier for most purposes. There is no absolute answer to your question. – Andreas Jul 16 '18 at 00:42
  • 2
    its a good way too use ProcessorId and uuid together? – M_rey3000 Jul 16 '18 at 00:46
  • 1
    Yes, that is a decent way, the more different ID you use the more unique the code is likely to be. – sorifiend Jul 16 '18 at 01:58
  • Use UUID for this , https://en.wikipedia.org/wiki/Universally_unique_identifier you can create a UUID from java.util.UUID class – Keaz Jul 16 '18 at 03:05
  • @markspace "UUID is not guaranteed to be unique" that depends which type of UUID you chose. And even if you choose a random UUID, the chances of a duplicate are for all practical purposes negligible. – Henry Jul 16 '18 at 03:43

1 Answers1

3

From what I can tell, there is nothing that could not be faked by someone with access to the hardware.

  • The MAC address of a NIC can be changed relatively easily in software on a typical OS if you have admin rights.

  • The ProcessorId (as reported by "wmic") is harder because it comes from the BIOS and the OS treats this as a read-only property; see How to change the ProcessorId.

    However:

    • it may be possible to change the ID via the BIOS itself,
    • the user could potentially replace the BIOS,
    • the user could potentially update the private memory location (?) that the BIOS is reading the ProcessorId value from.
    • if your code used "wmic" to retrieve the ProcessorId, it would be feasible to replace it with a version that returned a fake id
    • etcetera

    See also: WIN32_Processor::Is ProcessorId Unique for all computers

Even assuming that you could stop users from faking the ids, if they can spin a virtual machine, they can almost certainly fake the ids in the virtual.

This means that any scheme that you develop that depends on having a unique, unfakeable (uncloneable) identifier for a stock-standard Windows system is fundamentally flawed. If you really need unfakeable ids, you will have to look at alternative approaches like tamper-proof hardware "dongles".

On the other hand, if you are willing to accept that users could fake id information, then combining the MAC address, the ProcessorId and/or a type 4 UUID should be sufficient for uniqueness. (Indeed, just a type 4 UUID by itself should be sufficient, provided you have a good source of random numbers.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216