Question

Is there any way to do these OpenSSL operation using Java

openssl genrsa -out private.pem 2048
openssl pkcs8 -topk8 -in private.pem -outform DER -out private.der -nocrypt
openssl rsa -in private.pem -pubout -outform DER -out public.der

to generate private.der and public.der

Was it helpful?

Solution

KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(2048);
KeyPair pair = gen.generateKeyPair();
try (OutputStream os = Files.newOutputStream(Paths.get("public.der"), StandardOpenOption.CREATE_NEW)) {
  os.write(pair.getPublic().getEncoded());
  os.flush();
}
try (OutputStream os = Files.newOutputStream(Paths.get("private.der"), StandardOpenOption.CREATE_NEW)) {
  os.write(pair.getPrivate().getEncoded());
  os.flush();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top