--- /dev/null
+package dk.cbs.archi.libkeystore;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
+public class KeyStoreInterface {
+
+ private String path;
+ private String password;
+
+ public KeyStoreInterface() {
+ this("", "");
+ }
+
+ public KeyStoreInterface(String path) {
+ this(path, "");
+ }
+
+ public KeyStoreInterface(String path, String password) {
+ this.path = path;
+ this.password = password;
+ }
+
+ /**
+ *
+ * @param keyAlias
+ * @param password
+ * @throws Exception KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException
+ */
+ public void getKey(String keyAlias, String password) throws Exception {
+ KeyStore ks = KeyStore.getInstance("PKCS12");
+ FileInputStream fis = new FileInputStream(path);
+ ks.load(fis, this.password.toCharArray());
+ if (ks.containsAlias(keyAlias)) {
+ //KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
+ // ks.getEntry("privateKeyAlias", password);
+ //PrivateKey myPrivateKey = pkEntry.getPrivateKey();
+ PrivateKey key = (PrivateKey) ks.getKey(keyAlias, null);
+ if (key != null)
+ System.out.println(key.getFormat());
+ X509Certificate certificate = (X509Certificate) ks.getCertificate(keyAlias);
+ System.out.println(certificate.toString());
+ }
+ }
+
+ public static void main(String[] args) {
+ KeyStoreInterface ksi = new KeyStoreInterface("/home/mir/git/libkeystore/keystore.jks", "changeit");
+ try {
+ ksi.getKey("mykey", "password");
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+}