How to use RSA as kty jwk parameter (using jose4j)

1k Views Asked by At

I am using jose4j to encrypt a String with JWE, following this documentation https://bitbucket.org/b_c/jose4j/wiki/JWE%20Examples

In the example, they used {\"kty\":\"oct\",\"k\":\"Fdh9u8rINxfivbrianbbVT1u232VQBZYKx1HGAGPt2I\"}

It works well, but how to use RSA as kty ? Should I generate myself a key and put it in the "k" parameter ?

2

There are 2 best solutions below

2
On BEST ANSWER

no, a JWK representation of an RSA key has different parameters e.g.:

{
  "kty":"RSA",
  "n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx
     4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMs
     tn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2
     QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbI
     SD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqb
     w0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
   "e":"AQAB",
   "alg":"RS256",
   "kid":"2011-04-29"
}

See RFC7517 : https://www.rfc-editor.org/rfc/rfc7517

0
On

Additions to Hans answer, I've looked to the Jose4j Github, and there were many tests that helped me to understand how RSA works on this lib.

Found my answer here : https://github.com/pvliesdonk/jose4j/blob/master/src/test/java/org/jose4j/jwe/RsaOaepKeyManagementAlgorithmTest.java