liveBook · Manning

RSA

As explained in Secure APIs

RSA is a public-key cryptosystem based on the computational difficulty of factoring large integers.

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

How Secure APIs covers it

How deep it goes
Explains RSA through JWT signing, key generation, private-key loading, and JWE placement, without developing factoring mathematics.
Examples
Demonstrates OpenSSL key generation, Python key loading, and PyJWT issuance of a PS256-signed token.
What it assumes
Assumes familiarity with JWT headers, payloads, signatures, and claims while demonstrating RSA key handling from concrete commands.
What you can do after
Apply it via OpenSSL, cryptography, and PyJWT to generate RSA keys and issue PS256-signed JWTs.
How it connects
Connects RSA with JWT, JOSE, JWE, RS256, PS256, RSAES-OAEP, and A256GCM.

Also explained in

Compare how 4 books explain RSA →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secure APIs. The section links point to the parts of the book it draws on.