RSA is a public-key cryptosystem based on the computational difficulty of factoring large integers. In API authentication, it appears as a key-pair approach: a private key signs a JWT, while the corresponding public-key arrangement supports verification. RSA also appears in JWE encryption, where RSAES-OAEP encrypts the content-encryption key. section 8.2 section 7.2.2
RSA separates signing authority from token verification more cleanly than a shared secret. A JWT can use RS256, or the probabilistic PS256 signature scheme, with the private key kept by the issuer. Key identifiers such as kid help identify the signing key in a JOSE header. section 8.2 section 7.2.2
Issue a PS256-signed JWT
Generate a 3072-bit RSA key pair with OpenSSL, load the private key through Python’s cryptography library, and pass it to PyJWT with algorithm="PS256". Keep the private key outside application source in real deployments; the example reads it from private_key.pem. section 8.2
openssl genpkey -algorithm RSA -out private_key.pem \ -outpubkey public_key.pem -pkeyopt rsa_keygen_bits:3072
Shows the OpenSSL command for generating a 3072-bit RSA private and public key pair. section 8.2
Listing 8.6 Issuing a JWT with PS256 signature using PyJWT
# file: ch08/jwt_generator_pyjwt_ps256.py
from pathlib import Path
import jwt
from cryptography.hazmat.primitives import serialization
private_key_text = Path("private_key.pem").read_text()
private_key = serialization.load_pem_private_key(
data=private_key_text.encode(), password=None
)
payload = {...} # same as in listing 8.3
print(
jwt.encode(payload=payload, key=private_key, algorithm="PS256")
) #1
Shows the focused PyJWT call that signs a payload with a loaded RSA private key and PS256. section 8.2
Apply RSA safely by checking the surrounding token design:
- Select and enforce the intended signing algorithm instead of treating the token’s algorithm metadata as an unrestricted choice. section 7.2.2
- Protect the private key and keep personally identifiable information out of JWT payloads, which are represented separately from the signature. section 8.2 section 7.2.2
- Use expiration, issuer, audience, and issued-at claims so consumers can evaluate the token’s intended scope and lifetime. section 8.2



