aspose file tools*
The moose likes Java in General and the fly likes how to extract private key from pfx(PKCS) certificate using java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to extract private key from pfx(PKCS) certificate using java code" Watch "how to extract private key from pfx(PKCS) certificate using java code" New topic
Author

how to extract private key from pfx(PKCS) certificate using java code

arpit maurya
Greenhorn

Joined: Jul 29, 2011
Posts: 16
Can anyone please tell me how to extract the private key from .pfx file in java.
I am able to extract the client cert & server cert from the file..



Thanks in advance.
Alan Rosine
Greenhorn

Joined: Aug 20, 2004
Posts: 1
You've created the KeyStore and loaded the private key just fine. Your issue is the manner you are retrieving the Key from the KeyStore.

An alias is generated for you when you load your private key into the KeyStore. You are requesting alias "1" which does not exist so your getKey call returns null.

Key key = ks.getKey("1", "PrivateKeyPassword".toCharArray());
Certificate[] cc = ks.getCertificateChain("1");

Since you've created a new KeyStore, it obviously contains only the 1 key you have loaded. You can iterate over the list of aliases provided using the first one or you could use a shorter version as follows.

String alias = ks.aliases().nextElement();
Key key = ks.getKey(alias, "PrivateKeyPassword".toCharArray());
Certificate[] cc = ks.getCertificateChain(alias);

BTW. I'm hoping that the password for the private certificate that you posted on a public forum is for a development certificate.

HTH
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40064
    
  28
Welcome to the Ranch
And you finally broke your silence after 8 years!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to extract private key from pfx(PKCS) certificate using java code