Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
arpit maurya
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
And you finally broke your silence after 8 years!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic