Java KeyStore load from file

Description

Java KeyStore load from file

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.util.Enumeration;

public class Main {
   public static void main(String[] args) throws Exception {
      char[] password = "your password".toCharArray();
      KeyStore ks = KeyStore.getInstance("JKS");
      ks.load(new FileInputStream("your key file"), password);
      Enumeration e = ks.aliases();
      while (e.hasMoreElements()) {
         String alias = (String) e.nextElement();
         KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(password);
         KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(alias, protParam);

         PrivateKey pri = pkEntry.getPrivateKey();
         System.out.println(pri);
         java.security.cert.Certificate[] certs = pkEntry.getCertificateChain();
         for (java.security.cert.Certificate cert : certs)
            System.out.println(cert);
      }//from  ww w.  ja va  2 s  . c om
   }
}



PreviousNext

Related