用于Java的OpenSSL模拟

时间:2014-03-03 18:31:00

标签: java openssl rsa public-key-encryption

有没有办法使用Java进行这些OpenSSL操作

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

生成private.der和public.der

1 个答案:

答案 0 :(得分:2)

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();
}