好的,我从这个链接找到了一个可行的解决方案。
我发布了对我有用的答案:
// Generate a new RSA key pair wrapped in a JWK
PublicJsonWebKey rsaJwk = RsaJwkGenerator.generateJwk(2048);
// or an EC key, if you prefer
PublicJsonWebKey ecJwk = EcJwkGenerator.generateJwk(EllipticCurves.P256);
// A JSON string with only the public key info
String publicKeyJwkString = rsaJwk.toJson(JsonWebKey.OutputControlLevel.PUBLIC_ONLY);
System.out.println(publicKeyJwkString);
// A JSON string with both the public and private key info
String keyPairJwkString = rsaJwk.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE);
System.out.println(keyPairJwkString);
// parse and convert into PublicJsonWebKey/JsonWebKey objects
PublicJsonWebKey parsedPublicKeyJwk = PublicJsonWebKey.Factory.newPublicJwk(publicKeyJwkString);
PublicJsonWebKey parsedKeyPairJwk = PublicJsonWebKey.Factory.newPublicJwk(keyPairJwkString);
// the private key can be used to sign (JWS) or decrypt (JWE)
PrivateKey privateKey = parsedKeyPairJwk.getPrivateKey();
// the public key can be used to verify (JWS) or encrypt (JWE)
PublicKey publicKey = parsedPublicKeyJwk.getPublicKey();