Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74645
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
And you finally broke your silence after 8 years!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic