Get Private key from pfx or p12 file

Would anyone tell me how to get the private key from pfx file or p12 file in Java.Thanks a lot.
[123 byte] By [cslhs] at [2007-9-26 7:00:22]
# 1

hi!

i had the same problem and here is the solution for it,

the pfx is a pkcs#12 standard and tho cryptographic tool kit which supports the pkcs#12 standards is the entrust toolkit, so if you are using that toolkit then you have to first get an inputstream from the pfx file get the pkcs12Readerobject, decrypt that object with the password used for the privatekey protection in pfx file. now you get the safebags get the safe bag os private kay and get the private key try this i can also give you the exact code.

Tarun

tarunmatai at 2007-7-1 16:35:34 > top of Java-index,Security,Cryptography...
# 2
Tarun,Thank you very much. This helps me a lot.Could you post the code for referencing?
cslhs at 2007-7-1 16:35:34 > top of Java-index,Security,Cryptography...
# 3
Isn't there a way to do this via JSSE in jdk1.4 ?ThanksMichael
b2n3m4 at 2007-7-1 16:35:34 > top of Java-index,Security,Cryptography...
# 4

yes there is:

/**

* Get the private key from a KeyStore in PKCS#12 format (*.PFX created

* by Microsoft IE/OE and others)

*/

public static PrivateKey getPrivateKeyFromPFX(String keyStore, char[] password) {

try {

FileInputStream fis = new FileInputStream(keyStore);

// supported KeyStore types (JDK1.4): PKCS12 and JKS (native Sun)

KeyStore ks = KeyStore.getInstance("PKCS12");

ks.load(fis, password);

for (Enumeration enum=ks.aliases(); enum.hasMoreElements(); ) {

String alias = (String)enum.nextElement();

if (ks.isKeyEntry(alias)) {

return (PrivateKey)ks.getKey(alias, password);

}

}

} catch (IOException ex) {

log.error(ex);

} catch (Exception ex) {

log.error(ex);

}

return null;

}

Ronald

Rmuller at 2007-7-1 16:35:34 > top of Java-index,Security,Cryptography...
# 5
Thank you very much for your information.But, how can i import pfx (private key/public key) file into keystore?In other words, how can i store this file into keystore?Can anyone help me out?
cjay75 at 2007-7-1 16:35:34 > top of Java-index,Security,Cryptography...