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

The following examples show how to use org.bouncycastle.util.io.pem.PemWriter#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: CryptoUtil.java    From julongchain with Apache License 2.0 6 votes vote down vote up
/**
 * 公钥文件生成
 * @param path
 * @param content
 */
public static void publicKeyFileGen(String path, byte[] content) {
    PemObject pemObject = new PemObject("PUBLIC KEY", content);
    StringWriter str = new StringWriter();
    PemWriter pemWriter = new PemWriter(str);
    try {
        pemWriter.writeObject(pemObject);
        pemWriter.close();
        str.close();
        PrintWriter pw = new PrintWriter(new FileOutputStream(path + PK));
        String publiKey = new String(str.toString());
        pw.print(publiKey);
        pw.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: CryptoUtil.java    From julongchain with Apache License 2.0 6 votes vote down vote up
/**
 * 私钥文件生成
 * @param path
 * @param content
 */
public static void privateKeyFileGen(String path, byte[] content) {
    PemObject pemObject = new PemObject("PRIVATE KEY", content);
    StringWriter str = new StringWriter();
    PemWriter pemWriter = new PemWriter(str);
    try {
        pemWriter.writeObject(pemObject);
        pemWriter.close();
        str.close();
        PrintWriter pw = new PrintWriter(new FileOutputStream(path + SK));
        String publiKey = new String(str.toString());
        pw.print(publiKey);
        pw.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 3
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 4
Source File: BCECUtil.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
private static String convertEncodedDataToPEM(String type, byte[] encodedData) throws IOException {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PemWriter pWrt = new PemWriter(new OutputStreamWriter(bOut));
    try {
        PemObject pemObj = new PemObject(type, encodedData);
        pWrt.writeObject(pemObj);
    } finally {
        pWrt.close();
    }
    return new String(bOut.toByteArray());
}
 
Example 5
Source File: BCECUtil.java    From littleca with Apache License 2.0 5 votes vote down vote up
private static String convertDerEcDataToPem(String type, byte[] encodedData) throws IOException {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PemWriter pWrt = new PemWriter(new OutputStreamWriter(bOut));
    try {
        PemObject pemObj = new PemObject(type, encodedData);
        pWrt.writeObject(pemObj);
    } finally {
        pWrt.close();
    }
    return new String(bOut.toByteArray());
}
 
Example 6
Source File: Util.java    From julongchain with Apache License 2.0 5 votes vote down vote up
public static void pemExport(String path, String pemType, byte[] bytes) throws JulongChainException {

        File file = new File(path);
        if (file.exists()) {
            if (!file.delete()) {
                throw new JulongChainException("deleted the file unsuccessfully");
            }
        } else {
            File dir = new File(file.getParent());
            if (!dir.exists()) {
                if (!dir.mkdirs()) {
                    throw new JulongChainException("made dir failed in method newCA");
                }
            }
        }

        try {
            if (!file.createNewFile()) {
                throw new JulongChainException("created new file unsuccessfully");
            }
            PemWriter pemWriter = new PemWriter(new OutputStreamWriter(new FileOutputStream(file)));
            PemObject pemObject = new PemObject(pemType, bytes);
            pemWriter.writeObject(pemObject);
            pemWriter.close();
        } catch (Exception e) {
            throw new JulongChainException("An error occurred on Util.pemExport :" + e.getMessage());
        }
    }
 
Example 7
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 8
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 9
Source File: BCECUtil.java    From jiguang-java-client-common with MIT License 5 votes vote down vote up
private static String convertEncodedDataToPEM(String type, byte[] encodedData) throws IOException {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    PemWriter pWrt = new PemWriter(new OutputStreamWriter(bOut));
    try {
        PemObject pemObj = new PemObject(type, encodedData);
        pWrt.writeObject(pemObj);
    } finally {
        pWrt.close();
    }
    return new String(bOut.toByteArray());
}
 
Example 10
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 11
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();
}
 
Example 12
Source File: Main.java    From keystore-decryptor with Apache License 2.0 5 votes vote down vote up
private static void showCert(KeystoreBlob ksBlob) throws Exception {
    X509Certificate cert = ksBlob.getCertificate();
    PemObject certPem = new PemObject("CERTIFICATE", cert.getEncoded());
    StringWriter sw = new StringWriter();
    PemWriter pemWriter = new PemWriter(sw);
    try {
        pemWriter.writeObject(certPem);
    } finally {
        pemWriter.close();
    }
    System.out.println(sw.toString());
}
 
Example 13
Source File: Main.java    From keystore-decryptor with Apache License 2.0 5 votes vote down vote up
private static void showJcaPrivateKey(PrivateKey pk) throws Exception {
    if (pk instanceof RSAPrivateKey) {
        RSAPrivateKey rsaPrivKey = (RSAPrivateKey) pk;
        PemObject rsaPem = new PemObject("RSA PRIVATE KEY", rsaPrivKey.getEncoded());
        StringWriter sw = new StringWriter();
        PemWriter pemWriter = new PemWriter(sw);
        try {
            pemWriter.writeObject(rsaPem);
        } finally {
            pemWriter.close();
        }
        System.out.println(sw.toString());
    } else if (pk instanceof java.security.interfaces.ECPrivateKey) {
        java.security.interfaces.ECPrivateKey ecPrivKey = (java.security.interfaces.ECPrivateKey) pk;
        System.out.printf("EC S: %s... (%d)\n",
                ecPrivKey.getS().toString(16).substring(0, 32),
                ecPrivKey.getS().bitLength());
        if (ecPrivKey.getParams() instanceof ECNamedCurveSpec) {
            ECNamedCurveSpec namedCurveSpec = (ECNamedCurveSpec) ecPrivKey.getParams();
            System.out.println("curve name: " + namedCurveSpec.getName());
        } else {
            System.out.println("EC params: " + ecPrivKey.getParams());
        }
    } else if (pk instanceof DSAPrivateKey) {
        DSAPrivateKey dsaPrivKey = (DSAPrivateKey) pk;
        System.out.printf("DSA X: %s... (%d)\n",
                dsaPrivKey.getX().toString(16).substring(0, 32), dsaPrivKey.getX()
                        .bitLength());
        System.out.println("DSA params: " + dsaPrivKey.getParams());
    } else {
        System.out.println("Unknown private key type: " + pk.getClass().getName());
    }
}
 
Example 14
Source File: PemFile.java    From bouncycastle-rsa-pem-write with MIT License 5 votes vote down vote up
public void write(String filename) throws FileNotFoundException, IOException {
	PemWriter pemWriter = new PemWriter(new OutputStreamWriter(new FileOutputStream(filename)));
	try {
		pemWriter.writeObject(this.pemObject);
	} finally {
		pemWriter.close();
	}
}