Java KeyStore get keys from file

Description

Java KeyStore get keys from file

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.KeyStore;

public class Main {
   public static void main(String[] args) throws Exception {
      char[] password = "123456".toCharArray();
      KeyStore ks = KeyStore.getInstance("JKS");
      ks.load(new FileInputStream("your key file"), password);

      String alias = "mykey";
      FileOutputStream kos = new FileOutputStream("key.der");
      Key pri = ks.getKey(alias, password);
      kos.write(pri.getEncoded());//from ww  w.java 2s . co  m

      FileOutputStream cos = new FileOutputStream("cert.der");
      java.security.cert.Certificate cert = ks.getCertificate(alias);
      cos.write(cert.getEncoded());
   }
}



PreviousNext

Related