org.bouncycastle.openssl.PEMWriter Java Examples

The following examples show how to use org.bouncycastle.openssl.PEMWriter. 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: CryptographicUtilities.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static String getStringFromKeyPair(PrivateKey privateKey, PublicKey publicKey) throws Exception {
	try (StringWriter stringWriter = new StringWriter()) {
		try (PEMWriter pemWriter = new PEMWriter(stringWriter)) {
			pemWriter.writeObject(privateKey);
			pemWriter.writeObject(publicKey);
		}
		return stringWriter.toString();
	} catch (Exception e) {
		throw new Exception("Cannot create key string: " + e.getMessage(), e);
	}
}
 
Example #4
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 #5
Source File: CertUtil.java    From nitmproxy with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static byte[] toPem(Object object) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try (PEMWriter writer = new PEMWriter(new OutputStreamWriter(outputStream))) {
        writer.writeObject(object);
        writer.flush();
        return outputStream.toByteArray();
    }
}
 
Example #6
Source File: CertUtil.java    From nitmproxy with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static byte[] toPem(Object... objects) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try (PEMWriter writer = new PEMWriter(new OutputStreamWriter(outputStream))) {
        for (Object object : objects) {
            writer.writeObject(object);
        }
        writer.flush();
        return outputStream.toByteArray();
    }
}
 
Example #7
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 #8
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 #9
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 #10
Source File: EbicsCertificateService.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
public String convertToPEMString(X509Certificate x509Cert) throws IOException {

    StringWriter sw = new StringWriter();
    try (PEMWriter pw = new PEMWriter(sw)) {
      pw.writeObject(x509Cert);
    }

    return sw.toString();
  }
 
Example #11
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 #12
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();
}