class CertUtils {

    public static byte[] generateCertRequest(PrivateKey privateKey, PublicKey publicKey, String
            principal)
            throws NoSuchAlgorithmException, OperatorCreationException, IOException {
        X500Principal subject = new X500Principal(principal);

        ContentSigner signGen = new JcaContentSignerBuilder("SHA256withRSA").build(privateKey);

        PKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(subject, publicKey);
        PKCS10CertificationRequest csr = builder.build(signGen);

        try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
            try (Writer destination = new OutputStreamWriter(output)) {
                try (JcaPEMWriter pemWriter = new JcaPEMWriter(destination)) {
                    pemWriter.writeObject(csr);
                }
                return output.toByteArray();
            }
        }
    }

    private  CertUtils();

}