org.bouncycastle.util.io.pem.PemWriter Java Examples
The following examples show how to use
org.bouncycastle.util.io.pem.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: KeyCodec.java From hadoop-ozone with Apache License 2.0 | 6 votes |
/** * Writes a given private key using the default config options. * * @param key - Key to write to file. * @throws IOException - On I/O failure. */ public void writePrivateKey(PrivateKey key) throws IOException { File privateKeyFile = Paths.get(location.toString(), securityConfig.getPrivateKeyFileName()).toFile(); if (Files.exists(privateKeyFile.toPath())) { throw new IOException("Private key already exist."); } try (PemWriter privateKeyWriter = new PemWriter(new FileWriterWithEncoding(privateKeyFile, DEFAULT_CHARSET))) { privateKeyWriter.writeObject( new PemObject(PRIVATE_KEY, key.getEncoded())); } Files.setPosixFilePermissions(privateKeyFile.toPath(), permissionSet); }
Example #2
Source File: KeyCodec.java From hadoop-ozone with Apache License 2.0 | 6 votes |
/** * Writes a given public key using the default config options. * * @param key - Key to write to file. * @throws IOException - On I/O failure. */ public void writePublicKey(PublicKey key) throws IOException { File publicKeyFile = Paths.get(location.toString(), securityConfig.getPublicKeyFileName()).toFile(); if (Files.exists(publicKeyFile.toPath())) { throw new IOException("Private key already exist."); } try (PemWriter keyWriter = new PemWriter(new FileWriterWithEncoding(publicKeyFile, DEFAULT_CHARSET))) { keyWriter.writeObject( new PemObject(PUBLIC_KEY, key.getEncoded())); } Files.setPosixFilePermissions(publicKeyFile.toPath(), permissionSet); }
Example #3
Source File: KeyCodec.java From hadoop-ozone with Apache License 2.0 | 6 votes |
/** * Helper function that actually writes data to the files. * * @param basePath - base path to write key * @param keyPair - Key pair to write to file. * @param privateKeyFileName - private key file name. * @param publicKeyFileName - public key file name. * @param force - forces overwriting the keys. * @throws IOException - On I/O failure. */ private synchronized void writeKey(Path basePath, KeyPair keyPair, String privateKeyFileName, String publicKeyFileName, boolean force) throws IOException { checkPreconditions(basePath); File privateKeyFile = Paths.get(location.toString(), privateKeyFileName).toFile(); File publicKeyFile = Paths.get(location.toString(), publicKeyFileName).toFile(); checkKeyFile(privateKeyFile, force, publicKeyFile); try (PemWriter privateKeyWriter = new PemWriter(new FileWriterWithEncoding(privateKeyFile, DEFAULT_CHARSET))) { privateKeyWriter.writeObject( new PemObject(PRIVATE_KEY, keyPair.getPrivate().getEncoded())); } try (PemWriter publicKeyWriter = new PemWriter(new FileWriterWithEncoding(publicKeyFile, DEFAULT_CHARSET))) { publicKeyWriter.writeObject( new PemObject(PUBLIC_KEY, keyPair.getPublic().getEncoded())); } Files.setPosixFilePermissions(privateKeyFile.toPath(), permissionSet); Files.setPosixFilePermissions(publicKeyFile.toPath(), permissionSet); }
Example #4
Source File: CertGen.java From snowblossom with Apache License 2.0 | 6 votes |
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 #5
Source File: CryptoUtil.java From julongchain with Apache License 2.0 | 6 votes |
/** * 公钥文件生成 * @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 #6
Source File: CryptoUtil.java From julongchain with Apache License 2.0 | 6 votes |
/** * 私钥文件生成 * @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 #7
Source File: MessageStatusCli.java From protect with MIT License | 6 votes |
private static void writeObject(final Key key, final PemWriter writer) throws IOException { final String description; if (key instanceof RSAPrivateKey) { description = "PAILLIER PRIVATE KEY"; } else if (key instanceof RSAPublicKey) { description = "PAILLIER PUBLIC KEY"; } else if (key instanceof ECPrivateKey) { description = "EC PRIVATE KEY"; } else if (key instanceof ECPublicKey) { description = "EC PUBLIC KEY"; } else if (key instanceof EdDSAPrivateKey) { description = "ED25519 PRIVATE KEY"; } else if (key instanceof EdDSAPublicKey) { description = "ED25519 PUBLIC KEY"; } else if (key instanceof PrivateKey) { description = "PRIVATE KEY"; } else if (key instanceof PublicKey) { description = "PUBLIC KEY"; } else { description = "KEY"; } writer.writeObject(new PemObject(description, key.getEncoded())); }
Example #8
Source File: NiFiRestConfigurationProviderSecureTest.java From nifi-minifi with Apache License 2.0 | 5 votes |
/** * Generates certificates with the tls-toolkit and then starts up the docker compose file */ @BeforeClass public static void initCertificates() throws Exception { certificatesDirectory = Paths.get(NiFiRestConfigurationProviderSecureTest.class.getClassLoader() .getResource("docker-compose-NiFiRestConfigurationProviderSecureTest.yml").getFile()).getParent().toAbsolutePath().resolve("certificates-NiFiRestConfigurationProviderSecureTest"); trustSslContext = initCertificates(certificatesDirectory, Arrays.asList("c2", "mocknifi")); healthCheckSocketFactory = trustSslContext.getSocketFactory(); KeyStore mockNiFiKeyStore = KeyStore.getInstance("JKS"); try (InputStream inputStream = Files.newInputStream(certificatesDirectory.resolve("mocknifi").resolve("keystore.jks"))) { mockNiFiKeyStore.load(inputStream, "badKeystorePass".toCharArray()); } try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(certificatesDirectory.resolve("mocknifi").resolve("cert.pem"))))) { pemWriter.writeObject(new JcaMiscPEMGenerator(mockNiFiKeyStore.getKey(TlsToolkitStandalone.NIFI_KEY, "badKeyPass".toCharArray()))); for (Certificate certificate : mockNiFiKeyStore.getCertificateChain(TlsToolkitStandalone.NIFI_KEY)) { pemWriter.writeObject(new JcaMiscPEMGenerator(certificate)); } } KeyStore mockNiFiTrustStore = KeyStore.getInstance("JKS"); try (InputStream inputStream = Files.newInputStream(certificatesDirectory.resolve("mocknifi").resolve("truststore.jks"))) { mockNiFiTrustStore.load(inputStream, "badTrustPass".toCharArray()); } try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(Files.newOutputStream(certificatesDirectory.resolve("mocknifi").resolve("ca.pem"))))) { pemWriter.writeObject(new JcaMiscPEMGenerator(mockNiFiTrustStore.getCertificate(TlsToolkitStandalone.NIFI_CERT))); } docker.before(); }
Example #9
Source File: CertUtils.java From cloudstack with Apache License 2.0 | 5 votes |
public static String publicKeyToPem(final PublicKey key) throws IOException { final PemObject pemObject = new PemObject("PUBLIC KEY", key.getEncoded()); final StringWriter sw = new StringWriter(); try (final PemWriter pw = new PemWriter(sw)) { pw.writeObject(pemObject); } return sw.toString(); }
Example #10
Source File: IOManager.java From acme_client with MIT License | 5 votes |
public static void writeX509CertificateChain(X509Certificate[] certificates, String path) throws IOException, CertificateEncodingException { try (Writer writer = new FileWriter(path); PemWriter pemWriter = new PemWriter(writer)) { for (X509Certificate certificate : certificates) { pemWriter.writeObject(new PemObject("CERTIFICATE", certificate.getEncoded())); } } }
Example #11
Source File: PEMUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
public static byte[] toPEM(PKCS10CertificationRequest csr) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8))) { pemWriter.writeObject(new JcaMiscPEMGenerator(csr)); pemWriter.flush(); } return os.toByteArray(); }
Example #12
Source File: PEMUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
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 #13
Source File: PEMUtils.java From dcos-commons with Apache License 2.0 | 5 votes |
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 #14
Source File: GenerateKeyHandler.java From webpush-java with MIT License | 5 votes |
/** * Write the given key to the given file. * * @param key * @param file */ private void writeKey(Key key, File file) throws IOException { file.createNewFile(); try (PemWriter pemWriter = new PemWriter(new OutputStreamWriter(new FileOutputStream(file)))) { PemObject pemObject = new PemObject("Key", key.getEncoded()); pemWriter.writeObject(pemObject); } }
Example #15
Source File: BCECUtil.java From jiguang-java-client-common with MIT License | 5 votes |
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 #16
Source File: KeycloakOauthPolicyTest.java From apiman-plugins with Apache License 2.0 | 5 votes |
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 #17
Source File: KeycloakOauthPolicyLegacyTest.java From apiman-plugins with Apache License 2.0 | 5 votes |
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 #18
Source File: DSSUtils.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
private static String convertToPEM(Object obj) { try (StringWriter out = new StringWriter(); PemWriter pemWriter = new PemWriter(out)) { pemWriter.writeObject(new JcaMiscPEMGenerator(obj)); pemWriter.flush(); return out.toString(); } catch (Exception e) { throw new DSSException("Unable to convert DER to PEM", e); } }
Example #19
Source File: DefaultExplorePanel.java From zap-extensions with Apache License 2.0 | 5 votes |
private void writePubCertificateToFile(File f) throws IOException, KeyStoreException { OptionsParam options = Model.getSingleton().getOptionsParam(); DynSSLParam param = options.getParamSet(DynSSLParam.class); KeyStore ks = param.getRootca(); if (ks != null) { final Certificate cert = ks.getCertificate(SslCertificateService.ZAPROXY_JKS_ALIAS); try (final Writer w = Files.newBufferedWriter(f.toPath(), StandardCharsets.US_ASCII); final PemWriter pw = new PemWriter(w)) { pw.writeObject(new JcaMiscPEMGenerator(cert)); pw.flush(); } } }
Example #20
Source File: Main.java From keystore-decryptor with Apache License 2.0 | 5 votes |
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 #21
Source File: Main.java From keystore-decryptor with Apache License 2.0 | 5 votes |
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 #22
Source File: CertificateManager.java From Openfire with Apache License 2.0 | 5 votes |
/** * Generates a PEM representation of the input argument. * * @param object the input argument (cannot be null). * @return PEM representation of the input argument. * @throws IOException When a PEM representation of the input could not be created. */ public static String toPemRepresentation( Object object ) throws IOException { final StringWriter result = new StringWriter(); try ( final PemWriter pemWriter = new PemWriter(result) ) { final PemObjectGenerator objGen = new JcaMiscPEMGenerator ( object ); pemWriter.writeObject( objGen ); } return result.toString(); }
Example #23
Source File: PemFile.java From bouncycastle-rsa-pem-write with MIT License | 5 votes |
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(); } }
Example #24
Source File: X509Bridge.java From desktopclient-java with GNU General Public License v3.0 | 5 votes |
public static byte[] encode(X509Certificate cert) throws CertificateEncodingException, IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); try (PemWriter writer = new PemWriter(new OutputStreamWriter(stream))) { writer.writeObject(new PemObject(X509Bridge.PEM_TYPE_CERTIFICATE, cert.getEncoded())); } return stream.toByteArray(); }
Example #25
Source File: TlsClientManager.java From nifi with Apache License 2.0 | 5 votes |
@Override public void write(OutputStreamFactory outputStreamFactory) throws IOException, GeneralSecurityException { super.write(outputStreamFactory); String trustStorePassword = tlsClientConfig.getTrustStorePassword(); boolean trustStorePasswordGenerated = false; if (StringUtils.isEmpty(trustStorePassword)) { trustStorePassword = getPasswordUtil().generatePassword(); trustStorePasswordGenerated = true; } trustStorePassword = TlsHelper.writeKeyStore(trustStore, outputStreamFactory, new File(tlsClientConfig.getTrustStore()), trustStorePassword, trustStorePasswordGenerated); tlsClientConfig.setTrustStorePassword(trustStorePassword); for (ConfigurationWriter<TlsClientConfig> configurationWriter : configurationWriters) { configurationWriter.write(tlsClientConfig, outputStreamFactory); } if (certificateAuthorityDirectory != null) { // Write out all trusted certificates from truststore for (String alias : Collections.list(trustStore.aliases())) { try { KeyStore.Entry trustStoreEntry = trustStore.getEntry(alias, null); if (trustStoreEntry instanceof KeyStore.TrustedCertificateEntry) { Certificate trustedCertificate = ((KeyStore.TrustedCertificateEntry) trustStoreEntry).getTrustedCertificate(); try (OutputStream outputStream = outputStreamFactory.create(new File(certificateAuthorityDirectory, TlsHelper.escapeFilename(alias) + ".pem")); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); PemWriter pemWriter = new PemWriter(outputStreamWriter)) { pemWriter.writeObject(new JcaMiscPEMGenerator(trustedCertificate)); } } } catch (UnrecoverableEntryException e) { // Ignore, not a trusted cert } } } }
Example #26
Source File: TlsHelper.java From nifi with Apache License 2.0 | 5 votes |
public static String pemEncodeJcaObject(Object object) throws IOException { StringWriter writer = new StringWriter(); try (PemWriter pemWriter = new PemWriter(writer)) { pemWriter.writeObject(new JcaMiscPEMGenerator(object)); } return writer.toString(); }
Example #27
Source File: CertUtils.java From cloudstack with Apache License 2.0 | 5 votes |
public static String privateKeyToPem(final PrivateKey key) throws IOException { final PemObject pemObject = new PemObject("PRIVATE KEY", key.getEncoded()); final StringWriter sw = new StringWriter(); try (final PemWriter pw = new PemWriter(sw)) { pw.writeObject(pemObject); } return sw.toString(); }
Example #28
Source File: SshKeyUtils.java From onedev with MIT License | 5 votes |
public static String generatePEMPrivateKey() { try (StringWriter privateWriter = new StringWriter(); PemWriter privatePemWriter = new PemWriter(privateWriter)) { KeyPair keyPair = KeyUtils.generateKeyPair("ssh-rsa", 4096); privatePemWriter.writeObject(new PemObject("RSA PRIVATE KEY", keyPair.getPrivate().getEncoded())); privatePemWriter.flush(); return privateWriter.toString(); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException(e); } }
Example #29
Source File: PKCS12CertExtractor.java From onedev with MIT License | 5 votes |
private String getCertContent(Certificate cert) { StringWriter stringWriter = new StringWriter(); try (PemWriter pemWriter = new PemWriter(stringWriter)) { pemWriter.writeObject(new PemObject("CERTIFICATE", cert.getEncoded())); pemWriter.flush(); } catch (CertificateEncodingException|IOException e) { throw new RuntimeException(e); } return stringWriter.toString().trim(); }
Example #30
Source File: Ed25519PrivateKey.java From hedera-sdk-java with Apache License 2.0 | 5 votes |
/** * 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(); }