StringWriter stringWriter = new StringWriter();
try {
PemWriter writer = new PemWriter(stringWriter);
PemObjectGenerator pemObject = new PemObject("CERTIFICATE REQUEST", csr.getEncoded());
writer.writeObject(pemObject);
writer.close();
} catch (IOException e) {
throw new IllegalArgumentException("Error generating PEM", e);
}