Java Key Public getPublicKey(String certificatePath)

Here you can find the source of getPublicKey(String certificatePath)

Description

get Public Key

License

Open Source License

Declaration

public static PublicKey getPublicKey(String certificatePath) throws Exception 

Method Source Code

//package com.java2s;

import java.io.FileInputStream;

import java.security.KeyStore;

import java.security.PublicKey;

import java.security.cert.Certificate;

import java.security.cert.CertificateFactory;

public class Main {
    public static final String KEY_STORE = "JKS";
    private static final String X509 = "X.509";

    public static PublicKey getPublicKey(String certificatePath) throws Exception {
        Certificate certificate = getCertificate(certificatePath);
        PublicKey key = certificate.getPublicKey();
        return key;
    }/*from w  ww  .  j ava 2  s  . com*/

    public static Certificate getCertificate(String certificatePath) throws Exception {
        CertificateFactory certificateFactory = CertificateFactory.getInstance(X509);
        FileInputStream in = new FileInputStream(certificatePath);

        Certificate certificate = certificateFactory.generateCertificate(in);
        in.close();

        return certificate;
    }

    public static Certificate getCertificate(String keyStorePath, String alias, String password) throws Exception {
        KeyStore ks = getKeyStore(keyStorePath, password);
        Certificate certificate = ks.getCertificate(alias);

        return certificate;
    }

    public static KeyStore getKeyStore(String keyStorePath, String password) throws Exception {
        FileInputStream is = new FileInputStream(keyStorePath);
        KeyStore ks = KeyStore.getInstance(KEY_STORE);
        ks.load(is, password.toCharArray());
        is.close();
        return ks;
    }
}

Related

  1. getPublicKey(KeyStore keyStore, String alias)
  2. getPublicKey(KeyStore keyStore, String alias, char[] password)
  3. getPublicKey(KeyStore ks, String alias, char[] password)
  4. getPublicKey(String algo)
  5. getPublicKey(String alias)
  6. getPublicKey(String certPath)
  7. getPublicKey(String filename)
  8. getPublicKey(String filename)
  9. getPublicKey(String filename)