Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read the content of a .pfx file in Java?

Tags:

java

pkcs#12

I have file.pfx file and also have a private key. How can I read the certificate in file.pfx in Java?

I have used this code:

import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.security.cert.CertificateException;
import javax.crypto.SecretKey;
import javax.security.auth.callback.*;
//These packages I have used.

public String readFile(String fn) { 
  String thisLine, ret = ""; 
  KeyStore ks = KeyStore.getInstance("pkcs12", "SunJSSE"); 
  ks.load(new FileInputStream(fn),"password".toCharArray()); 
  try { 
    Key key = ks.getKey("1", "password".toCharArray());
    Certificate[] cc = ks.getCertificateChain("1");
    X509Certificate certificate1 = (X509Certificate) cc[0];//Here it throws  java.lang.NullPointerException 
    ret += certificate1.getNotAfter(); 
    ret += certificate1.getNotBefore(); 
  } catch(Exception e) { 
    ret = "Cannot load, exception!";
  } 
  return ret; 
}
like image 469
Banshi Avatar asked Feb 28 '13 08:02

Banshi


People also ask

How do I view PFX contents?

The contents of a pfx file can be viewed in the GUI by right-clicking the PFX file and selecting Open (instead of the default action, Install). This will open mmc and show the pfx file as a folder. Open the pfx folder and the Certificates subfolder, and you will see the certificate(s) contained in the pfx.

Can Java use PFX file?

from a PFX file to a JKS file so that it can be used in the Java Key Store to set up WebLogic Server SSL. Sometimes the server certificate is in PFX format, and to utilize the same certificate in WebLogic Server, we need to export its certificates to a JKS file store.

Can I open a PFX file?

You can open a PFX file with the native program Microsoft Certificate Manager. This program is already installed on Windows computers, so you won't have to download anything new to use it.

How do I use a PFX file?

Start Windows Explorer and select and hold (or right-click) the . pfx file, then select Open to open the Certificate Import Wizard. Follow the procedure in the Certificate Import Wizard to import the code-signing certificate into the Personal certificate store.


1 Answers

Try This Code for Reading .pfx file:-

  public void checkExpire() {

        try {
            KeyManagerFactory kmf = javax.net.ssl.KeyManagerFactory.getInstance("SunX509");
            KeyStore keystore = KeyStore.getInstance("PKCS12");
            char[] password= "yourfilepassword".toCharArray();

            keystore.load(new FileInputStream("filepath\filename.pfx"),password);
            //keystore.load(new FileInputStream(certificate), password);
            kmf.init(keystore, psswd);
            Enumeration<String> aliases = keystore.aliases();
            while(aliases.hasMoreElements()){
                String alias = aliases.nextElement();
                if(keystore.getCertificate(alias).getType().equals("X.509")){
                Date expDate = ((X509Certificate) keystore.getCertificate(alias)).getNotAfter();
                Date fromDate= ((X509Certificate) keystore.getCertificate(alias)).getNotBefore();
        System.out.println("Expiray Date:-"+expDate );
        System.out.println("From Date:-"+fromDate);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
like image 192
Akshay Prabhu Avatar answered Sep 18 '22 12:09

Akshay Prabhu