Question

I want to create an encryption with java.
Is there anyway to get CPU Id or anything that be unique in PC such as BIOS or ...

for example System.getCpuId(); it is just an example 😉

Thanks a lot ...

Was it helpful?

Solution

if you need unique id you can use UUID :

import java.util.UUID;

public class GenerateUUID {


      public static final void main(String... aArgs){
        //generate random UUIDs
        UUID idOne = UUID.randomUUID();
        UUID idTwo = UUID.randomUUID();
        log("UUID One: " + idOne);
        log("UUID Two: " + idTwo);
      }

      private static void log(Object aObject){
        System.out.println( String.valueOf(aObject) );
      }
    } 

Example run :

>java -cp . GenerateUUID
UUID One: 067e6162-3b6f-4ae2-a171-2470b63dff00 
UUID Two: 54947df8-0e9e-4471-a2f9-9af509fb5889

OTHER TIPS

So you want a unique number (or string?) that identifies the user's computer? Or at least unique enough that the chance of a duplicate is very low, right?

You can get the Mac address of the network interface. This is making many assumptions, but it may be good enough for your needs:

final byte[] address = NetworkInterface.getNetworkInterfaces().nextElement().getHardwareAddress();
System.out.println("address = " + Arrays.toString(address));

This gives you an array of bytes. You can convert that to an id in several ways... like as a hex string.

Expect support though, when people replace bits of hardware in their computer.

I think such OS specific command is not available in Java.

This link shows a way to run it on windows.

You can't (reliably) get hardware information in pure Java. You would have to use JNA or JNI. Can you clarify what kind of encryption system you're building, and why you need the hardware info?

EDIT: Steve McLeod has noted that Java has a NetworkInterface.getHardwareAddress() method. However, there are serious caveats, including the fact that not all Java implementations allow access to it, and MAC addresses can be trivially forged.

There's no way to get hardware information directly with Java without some JNA/JNI library. That said, you can get "somewhat unique, system-specific values" with System.getEnv(). For instance,

System.getEnv("COMPUTERNAME")

should return computer's name in a Windows system. This is, of course, higly unportable. And the values can change with time in the same system. Or be the same in different systems. Oh, well...

You should also consider a machine can have more than one CPU/NIC/whatever and thus more than one IDs.

What you are really looking for is a good entropy source, but I would actually suggest you investigate the Java Cryptography Architechture as it provides a framework for this, so you can concentrate on your actual algorithm.

http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top