java中如何获取cpu-id?

发布于 2024-08-20 05:07:28 字数 114 浏览 4 评论 0原文

我想用 java 创建加密。
是否有办法获取 CPU Id 或 PC 中唯一的任何内容,例如 BIOS 或...

例如 System.getCpuId(); 它是只是一个例子

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

赠佳期 2024-08-27 05:07:28

我认为这种操作系统特定的命令在 Java 中不可用。

此链接展示了在 Windows 上运行它的方法。

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

This link shows a way to run it on windows.

兔小萌 2024-08-27 05:07:28

那么您想要一个唯一的数字(或字符串?)来标识用户的计算机吗?或者至少足够独特,重复的可能性非常低,对吧?

您可以获取网络接口的 Mac 地址。这做出了很多假设,但它可能足以满足您的需求:

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

这为您提供了一个字节数组。您可以通过多种方式将其转换为 id...例如十六进制字符串。

不过,当人们更换计算机中的硬件时,请期待支持。

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.

铜锣湾横着走 2024-08-27 05:07:28

您无法(可靠地)使用纯 Java 获取硬件信息。您必须使用 JNAJNI。您能否澄清一下您正在构建哪种加密系统以及为什么需要硬件信息?

编辑:Steve McLeod 注意到 Java 有一个 NetworkInterface.getHardwareAddress() 方法。然而,有一些严重的警告,包括并非所有 Java 实现都允许访问它,并且 MAC 地址可以是 轻易伪造

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.

那伤。 2024-08-27 05:07:28

您还应该考虑一台机器可以有多个 CPU/NIC/任何东西,因此也可以有多个 ID。

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

找个人就嫁了吧 2024-08-27 05:07:28

如果您需要唯一的ID,您可以使用 UUID< /a> :

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) );
      }
    } 

运行示例:

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

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
以可爱出名 2024-08-27 05:07:28

如果没有一些 JNA/JNI 库,就无法直接使用 Java 获取硬件信息。也就是说,您可以使用System.getEnv()获取“某种独特的、特定于系统的值”。例如,

System.getEnv("COMPUTERNAME")

应返回 Windows 系统中的计算机名称。当然,这是非常不可移植的。并且在同一系统中这些值会随着时间而变化。或者在不同的系统中是相同的。那好吧...

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

梦初启 2024-08-27 05:07:28

您真正寻找的是一个好的熵源,但我实际上建议您研究 Java 密码学架构,因为它为此提供了一个框架,因此您可以专注于实际的算法。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文