What Web Crypto exports here (SPKI, PKCS#8, and JWK)
When you click Generate Key Pair, your browser calls crypto.subtle.generateKey() with public exponent 65537 (0x010001) and exports the key pair in two formats without sending a single byte over the network:
- PEM mode wraps the X.509 SubjectPublicKeyInfo (
spki) bytes in-----BEGIN PUBLIC KEY-----and the PKCS#8 (pkcs8) private bytes in-----BEGIN PRIVATE KEY-----, line-wrapped at 64 Base64 characters. - JWK mode outputs the exact same key pair as RFC 7517 JSON Web Keys (
kty: "RSA", modulusn, exponente, and private CRT parameters) ready for OIDC/.well-known/jwks.jsonendpoints or Web CryptoimportKey("jwk", ...).
Because both formats come from the same generated key object, switching the Output Format dropdown toggles instantly between PEM and JWK without regenerating new primes.
Choosing 2048-bit, 3072-bit, or 4096-bit keys
2048-bit RSA is the standard size for JWT RS256 signing, OAuth providers, and API authentication. Generating a 2048-bit pair in Chrome or Safari usually finishes in 50 to 250 milliseconds. Moving to 4096-bit keys takes noticeably longer to generate (often 1 to 4 seconds in a browser) and makes private-key signing roughly 4x to 6x slower on your server, while public-key verification stays fast. Pick 3072-bit or 4096-bit when a compliance policy requires it or when a root key needs to stay valid for a decade.
Why asymmetric keys beat shared secrets in multi-service apps
With symmetric JWT signing (HS256), every microservice that verifies a token also has to hold the signing secret, which means any compromised service can forge new admin tokens. With RSA (RS256), only your auth server holds private.pem. Every downstream API, edge worker, or third-party verifier only needs public.pem or the public JWK, which you can distribute openly without exposing signing privileges.
Handling PEM files in `.env` variables
Multi-line PEM keys often break simple .env parsers if you paste raw newlines directly. Two clean workarounds are replacing newlines with literal \n inside double quotes and restoring them with key.replace(/\\n/g, "\n") at startup, or Base64-encoding the entire PEM file using our Base64 Encoder & Decoder and decoding it in memory. For symmetric HMAC secrets instead of asymmetric keys, use our Random Token Generator.