From 127841d6833d74f9ac8936767af16387a0abdb4a Mon Sep 17 00:00:00 2001 From: Michael Rasmussen Date: Tue, 5 Dec 2017 00:58:08 +0100 Subject: [PATCH 1/1] Initial commit --- .classpath | 6 ++ .gitignore | 1 + .project | 17 +++++ .../archi/libkeystore/KeyStoreInterface.java | 64 +++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 src/dk/cbs/archi/libkeystore/KeyStoreInterface.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..91f21ee --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + libkeystore + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/dk/cbs/archi/libkeystore/KeyStoreInterface.java b/src/dk/cbs/archi/libkeystore/KeyStoreInterface.java new file mode 100644 index 0000000..4cf74f4 --- /dev/null +++ b/src/dk/cbs/archi/libkeystore/KeyStoreInterface.java @@ -0,0 +1,64 @@ +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(); + } + } + +} -- 2.39.2