]>
Commit | Line | Data |
---|---|---|
127841d6 MR |
1 | package dk.cbs.archi.libkeystore; |
2 | ||
3 | import java.io.FileInputStream; | |
4 | import java.io.FileNotFoundException; | |
5 | import java.io.IOException; | |
6 | import java.security.KeyStore; | |
7 | import java.security.KeyStoreException; | |
8 | import java.security.NoSuchAlgorithmException; | |
9 | import java.security.PrivateKey; | |
10 | import java.security.UnrecoverableKeyException; | |
11 | import java.security.cert.CertificateException; | |
12 | import java.security.cert.X509Certificate; | |
13 | ||
14 | public class KeyStoreInterface { | |
15 | ||
16 | private String path; | |
17 | private String password; | |
18 | ||
19 | public KeyStoreInterface() { | |
20 | this("", ""); | |
21 | } | |
22 | ||
23 | public KeyStoreInterface(String path) { | |
24 | this(path, ""); | |
25 | } | |
26 | ||
27 | public KeyStoreInterface(String path, String password) { | |
28 | this.path = path; | |
29 | this.password = password; | |
30 | } | |
31 | ||
32 | /** | |
33 | * | |
34 | * @param keyAlias | |
35 | * @param password | |
36 | * @throws Exception KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException | |
37 | */ | |
38 | public void getKey(String keyAlias, String password) throws Exception { | |
39 | KeyStore ks = KeyStore.getInstance("PKCS12"); | |
40 | FileInputStream fis = new FileInputStream(path); | |
41 | ks.load(fis, this.password.toCharArray()); | |
42 | if (ks.containsAlias(keyAlias)) { | |
43 | //KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) | |
44 | // ks.getEntry("privateKeyAlias", password); | |
45 | //PrivateKey myPrivateKey = pkEntry.getPrivateKey(); | |
46 | PrivateKey key = (PrivateKey) ks.getKey(keyAlias, null); | |
47 | if (key != null) | |
48 | System.out.println(key.getFormat()); | |
49 | X509Certificate certificate = (X509Certificate) ks.getCertificate(keyAlias); | |
50 | System.out.println(certificate.toString()); | |
51 | } | |
52 | } | |
53 | ||
54 | public static void main(String[] args) { | |
55 | KeyStoreInterface ksi = new KeyStoreInterface("/home/mir/git/libkeystore/keystore.jks", "changeit"); | |
56 | try { | |
57 | ksi.getKey("mykey", "password"); | |
58 | } catch (Exception e) { | |
59 | // TODO Auto-generated catch block | |
60 | e.printStackTrace(); | |
61 | } | |
62 | } | |
63 | ||
64 | } |