Java Code Examples for org.bouncycastle.util.io.pem.PemWriter#flush()

The following examples show how to use org.bouncycastle.util.io.pem.PemWriter#flush() . 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: CertGen.java    From snowblossom with Apache License 2.0 6 votes vote down vote up
public static ByteString pemCode(byte[] encoded, String type)
{
  try
  {
    PemObject po = new PemObject(type, encoded);

    ByteArrayOutputStream b_out = new ByteArrayOutputStream();

    PemWriter w = new PemWriter( new OutputStreamWriter(b_out));

    w.writeObject(po);
    w.flush();
    w.close();

    return ByteString.copyFrom(b_out.toByteArray());
  }
  catch(java.io.IOException e)
  {
    throw new RuntimeException(e);
  }

}
 
Example 2
Source File: Ed25519PrivateKey.java    From hedera-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Write out a PEM encoded version of this private key.
 *
 * @deprecated for removal; exporting unencrypted PEMs is very insecure and has dubious
 * utility.
 */
@Deprecated
public void writePem(Writer out) throws IOException {
    final PemWriter pemWriter = new PemWriter(out);
    pemWriter.writeObject(new PemObject(PemUtils.TYPE_PRIVATE_KEY, encodeDER()));
    pemWriter.flush();
}
 
Example 3
Source File: PemUtils.java    From hedera-sdk-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
public static void writeEncryptedPrivateKey(PrivateKeyInfo pkInfo, Writer out, String passphrase) throws IOException {
    byte[] salt = CryptoUtils.randomBytes(CryptoUtils.SALT_LEN);

    KeyParameter derivedKey = CryptoUtils.deriveKeySha256(
        passphrase, salt, CryptoUtils.ITERATIONS, CryptoUtils.CBC_DK_LEN);

    byte[] iv = CryptoUtils.randomBytes(CryptoUtils.IV_LEN);

    Cipher cipher = CryptoUtils.initAesCbc128Encrypt(derivedKey, iv);

    byte[] encryptedKey = CryptoUtils.runCipher(cipher, pkInfo.getEncoded());

    // I wanted to just do this with BC's PKCS8Generator and KcePKCSPBEOutputEncryptorBuilder
    // but it tries to init AES instance of `Cipher` with a `PBKDF2Key` and the former complains

    // So this is basically a reimplementation of that minus the excess OO
    PBES2Parameters parameters = new PBES2Parameters(
        new KeyDerivationFunc(
            PKCSObjectIdentifiers.id_PBKDF2,
            new PBKDF2Params(
                salt,
                CryptoUtils.ITERATIONS,
                CryptoUtils.CBC_DK_LEN,
                new AlgorithmIdentifier(PKCSObjectIdentifiers.id_hmacWithSHA256))),
        new EncryptionScheme(NISTObjectIdentifiers.id_aes128_CBC,
            ASN1Primitive.fromByteArray(cipher.getParameters().getEncoded())));

    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(
        new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBES2, parameters),
        encryptedKey);

    PemWriter writer = new PemWriter(out);
    writer.writeObject(new PemObject(TYPE_ENCRYPTED_PRIVATE_KEY, encryptedPrivateKeyInfo.getEncoded()));
    writer.flush();
}
 
Example 4
Source File: PEMUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
public static String toPEM(X509Certificate certificate) throws IOException {
  StringWriter stringWriter = new StringWriter();
  PemWriter pemWriter = new PemWriter(stringWriter);
  try {
    pemWriter.writeObject(new JcaMiscPEMGenerator(certificate));
    pemWriter.flush();
  } finally {
    pemWriter.close();
  }
  return stringWriter.toString();
}
 
Example 5
Source File: PEMUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
public static String toPEM(PrivateKey privateKey) throws IOException {
  StringWriter stringWriter = new StringWriter();
  PemWriter pemWriter = new PemWriter(stringWriter);
  try {
    pemWriter.writeObject(new JcaMiscPEMGenerator(privateKey));
    pemWriter.flush();
  } finally {
    pemWriter.close();
  }
  return stringWriter.toString();
}
 
Example 6
Source File: KeycloakOauthPolicyTest.java    From apiman-plugins with Apache License 2.0 5 votes vote down vote up
private String certificateAsPem(X509Certificate x509) throws CertificateEncodingException, IOException {
    StringWriter sw = new StringWriter();
    PemWriter writer = new PemWriter(sw);
    PemObject pemObject = new PemObject("CERTIFICATE", x509.getEncoded());
    try {
        writer.writeObject(pemObject);
        writer.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        writer.close();
    }
    return sw.toString();
}
 
Example 7
Source File: KeycloakOauthPolicyLegacyTest.java    From apiman-plugins with Apache License 2.0 5 votes vote down vote up
private String certificateAsPem(X509Certificate x509) throws CertificateEncodingException, IOException {
    StringWriter sw = new StringWriter();
    PemWriter writer = new PemWriter(sw);
    PemObject pemObject = new PemObject("CERTIFICATE", x509.getEncoded());
    try {
        writer.writeObject(pemObject);
        writer.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        writer.close();
    }
    return sw.toString();
}