How to create an RSA JWK from java.security.interfaces.RSAPrivateKey ?
Issue #216
resolved
Hello,
I’m trying to create a JsonWebKey
from an RSAPrivateKey
.
Looking at JsonWebKey.Factory.newJwk
it seems that I can’t do that with a private key. I have to use a public key.
I don’t understand why it’s not possible because it looks like a common use case, so maybe I miss something.
I managed to achieve what I need like that :
val generator = KeyPairGenerator.getInstance("RSA")
generator.initialize(2048)
val pair = generator.generateKeyPair()
val rsaJwk = JsonWebKey.Factory.newJwk(pair.public) as RsaJsonWebKey
rsaJwk.privateKey = pair.private
It’s weird but it works.
Is there an other way to do that ?
Comments (6)
-
repo owner -
repo owner - changed status to open
-
repo owner Issue
#215was marked as a duplicate of this issue. -
repo owner Asymmetric keys in JWK require the public parts (see for example https://datatracker.ietf.org/doc/html/rfc7518.html#section-6.3 for RSA) so the
JsonWebKey.Factory.newJwk(Key)
stuff is kinda just designed from that.
-
repo owner -
repo owner - changed status to resolved
- Log in to comment
that’s pretty much how to do it