Java Code Examples for org.bouncycastle.openssl.jcajce.JcaPEMWriter#close()

The following examples show how to use org.bouncycastle.openssl.jcajce.JcaPEMWriter#close() . 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: CertUtil.java    From littleca with Apache License 2.0 6 votes vote down vote up
/**
 * 保存公钥证书 base64编码
 *
 * @param cert
 * @param savePath
 * @throws Exception
 */
public static void saveX509CertBase64(final X509Certificate cert, String savePath) throws Exception {

    try {
        if (null == cert) {
            throw new CertException("cert can't be null");
        }
        if (null == savePath) {
            throw new CertException(" savePath can't be null");
        }
        JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(new FileWriter(savePath));
        jcaPEMWriter.writeObject(cert, null);
        jcaPEMWriter.close();
    } catch (Exception e) {
        throw new CertException("save cert failed", e);
    }
}
 
Example 2
Source File: CertUtil.java    From littleca with Apache License 2.0 6 votes vote down vote up
/**
 * 明文保存 publicKey
 *
 * @param publicKey
 * @param savePath
 * @throws CertException
 */
public static void savePublicKeyPem(final PublicKey publicKey, String savePath) throws CertException {
    try {
        if (null == publicKey) {
            throw new CertException("publicKey can't be null");
        }
        if (null == savePath) {
            throw new CertException(" savePath can't be null");
        }
        JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(new FileWriter(savePath));
        jcaPEMWriter.writeObject(publicKey);
        jcaPEMWriter.close();
    } catch (Exception e) {
        throw new CertException("save publicKey failed", e);
    }
}
 
Example 3
Source File: CertUtil.java    From littleca with Apache License 2.0 6 votes vote down vote up
/**
 * 明文保存 privateKey
 *
 * @param privateKey
 * @param savePath
 * @throws CertException
 */
public static void savePrivateKeyPem(final PrivateKey privateKey, String savePath) throws CertException {
    try {
        if (null == privateKey) {
            throw new CertException("privateKey can't be null");
        }
        if (null == savePath) {
            throw new CertException(" savePath can't be null");
        }
        JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(new FileWriter(savePath));
        jcaPEMWriter.writeObject(privateKey);
        jcaPEMWriter.close();
    } catch (Exception e) {
        throw new CertException("save privateKey failed", e);
    }
}
 
Example 4
Source File: CertUtil.java    From littleca with Apache License 2.0 6 votes vote down vote up
/**
 * 不支持sm2
 *
 * @param privateKey  私钥
 * @param savePath    保存路径
 * @param password    加密保存密码
 * @param encryptType 加密类型 默认DES-EDE3-CBC
 * @throws CertException
 */
public static void savePrivateKeyPem(PrivateKey privateKey, String savePath, String password, String encryptType)
        throws CertException {
    try {
        if (null == privateKey) {
            throw new CertException("privateKey can't be null");
        }
        if (null == password) {
            throw new CertException("password can't be null");
        }
        if (null == savePath) {
            throw new CertException("savePath can't be null");
        }
        if (null == encryptType) {
            encryptType = "DES-EDE3-CBC";
        }
        JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(new FileWriter(savePath));
        jcaPEMWriter.writeObject(privateKey, new JcePEMEncryptorBuilder(encryptType).build(password.toCharArray()));
        jcaPEMWriter.close();
    } catch (Exception e) {
        throw new CertException("save privateKey failed", e);
    }
}
 
Example 5
Source File: TLSCertificateKeyPair.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
/***
 * Creates a TLSCertificateKeyPair out of the given {@link X509Certificate} and {@link KeyPair}
 * encoded in PEM and also in DER for the certificate
 * @param x509Cert the certificate to process
 * @param keyPair  the key pair to process
 * @return a TLSCertificateKeyPair
 * @throws IOException upon failure
 */
static TLSCertificateKeyPair fromX509CertKeyPair(X509Certificate x509Cert, KeyPair keyPair) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintWriter writer = new PrintWriter(baos);
    JcaPEMWriter w = new JcaPEMWriter(writer);
    w.writeObject(x509Cert);
    w.flush();
    w.close();
    byte[] pemBytes = baos.toByteArray();

    InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(pemBytes));
    PemReader pr = new PemReader(isr);
    PemObject pem = pr.readPemObject();
    byte[] derBytes = pem.getContent();

    baos = new ByteArrayOutputStream();
    PrintWriter wr = new PrintWriter(baos);
    wr.println("-----BEGIN PRIVATE KEY-----");
    wr.println(new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded())));
    wr.println("-----END PRIVATE KEY-----");
    wr.flush();
    wr.close();
    byte[] keyBytes = baos.toByteArray();
    return new TLSCertificateKeyPair(pemBytes, derBytes, keyBytes);
}
 
Example 6
Source File: KeyPairUtil.java    From littleca with Apache License 2.0 5 votes vote down vote up
public final static String convertPublicKeyToPemString(PublicKey pub) {
    try {
        if (null == pub) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        OutputStreamWriter outWriter = new OutputStreamWriter(out);
        JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(outWriter);
        jcaPEMWriter.writeObject(pub);
        jcaPEMWriter.close();
        return new String(out.toByteArray());
    } catch (Exception e) {
        return null;
    }
}
 
Example 7
Source File: TlsHelper.java    From nifi with Apache License 2.0 5 votes vote down vote up
private static void outputAsPem(Object pemObj, String filename, File directory, String extension) throws IOException {
    OutputStream outputStream = new FileOutputStream(new File(directory,  TlsHelper.escapeFilename(filename) + extension));
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
    JcaPEMWriter pemWriter = new JcaPEMWriter(outputStreamWriter);
    JcaMiscPEMGenerator pemGen = new JcaMiscPEMGenerator(pemObj);
    pemWriter.writeObject(pemGen);
    pemWriter.close();
}
 
Example 8
Source File: FileHelper.java    From SAMLRaider with MIT License 5 votes vote down vote up
/**
 * Helper method for exporting PEM object.
 * 
 * @param object
 *            to export in PEM format.
 * @param filename
 *            for the file to export.
 */
public void exportPEMObject(Object pemObject, String filename) throws IOException {
	Writer writer;
	writer = new FileWriter(filename);
	JcaPEMWriter jcaPemWriter = new JcaPEMWriter(writer);
	jcaPemWriter.writeObject(pemObject);
	jcaPemWriter.flush();
	jcaPemWriter.close();
}
 
Example 9
Source File: CertificateFormatter.java    From credhub with Apache License 2.0 5 votes vote down vote up
public static String pemOf(final Object pemObject) throws IOException {
  final StringWriter sw = new StringWriter();
  final JcaPEMWriter writer = new JcaPEMWriter(sw);
  writer.writeObject(pemObject);
  writer.close();
  return sw.toString();
}
 
Example 10
Source File: PrivateKeyConverter.java    From jlogstash-input-plugin with Apache License 2.0 5 votes vote down vote up
private InputStream generatePkcs8(PrivateKey kp) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    logger.debug("Generate a Pkcs8 private key: " + kp.getFormat());


    StringWriter out = new StringWriter();
    JcaPEMWriter writer = new JcaPEMWriter(out);
    writer.writeObject(kp);
    writer.close();

    return new ByteArrayInputStream(out.toString().getBytes());
}
 
Example 11
Source File: CryptoPrimitives.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * certificationRequestToPEM - Convert a PKCS10CertificationRequest to PEM
 * format.
 *
 * @param csr The Certificate to convert
 * @return An equivalent PEM format certificate.
 * @throws IOException
 */

private String certificationRequestToPEM(PKCS10CertificationRequest csr) throws IOException {
    PemObject pemCSR = new PemObject("CERTIFICATE REQUEST", csr.getEncoded());

    StringWriter str = new StringWriter();
    JcaPEMWriter pemWriter = new JcaPEMWriter(str);
    pemWriter.writeObject(pemCSR);
    pemWriter.close();
    str.close();
    return str.toString();
}
 
Example 12
Source File: CertificateGenerator.java    From webapp-hardware-bridge with MIT License 5 votes vote down vote up
private static void saveKey(PrivateKey key, String keyPath) {
    try {
        JcaPEMWriter writer = new JcaPEMWriter(new FileWriter(new File(keyPath)));
        writer.writeObject(new JcaPKCS8Generator(key, null));
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 13
Source File: CertificateGenerator.java    From webapp-hardware-bridge with MIT License 5 votes vote down vote up
private static void saveCert(X509Certificate cert, String certificatePath) {
    try {
        JcaPEMWriter writer = new JcaPEMWriter(new FileWriter(new File(certificatePath)));
        writer.writeObject(cert);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 14
Source File: KeyPairUtil.java    From littleca with Apache License 2.0 5 votes vote down vote up
public final static String convertPrivateKeyToPemString(PrivateKey pri) {
    try {
        if (null == pri) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        OutputStreamWriter outWriter = new OutputStreamWriter(out);
        JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(outWriter);
        jcaPEMWriter.writeObject(pri);
        jcaPEMWriter.close();
        return new String(out.toByteArray());
    } catch (Exception e) {
        return null;
    }
}
 
Example 15
Source File: SslHelper.java    From dremio-flight-connector with Apache License 2.0 5 votes vote down vote up
private static InputStream keyToStream(PrivateKey key) throws IOException {
  final StringWriter writer = new StringWriter();
  final JcaPEMWriter pemWriter = new JcaPEMWriter(writer);
  pemWriter.writeObject(new PemObject("PRIVATE KEY", key.getEncoded()));
  pemWriter.flush();
  pemWriter.close();
  String pemString = writer.toString();
  return new ByteArrayInputStream(pemString.getBytes());
}
 
Example 16
Source File: FIPSTest.java    From snowflake-kafka-connector with Apache License 2.0 5 votes vote down vote up
public static String generateAESKey(PrivateKey key, char[] passwd) throws IOException, OperatorCreationException
{
  Security.addProvider(new BouncyCastleFipsProvider());
  StringWriter writer = new StringWriter();
  JcaPEMWriter pemWriter = new JcaPEMWriter(writer);
  PKCS8EncryptedPrivateKeyInfoBuilder pkcs8EncryptedPrivateKeyInfoBuilder =
    new JcaPKCS8EncryptedPrivateKeyInfoBuilder(key);
  pemWriter.writeObject(pkcs8EncryptedPrivateKeyInfoBuilder
    .build(new JcePKCSPBEOutputEncryptorBuilder(NISTObjectIdentifiers.id_aes256_CBC)
      .setProvider("BCFIPS").build(passwd)));
  pemWriter.close();
  return writer.toString();
}
 
Example 17
Source File: TestSslFlightEndpoint.java    From dremio-flight-connector with Apache License 2.0 5 votes vote down vote up
private static InputStream certsToStream(List<Certificate> certs) throws IOException {

    final StringWriter writer = new StringWriter();
    final JcaPEMWriter pemWriter = new JcaPEMWriter(writer);
    for (Certificate cert : certs) {
      pemWriter.writeObject(cert);
    }
    pemWriter.flush();
    pemWriter.close();
    String pemString = writer.toString();
    return new ByteArrayInputStream(pemString.getBytes());
  }
 
Example 18
Source File: TestSslFlightEndpoint.java    From dremio-flight-connector with Apache License 2.0 5 votes vote down vote up
private static void toFile(List<Certificate> certificates) throws IOException {
  File root = tempFolder.getRoot();
  Path certFile = Paths.get(root.getAbsolutePath(), "certs.pem");
  JcaPEMWriter writer = new JcaPEMWriter(new FileWriter(certFile.toFile()));
  for (final Certificate c : certificates) {
    writer.writeObject(c);
  }
  writer.close();
}
 
Example 19
Source File: SslHelper.java    From dremio-flight-connector with Apache License 2.0 5 votes vote down vote up
private static InputStream certsToStream(Certificate[] certs) throws IOException {

    final StringWriter writer = new StringWriter();
    final JcaPEMWriter pemWriter = new JcaPEMWriter(writer);
    for (Certificate cert : certs) {
      pemWriter.writeObject(cert);
    }
    pemWriter.flush();
    pemWriter.close();
    String pemString = writer.toString();
    return new ByteArrayInputStream(pemString.getBytes());
  }