Refine search
private static String toPem(X509Certificate certificate) throws IOException { StringWriter stringWriter = new StringWriter(); PEMWriter pemWriter = new PEMWriter(stringWriter, BouncyCastleProvider.PROVIDER_NAME); pemWriter.writeObject(certificate); pemWriter.close(); return stringWriter.toString(); } }
private CharSequence asPEM( String ilk, Object object ) { try { StringWriter sw = new StringWriter(); PEMWriter pemWriter = new PEMWriter( sw, cryptoContext.providerName() ); pemWriter.writeObject( object ); pemWriter.flush(); return sw.getBuffer(); } catch ( IOException ex ) { throw new CryptoFailure( "Unable to write " + ilk + " as PEM", ex ); } }
public static void saveCertificate(Certificate cert, Writer to) throws IOException { try (PEMWriter pw = new PEMWriter(to)) { pw.writeObject(cert); } }
public static void pemEncodeToFile(String filename, Object obj, char[] password) throws Exception{ PEMWriter pw = new PEMWriter(new FileWriter(filename)); if (password != null && password.length > 0) { pw.writeObject(obj, "DESEDE", password, new SecureRandom()); } else { pw.writeObject(obj); } pw.flush(); pw.close(); }
StringWriter sw = new StringWriter(); PEMWriter pem = new PEMWriter(sw); try { pem.writeObject(key); System.out.printf("IOException: %s%n", e); return sw.toString();
PemObject pemObject = new PemObject("CERTIFICATE REQUEST", certRequest.getEncoded()); StringWriter str = new StringWriter(); PEMWriter pemWriter = new PEMWriter(str); pemWriter.writeObject(pemObject); pemWriter.close(); str.close(); System.out.println(str);
@SuppressWarnings("deprecation") @Override protected void doWriteKeyPair(String resourceKey, KeyPair kp, OutputStream outputStream) throws IOException, GeneralSecurityException { try (org.bouncycastle.openssl.PEMWriter w = new org.bouncycastle.openssl.PEMWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) { w.writeObject(kp); w.flush(); } } }
PEMWriter privatepemWriter = new PEMWriter(new FileWriter(filename))); privatepemWriter.writeObject(privKey); privatepemWriter.close();
/** * Write an encrypted private key to a PEM file. * * @param outStream * The output stream to write to * @param key * The key to write * @param passwd * The password for the key * @return the number of bytes written * @throws NoSuchAlgorithmException * if the encryption cipher algorithm cannot be found * @throws NoSuchProviderException * If the BouncyCastle provider is not installed * @throws IOException * If there was an error opening or writing to the file */ public int writePrivateKeyToPEM(OutputStream outStream, PrivateKey key, String passwd) throws NoSuchAlgorithmException, IOException, NoSuchProviderException { PKCS8Generator generator= new PKCS8Generator(key, PKCS8Generator.AES_256_CBC, "BC"); generator.setPassword(passwd.toCharArray()); @SuppressWarnings("resource") PEMWriter writer= new PEMWriter(new OutputStreamWriter(outStream, Constants.UTF8)); PemObject pemObject= generator.generate(); writer.writeObject(pemObject); writer.flush(); return writer.getOutputSize(pemObject); }
String type = "CERTIFICATE REQUEST"; byte[] encoding = pkcs10.getEncoded(); PemObject pemObject = new PemObject(type, encoding); StringWriter str = new StringWriter(); PEMWriter pemWriter = new PEMWriter(str); pemWriter.writeObject(pemObject); pemWriter.close(); str.close(); System.out.println(str);
PEMWriter pemWriter = new PEMWriter(new FileWriter("Demo-CMS.p7b")); pemWriter.writeObject(obj); pemWriter.flush(); pemWriter.close();
/** * Encodes an object in PEM format and writes it to the stream. * * @param obj The object to encode and write. Must be of a type that can be encoded to PEM. * Usually this is limited to certain types from the java.security package * @param writer The stream to write the encoded object to * @throws IOException * @see #pemToObjects * @see #pemToCerts * @see #pemToPrivateKeys * @see #pemToPrivateKey * @see #pemToCertificateRequest */ public static void writeToPEM(Object obj, Writer writer) throws IOException { PEMWriter pw = new PEMWriter(writer); pw.writeObject(obj); pw.flush(); }
PEMWriter writer = new PEMWriter(new FileWriter("test.pem")); KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(3072); KeyPair keyPair = kpg.generateKeyPair(); writer.writeObject(keyPair.getPublic()); writer.close();
public static void saveKey(PrivateKey key, Writer to) throws IOException { try (PEMWriter pw = new PEMWriter(to)) { pw.writeObject(key); } }
/** returns the PEM (base64, ie for id_rsa) string for the private key / key pair */ public static String stringPem(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); } } }
public static void saveCertificate(X509Certificate cert, KeyPair keyPair,String fileName) { logger.info("Saving certificate "+fileName); if (cert == null) return; PEMWriter pemWriter = null; try { pemWriter = new PEMWriter(new FileWriter(fileName)); pemWriter.writeObject(cert); pemWriter.flush(); pemWriter.writeObject(keyPair.getPrivate()); pemWriter.flush(); } catch (IOException e) { e.printStackTrace(); return; } finally { try { pemWriter.close(); } catch (IOException e) { } } }
@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(); } }
/** * Write a chain of X509 certificates to a PEM file * * @param outStream * The output stream to write to * @param certChain * The X509 Certificate chain * @return the number of bytes written * @throws IOException * If there was an error opening or writing to the file */ public int writeX509CertChainToPEM(OutputStream outStream, X509Certificate[] certChain) throws IOException { ByteArrayOutputStream baos= new ByteArrayOutputStream(); PEMWriter writer= new PEMWriter(new OutputStreamWriter(baos, Constants.UTF8)); for (X509Certificate certificate : certChain) { writer.writeObject(certificate); } writer.close(); byte[] data= baos.toByteArray(); int bytesWritten= data.length; outStream.write(data); outStream.flush(); return bytesWritten; }
public static void savePublicKey(PublicKey key, Writer to) throws IOException { try (PEMWriter pw = new PEMWriter(to)) { pw.writeObject(key); } }