Get the publickey from given cert file, the certificate Path stands for cert file, should under src source folder. - Java Security

Java examples for Security:Key

Description

Get the publickey from given cert file, the certificate Path stands for cert file, should under src source folder.

Demo Code


//package com.java2s;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

public class Main {


    public static final String X509_TYPE = "X.509";

    /**/*from w  ww  . ja  va 2  s  .  c o m*/
     * Get the publickey from given cert file, the certPath stands for cert file, should under src source folder.
     * @param certPath
     * @return
     * @throws Exception
     */
    public static PublicKey getPublicKeyByCert(String certPath)
            throws Exception {
        CertificateFactory certFactory = CertificateFactory
                .getInstance(X509_TYPE);
        Thread.currentThread().getContextClassLoader();
        Certificate certificate = certFactory
                .generateCertificate(ClassLoader
                        .getSystemResourceAsStream(certPath));
        return certificate.getPublicKey();
    }
}

Related Tutorials