Java Code Examples for org.bouncycastle.openssl.PEMWriter#writeObject()

The following examples show how to use org.bouncycastle.openssl.PEMWriter#writeObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SftpClient.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
private byte[] getPrivateKeyContent() throws Exception {

        try {
            KeyStore keyStore = SslUtils.loadKeystore(keyStoreFile, keyStorePassword);
            PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias, null);

            if (privateKey == null) {
                throw new Exception("The alias '" + keyAlias + "' does not point to an existing key-related entry");
            }

            StringWriter stringWriter = new StringWriter();
            PEMWriter pemWriter = new PEMWriter(stringWriter);
            pemWriter.writeObject(privateKey);
            pemWriter.close();

            byte[] privateKeyPEM = stringWriter.toString().getBytes();

            return privateKeyPEM;
        } catch (Exception e) {
            throw new Exception("Could not get private key content", e);
        }

    }
 
Example 2
Source File: PemUtils.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private static String encode(Object obj) {
    if (obj == null) {
        return null;
    }

    try {
        StringWriter writer = new StringWriter();
        PEMWriter pemWriter = new PEMWriter(writer);
        pemWriter.writeObject(obj);
        pemWriter.flush();
        pemWriter.close();
        String s = writer.toString();
        return PemUtils.removeBeginEnd(s);
    } catch (Exception e) {
        throw new PemException(e);
    }
}
 
Example 3
Source File: KeypairService.java    From Gatekeeper with Apache License 2.0 5 votes vote down vote up
public String getPEM(PrivateKey privKey) {

        StringWriter stringWriter = new StringWriter();
        PEMWriter pemWriter = new PEMWriter(stringWriter);
        try {
            pemWriter.writeObject(privKey);
            pemWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String privKeyString = stringWriter.toString();

        return privKeyString;

    }
 
Example 4
Source File: X509CertUtils.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
public static String generatePEMEncoded(Certificate cert) {
	StringWriter encoded = new StringWriter();
	PEMWriter pEMWriter = new PEMWriter(encoded);
	try {
		pEMWriter.writeObject(cert);
		pEMWriter.close();
		return encoded.toString();
	} catch (IOException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example 5
Source File: SignTask.java    From development with Apache License 2.0 5 votes vote down vote up
private void writeCertificate(Certificate... certificates)
        throws IOException {
    final PEMWriter writer = new PEMWriter(new FileWriter(destfile));
    for (final Certificate c : certificates) {
        writer.writeObject(c);
    }
    writer.close();
}
 
Example 6
Source File: SecureKeys.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/** returns the PEM (base64, ie for id_rsa) string for the private key / key pair;
 * this starts -----BEGIN PRIVATE KEY----- and ends similarly, like id_rsa.
 * also see {@link #readPem(byte[], String)} */
public static String toPem(KeyPair key) {
    try {
        StringWriter sw = new StringWriter();
        PEMWriter w = new PEMWriter(sw);
        w.writeObject(key);
        w.close();
        return sw.toString();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
 
Example 7
Source File: RSAVerifierTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testPemWriter() {
    PublicKey realmPublicKey = idpPair.getPublic();
    StringWriter sw = new StringWriter();
    PEMWriter writer = new PEMWriter(sw);
    try {
        writer.writeObject(realmPublicKey);
        writer.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    System.out.println(sw.toString());
}
 
Example 8
Source File: TestUtils.java    From fabric-sdk-java with Apache License 2.0 3 votes vote down vote up
public static String getPEMStringFromPrivateKey(PrivateKey privateKey) throws IOException {
    StringWriter pemStrWriter = new StringWriter();
    PEMWriter pemWriter = new PEMWriter(pemStrWriter);

    pemWriter.writeObject(privateKey);

    pemWriter.close();

    return pemStrWriter.toString();
}