Java Key Create getKeyStore(char[] password)

Here you can find the source of getKeyStore(char[] password)

Description

get Key Store

License

Open Source License

Declaration

public static KeyStore getKeyStore(char[] password) throws KeyStoreException, CertificateException 

Method Source Code


//package com.java2s;
import java.io.*;

import java.security.*;
import java.security.cert.*;

public class Main {
    private static final String KEY_STORE_TYPE = "JKS";

    public static KeyStore getKeyStore(char[] password) throws KeyStoreException, CertificateException {
        KeyStore ks = null;/*from  w  w w.j  av  a 2  s. com*/

        try {
            ks = getKeyStore(null, password, null, null, null);
        } catch (IOException ioe) {
            // Should not be thrown since no file is being loaded
        }

        return ks;
    }

    public static KeyStore getKeyStore(File keyFile, char[] password)
            throws KeyStoreException, CertificateException, IOException {
        return getKeyStore(keyFile, password, null, null, null);
    }

    public static KeyStore getKeyStore(char[] password, String keyAlias, PrivateKey privateKey,
            X509Certificate[] certList) throws KeyStoreException, CertificateException {
        KeyStore ks = null;

        try {
            ks = getKeyStore(null, password, keyAlias, privateKey, certList);
        } catch (IOException ioe) {
            // Should not be thrown since no file is being loaded
        }

        return ks;
    }

    public static KeyStore getKeyStore(File keyFile, char[] password, String keyAlias, PrivateKey privateKey,
            X509Certificate[] certList) throws KeyStoreException, CertificateException, IOException {

        KeyStore store = null;

        InputStream is = null;
        try {
            if (keyFile != null) {
                try {
                    is = new FileInputStream(keyFile);
                } catch (FileNotFoundException fnfe) {
                    // it's okay, just use null
                }
            }

            store = KeyStore.getInstance(KEY_STORE_TYPE);
            store.load(is, password);

            if (keyAlias != null && privateKey != null) {
                store.setKeyEntry(keyAlias, privateKey, password, certList);
            }
        } catch (NoSuchAlgorithmException nsae) {
            // Leave store as null
        } finally {
            if (is != null) {
                is.close();
            }
        }

        return store;

    }
}

Related

  1. getKeySize(PrivateKey key)
  2. getKeyStore()
  3. getKeyStore()
  4. getKeyStore(byte[] bytes, char[] password)
  5. getKeyStore(Certificate[] certificateChain, PrivateKey privateKey, char[] password)
  6. getKeyStore(File file, char[] storePass)
  7. getKeyStore(File keystore)
  8. getKeyStore(File keyStore)
  9. getKeyStore(final URL url, final String password)