Can JcaPEMWriter generate PKCS#8 output?


The following code uses the JcaPEMWriter class from BouncyCastle to output a randomly generated RSA private key in PKCS#1 format (-----BEGIN RSA PRIVATE KEY-----):

public static void main(String[] args) throws Exception { final KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(2048, null); final KeyPair kp = kpg.generateKeyPair(); final PrivateKey privateKey = kp.getPrivate(); final StringWriter s = new StringWriter(); try (JcaPEMWriter w = new JcaPEMWriter(s)) { w.writeObject(privateKey); } System.out.println(s); }

Is there any way to make JcaPEMWriter output PKCS#8 format (-----BEGIN PRIVATE KEY-----) instead?


You need to supply a slightly different object to the PEMWriter, namely a JcaPKCS8Generator. The following should work

try (JcaPEMWriter w = new JcaPEMWriter(s)) { w.writeObject(new JcaPKCS8Generator(privateKey, null)); }



  • RSA Key ( XML Format ) compatible for .Net
  • JS loop variable scope
  • Android - findViewById() vs findViewWithTag()
  • getting exception when inserting events in android calendar
  • How to work with Mailgun API in CodeIgniter; Forbidden error in curl_exe()
  • this.refs is undefined for shallow tests using enzyme on React native
  • Get domain\\username from microsoft graph
  • Set selected using jQuery
  • Multiple git user in single device
  • How to make responsive images in flutter?
  • Target in barchart in dc.js
  • How to highlighting active menu item on scroll and click?
  • Custom progress dialog not working
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • Homomorphic filtering in OpenCV
  • Authorize Attribute Authentication with Postman in Web Api
  • PHP: Need to close STDIN in order to read STDOUT?
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • Arraylist of strings into one comma separated string
  • Spotify cocoalibspotify offline status set to 1 but all tracks stuck at waiting
  • Multiplying polynomials/simplifying like terms
  • How to define something in JavaScript [closed]
  • Floating parent div grows to hypothetical width of floating child div
  • Calculate time from document
  • Neo4j…how to get a visual representation of my data?
  • How to turn off notice reporting in xampp?
  • Will this work on all screen sizes?
  • matrix multiplication apache pig
  • Bitrate JWplayer
  • Codeigniniter insert data through models and controller
  • Sign a Pdf using custom digital signature in Java
  • multiple button click in asp.net MVC 3
  • Accessing Arguments, Workflow Variables from custom activities
  • Spring Boot not autowiring @Repository
  • Running R's aov() mixed effects model from Python using rpy2
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}