Reputation: 71
I am trying to generate a JWK using my private key while using jose4j library to generate a JWE with the earlier mentioned JWK. But the key generated doesn't seem to be correct, also, it doesn't have all the fields (only n, e and kty get generated , expected fields are kty, n, e, d, p, q, dp, dq, qi). Any suggestions?
I am using the following code :
byte[] decoded = Base64.decodeBase64(privateKeyString);
PrivateKey privateKey =
KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec privKeySpec = kf.getKeySpec(privateKey, RSAPrivateKeySpec.class);
RSAPublicKeySpec pubKeySpec =
new RSAPublicKeySpec(privKeySpec.getModulus(), privKeySpec.getPrivateExponent());
RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(pubKeySpec);
RsaJsonWebKey jwkp = (RsaJsonWebKey) PublicJsonWebKey.Factory.newPublicJwk(pubKey);
jwkp.setPrivateKey(privateKey);
jwkp.toJson(JsonWebKey.OutputControlLevel.PUBLIC_ONLY);
Upvotes: 0
Views: 1476
Reputation: 2461
JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE
needs to be explicitly used in the toJson(...)
call for the private key field(s) to be included. That's d specifically. The others - p, q, dp, dq, qi - will be there if it's a RSAPrivateCrtKey
Upvotes: 1