title: OpenSSL commands in Java?
body: <p>So I made a program based off this tut:
  <a href="http://www.macs.hw.ac.uk/~ml355/lore/pkencryption.htm" rel="nofollow">http://www.macs.hw.ac.uk/~ml355/lore/pkencryption.htm</a></p>
  
  <p>It works perfectly fine, but as you can see at the bottom of the page it requires OpenSSL commands. I used OpenSSL and generated my keys in the correct format to test my program, works perfectly. But I was wondering how I would generate those keys just in Java. I have tried countless times but encryption is so hard to understand for me and to work with. So I have no code to show at the moment, how would I start this or if anyone has a good method for doing so. I don't mind using bouncy castle if it is possible.</p>
  
  <p>TL;DR: I would like to generate a unique Private and Public key in the correct format (PKCS#8, DER) using java (with or without a public library).</p>
  
  <p>Just need these commands in java (not running through OpenSSL):</p>
  
  <p>To generate a private key of length 2048 bits:</p>
  
  <pre><code>openssl genrsa -out private.pem 2048
  </code></pre>
  
  <p>To get it into the required (PKCS#8, DER) format:</p>
  
  <pre><code>openssl pkcs8 -topk8 -in private.pem -outform DER -out private.der -nocrypt
  </code></pre>
  
  <p>To generate a public key from the private key:</p>
  
  <pre><code>openssl rsa -in private.pem -pubout -outform DER -out public.der
  </code></pre>