2011-11-30 106 views
2

我想知道为什么私钥是不同的,当我使用此代码:私人公钥是不同

java.security.KeyStore keyStoreFile = java.security.KeyStore.getInstance("PKCS12"); 
keyStoreFile.load(new FileInputStream("keyStore.pfx"),"password".toCharArray()); 
PrivateKey privateKey = (PrivateKey) keyStoreFile.getKey("alias","password".toCharArray()); 
String temp = new String(Base64.encodeBase64(privateKey.getEncoded())); 
System.out.println(temp); 

,当我使用使用相同的密钥库与密钥工具,iui.jnlp导出私钥?

我认为这是代码错误,因为它产生单行私钥。

任何人都可以建议我该怎么做,因为我需要获得公钥并将其传递给其他程序员。但公共密钥也是单行的,这是不正确的。 请帮忙!

回答

1

要从私钥获取公钥,您应该首先将私钥导出到证书中,然后从证书中导出(获取)公钥。

加载密钥库后,你可以写如下 -

证书CRT = keyStoreFile.getCertificate( “aliasOfPrivateKey”); PublicKey publicKey = crt.getPublicKey();

然后从publicKey获取编码的字符串。

要了解更多有关生成私人 - 公共密钥对,请参考以下 - http://technologytriumph.blogspot.in/2012/10/steps-to-generate-public-priavet-key.html