This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 38033
    
  22
Welcome to the Ranch
And you finally broke your silence after 8 years!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to extract private key from pfx(PKCS) certificate using java code
 
Similar Threads
featchin security info. in jsp
as of release 5, 'enum' is a keyword...
JAX-WS Client with SSL certificate
getting error while creating keysotre