简体   繁体   English

无法使用Java从PKCS12文件访问私钥

[英]Not able to access Private key from PKCS12 file using java

I have created a pkcs12 file having a private key entry without any password. 我创建了一个pkcs12文件,其中包含没有任何密码的私钥条目。 I am able to access the p12 file using keytool utility without providing any password. 我可以使用keytool实用程序访问p12文件,而无需提供任何密码。

The same I am not able to to programmatically. 同样,我无法以编程方式进行。 When I am trying like below 当我尝试如下

if( keyStore.isKeyEntry(KEYSTORE_ALIAS)) {
    key = (PrivateKey) keyStore.getKey(KEYSTORE_ALIAS,new char[0]);
}

I am getting the below exception 我收到以下异常

Caused by: javax.crypto.BadPaddingException: Given final block not
properly padded
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
    at com.sun.crypto.provider.PKCS12PBECipherCore.implDoFinal(PKCS12PBECipherCore.java:355)
    at com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndDESede.engineDoFinal(PKCS12PBECipherCore.java:387)
    at javax.crypto.Cipher.doFinal(Cipher.java:2087)
    at sun.security.pkcs12.PKCS12KeyStore.engineGetKey(PKCS12KeyStore.java:266)
    ... 2 more
key = (PrivateKey) keyStore.getKey(KEYSTORE_ALIAS,new char[0]);

That's not the correct way to specify no key. 这不是指定没有密钥的正确方法。 Here you are specifying a zero-length key. 在这里,您指定一个零长度的密钥。 It isn't the same thing. 这不是同一回事。 Try this: 尝试这个:

key = (PrivateKey) keyStore.getKey(KEYSTORE_ALIAS, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM