The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.security.cert  [9 examples] > Certificates  [5 examples]

e225. Importing a Certificate from a File

See also e224 Exporting a Certificate to a File.
    // This method reads a certificate to a file. The certificate can be either
    // binary or base64 encoded.
    public static java.security.cert.Certificate importCertificate(File file) {
        try {
            FileInputStream is = new FileInputStream(file);
    
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            java.security.cert.Certificate cert = cf.generateCertificate(is);
            return cert;
        } catch (CertificateException e) {
        } catch (IOException e) {
        }
        return null;
    }
A certificate can be imported into a keystore using keytool:
    > keytool -storepass my-keystore-password -alias myalias -import -file infilename.cer

 Related Examples
e223. Creating a New Key Pair and Self-Signed Certificate Using keytool
e224. Exporting a Certificate to a File
e226. Listing All Available Certificate Formats
e227. Getting the Subject and Issuer Distinguished Names of an X509 Certificate

See also: Certification Paths   


© 2002 Addison-Wesley.