KeyPairGenerator 不生成随机密钥
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也遇到过同样的问题。对我有用的代码是:
现在打印它们以检查密钥是否不同,使用:
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:
Now print them to check for yourself if the keys are different using:
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
运行代码的稍微修改版本(以使其编译)工作得很好(即多次运行时产生不同的密钥:
您使用的提供程序是否有可能......行为不端?
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:
Is it possible that the provider you use is ... ill-behaving? What's the value of
provider.getName()
?你初始化了吗?根据 这里它应该看起来像这样:
Did you initialize it. According to here it should look something like: