title: How to generate a RSA keyPair with a Privatekey encrypted with password?
body: <p>I want to generate a privatekey PKCS8 format encrypted with password, and I try with this code:</p>
  
  <pre><code>String password = "123456";
  KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
  gen.initialize(2048);
  KeyPair key = gen.generateKeyPair();
  PrivateKey privateKey = key.getPrivate();
  PublicKey publicKey = key.getPublic();
  
  FileOutputStream pvt = new FileOutputStream("d:\\pvt123456.der");
  try {
      pvt.write(privateKey.getEncoded());
      pvt.flush();
  } finally {
      pvt.close();
  }
  FileOutputStream pub = new FileOutputStream("d:\\pub123456.der");
  try {
      pub.write(publicKey.getEncoded());
      pub.flush();
  } finally {
      pub.close();
  }
  </code></pre>
  
  <p>But I don´t know how to encrypt a password with 3des to be compatible with openssl format.</p>