Nimbus JOSE JWT Encryption with RSA, Private and Public Key

6.6k views Asked by At

Having doubts about a concept applied in the sample code named "JSON Web Token (JWT) with RSA encryption"

for reference see: http://connect2id.com/products/nimbus-jose-jwt/examples/jwt-with-rsa-encryption

The sample code delivers an RSAEncrypter class which is based on use of the public key, as well as an RSADencrypter class which use the opposite, a private key.

In a more practical view, I cannot understand why the JSON Web Token was generated this way, as the encrypted information usually will be sent to a client using the JWE format. In parallel, the client extracts the public key from a shared source, like a digital certificate store, or JWK store and then decrypts the information from the JWE data.

My question: Why does the client side use a private key? Why not use the private key at the Encrypter and the public key at the Decrypter class?

Clarifications about the conceptual side of this RSA sample code are welcome.

1

There are 1 answers

3
pedrofb On BEST ANSWER

The objective of encryption, as said in the example, is confidentiality: ensure the data is only read by the intended receiver

An essential security aspect in public key encryption is ensuring the data is encrypted for the intended recipient, and not some for other party, which may compromise the data’s confidentiality.

A JSON Web Token issued for authentication between parties is not encrypted, is digitally signed with the private key. The other party can verify authenticity and integrity with the published public key. But the content is not hidden. An observer could read the message but not modify it.

Encryption is done with recipient's public key. Only the owner of the matching private key will be able to decrypt the content of the JWT. Due to size restrictions of the data encrypted with a RSa key, in the example is generated an AES symmetryc encryption key. The message will be encrypted with the AES key, and this key is encrypted with the RSA public key and embedded into the JWT. The recipient will decrypt the AES key with the RSA private one