问题描述
我用 KeyPairGenerator 生成 RSA 密钥对,我注意到它一直在生成完全匹配的密钥,而不是像它应该的随机密钥?也许有人知道为什么会这样?
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;
}
问题已解决:这是 HSM 和我使用的提供商的问题.查看对 Joachim Sauer 回复的评论.
PROBLEM SOLVED:It was issue with the HSM and the provider i use. Look at the comment on Joachim Sauer reply.
推荐答案
运行稍微修改过的代码版本(以使其编译)工作正常(即多次运行时产生不同的键:
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());
您使用的提供者是否有可能......行为不端?provider.getName()
的值是什么?
Is it possible that the provider you use is ... ill-behaving? What's the value of provider.getName()
?
这篇关于KeyPairGenerator 不生成随机密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!