2
votes

I want to ask something, while using asymmetric encryption, we encrypt the data using client's public key, so the client can decrypt the data using their private key right?

I just found tutorials for signing JWT using RSA, but I found they encrypt the data using the server private key instead of client's public key, and the server's public key shared among clients.

Is it even safe? Because if the public key is fall to the wrong hands because it's shareable, everyone can decrypt it right?

so, is it okay to sign the jwt like that?

references: tutorial1 tutorial2

2
Don't confuse the RSA decryption as RSA signature, it is not. RSA signature requires special padding like RSA-PSS. Similarly, RSA encryption also requires padding like PKCS#1 v1.5 or RSA-OAEP. RSA should never be used without a proper padding.kelalaka

2 Answers

1
votes

In this scenario, the purpose is not to encrypt the data so that others cannot read it ("confidentiality"), it is to sign the data so that others with the public key can verify that you are in possession of the private key and you actually signed the data. The data in this case is a hash of the JWT header and payload. The private key is used for signing so that only one entity - the authentication server - can sign JWTs. The public key is used for signature validation so that any third party with the public key can validate the JWT. The public key cannot be used to create a valid signature.

So yes, it is safe!

2
votes

so the client can decrypt the data using their private key right?

Yes, in case of encrypting data this is the normal way.

But in case of JWTs it's not about encrypting something that only the receiver is supposed to decrypt, but about signing a token.

The goal is, that only one instance, e.g. the authentication server or generally the token issuer, can sign the token, but everyone can verify the signature with the public key.

Because if the public key is fall to the wrong hands because it's shareable, everyone can decrypt it right?

The public key can't fall into the wrong hands, because it is public. So per definition everyone is allowed to see it.

The contents of the token (i.e. the payload) is not encrypted, but just base64url encoded and can be read by everone, e.g on https://jwt.io.

so, is it okay to sign the jwt like that?

yes