Example usage for java.security KeyStore getKey

List of usage examples for java.security KeyStore getKey

Introduction

In this page you can find the example usage for java.security KeyStore getKey.

Prototype

public final Key getKey(String alias, char[] password)
        throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException 

Source Link

Document

Returns the key associated with the given alias, using the given password to recover it.

Usage

From source file:org.tolven.gatekeeper.CertificateHelper.java

public static void changeKeyStorePassword(KeyStore keyStore, String alias, char[] oldPassword,
        char[] newPassword) {
    try {//  w w  w . jav a 2s  .  co m
        PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, oldPassword);
        keyStore.setKeyEntry(alias, privateKey, newPassword, keyStore.getCertificateChain(alias));
    } catch (GeneralSecurityException ex) {
        throw new RuntimeException("Could not change the keystore password for with alias: " + alias, ex);
    }
}

From source file:com.hhi.bigdata.platform.push.client.RegisterUtil.java

/**
 * <pre>/*w w w  . j a v a 2 s .co m*/
 * create a SSLSocketFactory instance with given parameters
 * </pre>
 * @param keystore
 * @param password
 * @return
 * @throws IOException
 */
private static PrivateKey getPrivateKey(KeyStore keystore, String password) throws Exception {
    Key key = null;

    // List the aliases
    Enumeration<String> aliases = keystore.aliases();
    while (aliases.hasMoreElements()) {
        String alias = (String) aliases.nextElement();

        if (keystore.isKeyEntry(alias)) {
            key = keystore.getKey(alias, password.toCharArray());
        }
    }

    return (PrivateKey) key;
}

From source file:org.forgerock.openam.doc.jwt.bearer.Main.java

private static PrivateKey getPrivateKey() {
    PrivateKey privateKey = null;

    try {/*from www . j av  a 2  s  .  c  om*/
        KeyStore keystore = KeyStore.getInstance("JKS");
        keystore.load(Main.class.getResourceAsStream(keystorePath), keystorePass.toCharArray());
        privateKey = (PrivateKey) keystore.getKey(keystoreAlias, keyPass.toCharArray());
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }

    if (privateKey == null) {
        System.err.println("Failed to retrieve private key from " + keystorePath);
        System.exit(-1);
    }

    return privateKey;
}

From source file:com.aqnote.shared.cryptology.cert.util.KeyStoreFileUtil.java

/**
 * ?PFX?/*  ww w  . j av a  2 s  .c  o m*/
 * 
 * @param alias ??
 * @param pfxPath PFX
 * @param password ?
 * @return
 * @throws KeyStoreException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 * @throws IOException
 * @throws UnrecoverableKeyException
 */
public static PrivateKey readPrivateKey(String alias, String pfxPath, String password) throws KeyStoreException,
        NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {

    KeyStore keyStore = KeyStore.getInstance("pkcs12");

    FileInputStream fis = null;

    fis = new FileInputStream(pfxPath);

    keyStore.load(fis, password.toCharArray());

    fis.close();

    return (PrivateKey) keyStore.getKey(alias, password.toCharArray());
}

From source file:com.blackducksoftware.tools.commonframework.core.encryption.Password.java

/**
 * Get the key from the KeyStore.//from w  w  w  . j a  v  a2  s  . c  om
 *
 * @param keyStoreFilename
 * @param keypass
 * @throws IOException
 * @throws UnrecoverableKeyException
 * @throws KeyStoreException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 */
private static Key getKeyFromStore(final String keyStoreFilename, final char[] keypass) throws IOException,
        UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException {
    final InputStream keyStoreInputStream = thisClass.getResourceAsStream(keyStoreFilename); // Try the Sun KeyStore
    // first
    if (keyStoreInputStream == null) {
        throw new IOException("Unable to locate key store file " + keyStoreFilename); // File should always be found
    }
    Key key = null;
    try {
        final KeyStore keystore = KeyStore.getInstance(KEYSTORE_TYPE);
        keystore.load(keyStoreInputStream, keypass);
        key = keystore.getKey(KEY_ALIAS, keypass);
    } finally {
        if (keyStoreInputStream != null) {
            keyStoreInputStream.close();
        }
    }
    return key;
}

From source file:org.wso2.carbon.connector.CreateJWT.java

/**
 * The method is using to extract the private key from the p12 file
 *
 * @param keyFile the p12 file to extract the private key
 * @param password the password to extract the p12 file
 * @return the private key/*www.  jav  a  2 s .  c o  m*/
 * @throws KeyStoreException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 * @throws UnrecoverableKeyException
 */
private static PrivateKey getPrivateKey(String keyFile, String password)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
    KeyStore keystore = KeyStore.getInstance(JWTConstant.KEY_STORE);
    try {
        keystore.load(new FileInputStream(keyFile), password.toCharArray());
    } catch (IOException e) {
        log.error("PKCS12 file not found in the location : " + e.getMessage(), e);
        throw new SynapseException("PKCS12 file not found in the location : " + e.getMessage(), e);
    }
    return (PrivateKey) keystore.getKey(keyAlias, password.toCharArray());
}

From source file:mitm.common.security.certificate.GenerateBulkPFX.java

private static void loadCA() throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException,
        CertificateException, IOException, UnrecoverableKeyException {
    KeyStore caKeyStore = securityFactory.createKeyStore("PKCS12");

    File file = new File("test/resources/testdata/keys/testCA.p12");

    FileInputStream input = new FileInputStream(file);

    caKeyStore.load(input, "test".toCharArray());

    rootCertificate = (X509Certificate) caKeyStore.getCertificate("root");
    caCertificate = (X509Certificate) caKeyStore.getCertificate("ca");
    caPrivateKey = (PrivateKey) caKeyStore.getKey("ca", null);

    assertNotNull(caCertificate);/*w  ww. ja v  a  2s. c  o m*/
    assertNotNull(caPrivateKey);
}

From source file:cn.mrdear.pay.util.RSAUtils.java

/**
 * ?//from w w w  . ja va  2 s . c  o m
 * 
 * @param type
 *            
 * @param inputStream
 *            ?
 * @param password
 *            ?
 * @return 
 */
public static Key getKey(String type, InputStream inputStream, String password) {
    Assert.isNotEmpty(type);
    Assert.notNull(inputStream);

    try {
        KeyStore keyStore = KeyStore.getInstance(type, PROVIDER);
        keyStore.load(inputStream, password != null ? password.toCharArray() : null);
        String alias = keyStore.aliases().hasMoreElements() ? keyStore.aliases().nextElement() : null;
        return keyStore.getKey(alias, password != null ? password.toCharArray() : null);
    } catch (KeyStoreException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (CertificateException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (UnrecoverableKeyException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:net.firejack.platform.web.security.x509.KeyUtils.java

public static KeyPair load(File keyStoreFile) {
    if (keyStoreFile != null) {
        try {/*from w w w.j  a v  a  2 s  .c o  m*/
            KeyStore ks = KeyStore.getInstance("JKS", "SUN");
            if (keyStoreFile.exists()) {
                FileInputStream stream = new FileInputStream(keyStoreFile);
                ks.load(stream, SECRET);
                IOUtils.closeQuietly(stream);

                PrivateKey privateKey = (PrivateKey) ks.getKey(ALIAS, SECRET);
                if (privateKey == null)
                    return null;
                PublicKey publicKey = ks.getCertificate(ALIAS).getPublicKey();
                return new KeyPair(publicKey, privateKey);
            }
        } catch (Throwable th) {
            logger.error("Failed to initialize key store");
            throw new OpenFlameRuntimeException(th.getMessage(), th);
        }
    } else {
        throw new IllegalArgumentException("Key Store file should not be null.");
    }
    return null;
}

From source file:mitm.common.security.crl.GenerateTestCRLs.java

private static void loadCA() throws Exception {
    KeyStore caKeyStore = securityFactory.createKeyStore("PKCS12");

    File file = new File("test/resources/testdata/keys/testCA.p12");

    FileInputStream input = new FileInputStream(file);

    caKeyStore.load(input, "test".toCharArray());

    caCertificate = (X509Certificate) caKeyStore.getCertificate("ca");
    caPrivateKey = (PrivateKey) caKeyStore.getKey("ca", null);

    rootCertificate = (X509Certificate) caKeyStore.getCertificate("root");
    rootPrivateKey = (PrivateKey) caKeyStore.getKey("root", null);

    assertNotNull(caCertificate);//ww  w  .  ja  v  a  2 s  . c o m
    assertNotNull(caPrivateKey);
}