KeyPairGenerator 不生成随机密钥

发布于 2024-10-22 13:22:09 字数 657 浏览 1 评论 0原文

我使用 KeyPairGenerator 生成 RSA 密钥对,我注意到它始终生成完全匹配的密钥,而不是应有的随机密钥?也许有人知道为什么会这样?

我的代码现在看起来像这样:

private static KeyPair generateKeyPair(Provider provider, int keySize) throws Exception
{

    KeyPair keyPair = null;

    /* get the eracom keystore - access to the adapter */
    //KeyStore keyStore = KeyStore.getInstance("CRYPTOKI", provider.getName());
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", provider.getName());

    keyPairGenerator.initialize(keySize);
    keyPair = keyPairGenerator.generateKeyPair();

    return keyPair;
}

问题已解决: 这是 HSM 和我使用的提供商的问题。看看 Joachim Sauer 回复的评论。

Im generating RSA keypair with KeyPairGenerator and ive noticed that all the time its generating exactly matching keys, not random ones like it should be? Maybe anyone have some ide why could that be?

My code looks like this right now:

private static KeyPair generateKeyPair(Provider provider, int keySize) throws Exception
{

    KeyPair keyPair = null;

    /* get the eracom keystore - access to the adapter */
    //KeyStore keyStore = KeyStore.getInstance("CRYPTOKI", provider.getName());
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", provider.getName());

    keyPairGenerator.initialize(keySize);
    keyPair = keyPairGenerator.generateKeyPair();

    return keyPair;
}

PROBLEM SOLVED:
It was issue with the HSM and the provider i use. Look at the comment on Joachim Sauer reply.

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

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

发布评论

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

评论(3

瀞厅☆埖开 2024-10-29 13:22:09

我也遇到过同样的问题。对我有用的代码是:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");    
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024,random);
KeyPair key = keyGen.generateKeyPair();

现在打印它们以检查密钥是否不同,使用:

System.out.println(key.getPrivate());
System.out.println(key.getPublic());

SecureRandom 生成器使用给定的算法(SHA1PRNG)(在这种情况下)和 privider(在这种情况下是 SUN)创建一个新的随机数。您可以在这里找到更多信息:https://docs.oracle.com/ javase/tutorial/security/apisign/step2.html
和API在这里:
http://docs.oracle.com/javase/8 /docs/api/java/security/SecureRandom.html

I have faced the same problem. The code that worked for me is:

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");    
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024,random);
KeyPair key = keyGen.generateKeyPair();

Now print them to check for yourself if the keys are different using:

System.out.println(key.getPrivate());
System.out.println(key.getPublic());

The SecureRandom generator creates a new random number using the given algorithm (SHA1PRNG) in this case and the privider (SUN in this case). You can find more information here : https://docs.oracle.com/javase/tutorial/security/apisign/step2.html
and api here :
http://docs.oracle.com/javase/8/docs/api/java/security/SecureRandom.html

为你鎻心 2024-10-29 13:22:09

运行代码的稍微修改版本(以使其编译)工作得很好(即多次运行时产生不同的密钥:

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

keyPairGenerator.initialize(1024);
keyPair = keyPairGenerator.generateKeyPair();
System.out.println(keyPair.getPrivate());
System.out.println(keyPair.getPublic());

您使用的提供程序是否有可能......行为不端? provider.getName()

Running a slightly modified version of your code (to get it to compile) works just fine (i.e. produces different keys when run multiple times:

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

keyPairGenerator.initialize(1024);
keyPair = keyPairGenerator.generateKeyPair();
System.out.println(keyPair.getPrivate());
System.out.println(keyPair.getPublic());

Is it possible that the provider you use is ... ill-behaving? What's the value of provider.getName()?

疯狂的代价 2024-10-29 13:22:09

你初始化了吗?根据 这里它应该看起来像这样:

KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
KeyPair keyPair = generator.generateKeyPair();

Did you initialize it. According to here it should look something like:

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