views:

955

answers:

7

Hello guys

I want create an encryption with java ; How can i get CPU Id or anythings is unique in pc such as bios or ...

for example System.getCpuId(); :) it's just example ;)

Thanks a lot ...

+2  A: 

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.

Matthew Flaschen
I wrote an application and i want to encrypt it ...
Mike Redford
Not strictly true, as the java.net.NetworkInterface class shows.
GregS
You're right. But note that not all implementations support this method (see http://java.sun.com/javase/6/docs/api/java/net/NetworkInterface.html#getHardwareAddress%28%29), 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.
Matthew Flaschen
+4  A: 

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
SjB
What the exact meaning of UUID?
Mike Redford
universally unique identifier
PeanutPower
UUID is unique in JAVA and use in field that need universal unique ID
SjB
OK ; Is different My UUID with all people in the world ???
Mike Redford
@Mike Redford : of course some application work with UUID.It is universal :-)
SjB
Thanks Dear SjB ;) rep++
Mike Redford
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.
Steve McLeod
I think the OP wants an identifier that is stable (i.e. will stay the same for that machine) and non-trivial to fake.
finnw
+1  A: 

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

This link shows a way to run it on windows.

Laurent K
Thanks it's maybe useful I'll check it :)
Mike Redford
This is actually quite clever! Someone should port this to other environments too, and package it all into one library.
Joonas Pulakka
Is it realy possible ?
Mike Redford
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.
Joonas Pulakka
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.
rascher
A: 

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

Joonas Pulakka
Ok if the user want change his/her computer name ????
Mike Redford
You can get MAC addresses without JNI, FWIW
finnw
+1  A: 

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

Thorbjørn Ravn Andersen
+2  A: 

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.

Steve McLeod
Thanks but if my user doesn't have any NIC what ???
Mike Redford
This can easily be changed (http://www.topbits.com/how-to-change-a-mac-address.html)
Matthew Flaschen
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...
Steve McLeod
Wow thanks Matthew it's perfect :)
Mike Redford
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.
ericp
A: 

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

No problem you know why? because one cpuid is enough for generating a code ...
Mike Redford