How to get cpu-id in java?
Asked Answered
P

7

8

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 ...

Pole answered 5/2, 2010 at 11:14 Comment(8)
does it need to give the same result for the same machine? it sounds like you could get away with just generating a random key and storing it? – Gwenngwenneth
No, you know why? because I want get cpuid and then generate a code and after that users sent it(generated code) to me and I send to them activation code ... – Pole
So this is really a DRM scheme? – Seato
If DRM is Acronym for Digital Rights Management ; YES :) – Pole
Duplicate: #1987232 – Erbium
Note that if your software will be available for download on the Internet, crackers will simply remove the part of your code that checks the activation key against the cpu id. – Carmelitacarmelite
@PeanutPower, Not ideal for copy protection purposes as it can be replicated on another machine just by copying files & registry entries. Motherboard IDs can be faked too but that takes a bit more effort. – Erbium
Ah, yet another license manager. This will cost you more sweat, blood and tears than you will get in revenue. – Carboy
K
1

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
Kulda answered 5/2, 2010 at 11:19 Comment(6)
What the exact meaning of UUID? – Pole
UUID is unique in JAVA and use in field that need universal unique ID – Kulda
@Mike Redford : of course some application work with UUID.It is universal :-) – Kulda
You'll need to store the UUID you generated locally, to match it later when you send the activation code. It's easy for a determined user to work around this. – Carmelitacarmelite
I think the OP wants an identifier that is stable (i.e. will stay the same for that machine) and non-trivial to fake. – Erbium
The poster was asked for the CPU id not a UUID – Macula
T
5

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

This link shows a way to run it on windows.

Technocracy answered 5/2, 2010 at 11:20 Comment(4)
This is actually quite clever! Someone should port this to other environments too, and package it all into one library. – Rounded
Well, Linux (and I believe OS X too) has commands like dmesg and uname and places like /proc/cpuinfo that give various hardware-related information. So it would be just a matter of running the relevant commands with System.exec and parsing their output. Of course, it's hard to guarantee that some specific information is available (or that it is correct), but at least it could be tried. – Rounded
this specific command may be architecture-specific, not OS-specific - eg, the CPUID instruction and related instructions. That said, this is probably going to run on x86, so I don't usually see this as a Big Issue. – Priority
This is the clsoest to the correct answer according to the question – Macula
C
5

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.

Carmelitacarmelite answered 5/2, 2010 at 11:34 Comment(5)
This can easily be changed (topbits.com/how-to-change-a-mac-address.html) – Seato
That's of the many assumptions I was making! Just about every computer these days is net connected, and therefore has a NIC. But depending on your needs, that might not be a good assumption... – Carmelitacarmelite
I had some software that used this, and it was really, really annoying. I installed it on my laptop while at home, with my wired connection, and then I couldn't use it when I was using wireless. I know you can reorder your network devices in Network Connections, but it's a hassle. – Topknot
MAC could be easily changed. It is not supposed to run two machines in the same network – Wisconsin
I tried to use this approach but stumbled upon user who deactivate their network-access when they think they do not need it. The problem with that is, that whenever this happens the MAC-address is "gone" ... – Streamlined
S
3

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.

Seato answered 5/2, 2010 at 11:18 Comment(2)
Not strictly true, as the java.net.NetworkInterface class shows. – Unsearchable
You're right. But note that not all implementations support this method (see java.sun.com/javase/6/docs/api/java/net/…), not all computers have network cards at all, people change network cards often (think wired->wireless), and it is trivial to forge the address. Given all that, I consider this a dead end. – Seato
C
2

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

Cullie answered 5/2, 2010 at 12:3 Comment(0)
K
1

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
Kulda answered 5/2, 2010 at 11:19 Comment(6)
What the exact meaning of UUID? – Pole
UUID is unique in JAVA and use in field that need universal unique ID – Kulda
@Mike Redford : of course some application work with UUID.It is universal :-) – Kulda
You'll need to store the UUID you generated locally, to match it later when you send the activation code. It's easy for a determined user to work around this. – Carmelitacarmelite
I think the OP wants an identifier that is stable (i.e. will stay the same for that machine) and non-trivial to fake. – Erbium
The poster was asked for the CPU id not a UUID – Macula
R
1

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...

Rounded answered 5/2, 2010 at 11:22 Comment(1)
You can get MAC addresses without JNI, FWIW – Erbium
D
0

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

Damal answered 5/2, 2010 at 11:22 Comment(1)
The user wants the CPU ID not a random number. – Macula

© 2022 - 2024 β€” McMap. All rights reserved.