Example usage for org.bouncycastle.util.io.pem PemReader readPemObject

List of usage examples for org.bouncycastle.util.io.pem PemReader readPemObject

Introduction

In this page you can find the example usage for org.bouncycastle.util.io.pem PemReader readPemObject.

Prototype

public PemObject readPemObject() throws IOException 

Source Link

Document

Read the next PEM object as a blob of raw data with header information.

Usage

From source file:org.telegram.mtproto.Utilities.java

License:GNU General Public License

public static PrivateKey getPemPrivateKey(String filename, String algorithm) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader(filename));
    Security.addProvider(new BouncyCastleProvider());
    PemReader pp = new PemReader(br);
    PemObject pem = pp.readPemObject();
    byte[] content = pem.getContent();
    pp.close();// w w w .  j a v a 2s.c o  m

    PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(content);
    KeyFactory kf = KeyFactory.getInstance(algorithm);
    return kf.generatePrivate(spec);
}

From source file:org.telegram.mtproto.Utilities.java

License:GNU General Public License

public static PublicKey getPemPublicKey(String filename, String algorithm) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader(filename));
    Security.addProvider(new BouncyCastleProvider());
    PemReader pp = new PemReader(br);
    PemObject pem = pp.readPemObject();
    byte[] content = pem.getContent();
    pp.close();/*from  ww  w.  ja  v a 2s .com*/

    X509EncodedKeySpec spec = new X509EncodedKeySpec(content);
    KeyFactory kf = KeyFactory.getInstance(algorithm);
    return kf.generatePublic(spec);
}

From source file:pl.baczkowicz.spy.security.SecureSocketUtils.java

License:Open Source License

/**
 * Loads a PEM file from the specified location.
 * /* w w w  .  j  a  v a  2 s .com*/
 * @param file Location of the file to load
 * 
 * @return Content of the PEM file
 * 
 * @throws IOException Thrown when cannot read the file
 */
public static byte[] loadPemFile(final String file) throws IOException {
    final PemReader pemReader = new PemReader(new FileReader(file));
    final byte[] content = pemReader.readPemObject().getContent();
    pemReader.close();
    return content;
}

From source file:test.integ.be.fedict.trust.TSATest.java

License:Open Source License

private X509Certificate loadCertificate(String pemResourceName) throws IOException, CertificateException {
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    InputStream tsaCertInputStream = TSATest.class.getResourceAsStream(pemResourceName);
    PemReader pemReader = new PemReader(new InputStreamReader(tsaCertInputStream));
    PemObject pemObject = pemReader.readPemObject();
    X509Certificate certificate = (X509Certificate) certificateFactory
            .generateCertificate(new ByteArrayInputStream(pemObject.getContent()));
    pemReader.close();//  ww w. j a v  a2  s  .c o m
    return certificate;
}