java 如何在java中获取cpu-id?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2206712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get cpu-id in java?
提问by Freeman
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 ...
我想用java创建一个加密。
有没有办法获得 CPU Id 或任何在 PC 中唯一的东西,例如 BIOS 或...
for example System.getCpuId();it is just an example
例如System.getCpuId();这只是一个例子
Thanks a lot ...
非常感谢 ...
采纳答案by Sajad Bahmani
if you need unique id you can use UUID:
如果你需要唯一的 id 你可以使用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
回答by Steve McLeod
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:
您可以获取网络接口的 Mac 地址。这是做出许多假设,但它可能足以满足您的需求:
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.
这为您提供了一个字节数组。您可以通过多种方式将其转换为 id ......就像一个十六进制字符串。
Expect support though, when people replace bits of hardware in their computer.
不过,当人们更换计算机中的硬件时,请期待支持。
回答by Matthew Flaschen
You can't (reliably) get hardware information in pure Java. You would have to use JNAor JNI. Can you clarify what kind of encryption system you're building, and why you need the hardware info?
您不能(可靠地)使用纯 Java 获取硬件信息。您将不得不使用JNA或JNI。您能否说明您正在构建什么样的加密系统,以及您为什么需要硬件信息?
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.
编辑:Steve McLeod 指出 Java 有一个NetworkInterface.getHardwareAddress()方法。然而,有一些严重的警告,包括并非所有 Java 实现都允许访问它,并且 MAC 地址可以被轻易伪造。
回答by Laurent K
回答by Joonas Pulakka
There's no way to get hardware information directly with Java without some JNA/JNI library. That said, you canget "somewhat unique, system-specific values" with System.getEnv(). For instance,
如果没有 JNA/JNI 库,就无法直接使用 Java 获取硬件信息。也就是说,您可以使用System.getEnv(). 例如,
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...
在 Windows 系统中应该返回计算机的名称。当然,这是非常不可移植的。在同一系统中,这些值可以随时间变化。或者在不同的系统中是相同的。那好吧...
回答by user267027
You should also consider a machine can have more than one CPU/NIC/whatever and thus more than one IDs.
您还应该考虑一台机器可以有多个 CPU/NIC/任何东西,因此有多个 ID。
回答by Thorbj?rn Ravn Andersen
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.
您真正要寻找的是一个很好的熵源,但实际上我建议您调查 Java Cryptography Architechture,因为它为此提供了一个框架,这样您就可以专注于您的实际算法。
http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html
http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html

