Example usage for java.security KeyStore getEntry

List of usage examples for java.security KeyStore getEntry

Introduction

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

Prototype

public final Entry getEntry(String alias, ProtectionParameter protParam)
        throws NoSuchAlgorithmException, UnrecoverableEntryException, KeyStoreException 

Source Link

Document

Gets a keystore Entry for the specified alias with the specified protection parameter.

Usage

From source file:com.vimukti.accounter.developer.api.PublicKeyGenerator.java

/**
 * @param args//ww  w. j a  v  a2  s . c o  m
 * @throws NoSuchProviderException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws IOException
 * @throws CertificateException
 * @throws KeyStoreException
 * @throws UnrecoverableEntryException
 * @throws URISyntaxException
 */
public static void main(String[] args) throws KeyStoreException, NoSuchAlgorithmException, CertificateException,
        IOException, UnrecoverableEntryException {
    FileInputStream is = new FileInputStream(
            ServerConfiguration.getConfig() + File.separator + "license.keystore");
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    String password = "liceseStorePassword";
    keystore.load(is, password.toCharArray());
    PrivateKeyEntry entry = (PrivateKeyEntry) keystore.getEntry("licenseAlias",
            new PasswordProtection(ServerConfiguration.getLicenseKeystorePWD().toCharArray()));
    Key key = entry.getPrivateKey();
    System.out.println((PrivateKey) key);
    PublicKey publicKey = entry.getCertificate().getPublicKey();
    System.out.println(publicKey);

    byte[] encoded = publicKey.getEncoded();
    byte[] encodeBase64 = Base64.encodeBase64(encoded);
    System.out.println("Public Key:" + new String(encodeBase64));

}

From source file:Main.java

public static PrivateKey androidPrvKey(KeyStore keyStore, String alias) {
    try {//from  www .  java2 s .  c om
        KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);
        return privateKeyEntry.getPrivateKey();
    } catch (Exception expt) {
        expt.printStackTrace();
        throw new RuntimeException(expt);
    }
}

From source file:Main.java

public static PublicKey androidPubKey(KeyStore keyStore, String alias) {
    try {/*from  w w  w.  j  a  v a2s.  c  o  m*/
        KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);
        return privateKeyEntry.getCertificate().getPublicKey();
    } catch (Exception expt) {
        expt.printStackTrace();
        throw new RuntimeException(expt);
    }
}

From source file:Main.java

public static KeyStore.PrivateKeyEntry getThaliListenerKeyStoreEntry(KeyStore keyStore, char[] passPhrase)
        throws UnrecoverableEntryException, NoSuchAlgorithmException, KeyStoreException {
    return (KeyStore.PrivateKeyEntry) keyStore.getEntry(ThaliKeyAlias,
            new KeyStore.PasswordProtection(passPhrase));
}

From source file:org.xacml4j.saml.XACMLAuthzDecisionQueryEndpointTest.java

@BeforeClass
public static void init() throws Exception {
    DefaultBootstrap.bootstrap();/*w  ww  .  j a v  a 2 s. com*/
    KeyStore spKeyStore = getKeyStore("JCEKS", "/test-sp.jceks", "changeme");
    KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) spKeyStore.getEntry("mykey",
            new KeyStore.PasswordProtection("changeme".toCharArray()));
    spPrivateKey = entry.getPrivateKey();
    spPublicKey = (X509Certificate) entry.getCertificate();

    signer = new XACMLAuthzDecisionQuerySigner(spKeyStore, "mykey", "changeme");
}

From source file:be.fedict.eid.dss.sp.servlet.PkiServlet.java

public static KeyStore.PrivateKeyEntry getPrivateKeyEntry() throws Exception {

    LOG.debug("get SP private key entry");

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    KeyStore keyStore = KeyStore.getInstance("jks");
    InputStream keystoreStream = classLoader.getResourceAsStream("sp.jks");
    keyStore.load(keystoreStream, "secret".toCharArray());

    return (KeyStore.PrivateKeyEntry) keyStore.getEntry("sp",
            new KeyStore.PasswordProtection("secret".toCharArray()));
}

From source file:com.vangent.hieos.services.sts.util.STSUtil.java

/**
 *
 * @param stsConfig// ww  w .  j  a v a2s  .  c o  m
 * @param trustStore
 * @return
 * @throws STSException
 */
public static X509Certificate getIssuerCertificate(STSConfig stsConfig, KeyStore trustStore)
        throws STSException {
    X509Certificate certificate;
    try {
        String issuerAlias = stsConfig.getIssuerAlias();
        KeyStore.TrustedCertificateEntry tcEntry = (KeyStore.TrustedCertificateEntry) trustStore
                .getEntry(issuerAlias, null);
        certificate = (X509Certificate) tcEntry.getTrustedCertificate();
    } catch (Exception ex) {
        throw new STSException("Problem getting public certificate: " + ex.getMessage());
    }
    return certificate;
}

From source file:com.vangent.hieos.services.sts.util.STSUtil.java

/**
 *
 * @param stsConfig//from   w w w.j  ava 2s . c  o m
 * @param keyStore
 * @return
 * @throws STSException
 */
public static PrivateKeyEntry getIssuerPrivateKeyEntry(STSConfig stsConfig, KeyStore keyStore)
        throws STSException {
    //PrivateKey pk;
    PrivateKeyEntry pkEntry;
    try {
        String issuerAlias = stsConfig.getIssuerAlias();
        String issuerPassword = stsConfig.getIssuerPassword();
        pkEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(issuerAlias,
                new KeyStore.PasswordProtection(issuerPassword.toCharArray()));
        //pk = pkEntry.getPrivateKey();
        //certificate = (X509Certificate) pkEntry.getCertificate();
    } catch (Exception ex) {
        throw new STSException("Problem getting private key: " + ex.getMessage());
    }
    return pkEntry;
}

From source file:org.teknux.jettybootstrap.keystore.JettyKeystoreConvertorBuilder.java

private static PrivateKeyEntry getPrivateKeyEntryOfKeyStore(KeyStore keystore, String password, String alias)
        throws JettyKeystoreException {
    try {//from w  w w . j  ava 2 s .c  o  m
        if (alias == null) {
            Enumeration<String> aliasEnumeration = keystore.aliases();

            while (aliasEnumeration.hasMoreElements()) {
                String aliasItem = aliasEnumeration.nextElement();

                if (keystore.isKeyEntry(aliasItem)) {
                    Entry entry = keystore.getEntry(aliasItem,
                            new KeyStore.PasswordProtection(password.toCharArray()));

                    if (entry instanceof PrivateKeyEntry) {
                        return (PrivateKeyEntry) entry;
                    }
                }
            }

        } else {
            Entry entry = keystore.getEntry(alias, new KeyStore.PasswordProtection(password.toCharArray()));

            if (entry instanceof PrivateKeyEntry) {
                return (PrivateKeyEntry) entry;
            }
        }

        throw new JettyKeystoreException(JettyKeystoreException.ERROR_UNREACHABLE_PRIVATE_KEY_ENTRY,
                "Can not find private key entry");
    } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableEntryException e) {
        throw new JettyKeystoreException(JettyKeystoreException.ERROR_UNREACHABLE_PRIVATE_KEY_ENTRY,
                "Can not find private key entry", e);
    }
}

From source file:org.paxml.util.CryptoUtils.java

private static String getKey(KeyStore keyStore, String keyName, String keyPassword) {
    if (StringUtils.isBlank(keyName)) {
        keyName = DEFAULT_KEY_NAME;/*from www  . j  a v  a2  s  .c  om*/
    }
    if (keyPassword == null) {
        keyPassword = DEFAULT_KEY_PASSWORD;
    }
    PasswordProtection _keyPassword = new PasswordProtection(keyPassword.toCharArray());
    KeyStore.Entry entry;
    try {
        if (!keyStore.containsAlias(keyName)) {
            return null;
        }
        entry = keyStore.getEntry(keyName, _keyPassword);
    } catch (Exception e) {
        throw new PaxmlRuntimeException(e);
    }
    SecretKey key = ((KeyStore.SecretKeyEntry) entry).getSecretKey();
    try {
        return new String(key.getEncoded(), KEY_VALUE_ENCODING);
    } catch (UnsupportedEncodingException e) {
        throw new PaxmlRuntimeException(e);
    }

}