PemUtil { public static void writePrivateKey(PrivateKey key, File targetFile) { try { writePrivateKey(key, new FileOutputStream(targetFile)); } catch (FileNotFoundException ex) { LOGGER.warn(ex); } } private  PemUtil(); static void writePrivateKey(PrivateKey key, File targetFile); static void writePrivateKey(PrivateKey key, OutputStream outputStream); static void writePrivateKey(byte[] encodedKey, OutputStream outputStream); static void writePublicKey(PublicKey key, File targetFile); static void writePublicKey(PublicKey key, OutputStream outputStream); static void writePublicKey(byte[] encodedKey, OutputStream outputStream); static void writeCertificate(Certificate cert, File file); static Certificate readCertificate(InputStream stream); static Certificate readCertificate(File f); static PrivateKey readPrivateKey(InputStream stream); static PrivateKey readPrivateKey(File f); static PublicKey readPublicKey(InputStream stream); static PublicKey readPublicKey(File f); static byte[] encodeCert(Certificate cert);  }
@Test public void testWritePrivateKey() { BigInteger secret = new BigInteger(SECRET); CustomECPrivateKey key = new CustomECPrivateKey(secret, NamedGroup.SECP256R1); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PemUtil.writePrivateKey(key.getEncoded(), baos); String result = new String(baos.toByteArray()); assertThat(result, containsString("-----BEGIN PRIVATE KEY-----")); assertThat(result, containsString("MEECAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcEJzAlAgEBBCBlhdBA2pVVpBpVqfWQ")); assertThat(result, containsString("-----END PRIVATE KEY-----")); }