CertificateFormatter { public static String derOf(final RSAPublicKey publicKey) throws IOException { final RSAPublicKey rsaPublicKey = publicKey; final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); final DataOutputStream dataStream = new DataOutputStream(byteStream); writeBytesToDataStream(SSH_RSA.getBytes(UTF_8), dataStream); writeBytesToDataStream(rsaPublicKey.getPublicExponent().toByteArray(), dataStream); writeBytesToDataStream(rsaPublicKey.getModulus().toByteArray(), dataStream); final String publicKeyEncoded = new String(Base64.encodeBase64(byteStream.toByteArray()), UTF_8); return SSH_RSA + " " + publicKeyEncoded; } private  CertificateFormatter(); static String pemOf(final Object pemObject); static String derOf(final RSAPublicKey publicKey); static final String SSH_RSA; }
@Test public void derOf_convertsObjectToDerEncodedString() throws IOException { final String pemString = CertificateFormatter.derOf((RSAPublicKey) keyPair.getPublic()); assertThat(pemString, startsWith("ssh-rsa ")); }