Creating PrivateKey Object from PKCS12
I created private key from PKCS12 file with this command:
openssl pkcs12 -in test.p12 -nocerts -out privateKey.pem
How can I create PrivateKey Object from this privateKey.pem file now?
I tried using PKCS12 file itself with this code:
KeyStore pfx = KeyStore.getInstance("pkcs12");
pfx.load(new FileInputStream(P12), "123456".toCharArray());
final Enumeration<String> aliases = pfx.aliases(); //this is empty
pfx.aliases() - was empty, I verified using keytool that it is really
empty, no entries.
keytool -v -list -storetype pkcs12 -keystore test.p12
Keystore type: PKCS12
Keystore provider: SunJSSE
Your keystore contains 0 entries
My question is how can I create PrivateKey using code like this:
public static RSAPrivateKey getPrivateKey(File privateKeyFile) throws
IOException {
byte[] keyBytes = new byte[(int) privateKeyFile.length()];
FileInputStream fis = new FileInputStream(privateKeyFile);
fis.read(keyBytes);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);// it
works only with PKCS8
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey privKey = (RSAPrivateKey)
keyFactory.generatePrivate(spec);
return privKey;
}
The problem with this code it only works for PKCS8, I need something like
this for PKCS12.
No comments:
Post a Comment