How a JWK maps to PEM
Two expressions of one key
A JWK lists key values under field names: n (modulus) and e (public exponent) for RSA, crv and x, y for EC, crv and x for OKP, all Base64URL. A PEM is a Base64 envelope around DER, and the DER holds an algorithm identifier (OID) and the key value in a fixed order.
Conversion therefore has two steps. Decode the JWK values from Base64URL into bytes, arrange them into the DER structure, and wrap the result as PEM. The reverse decodes the DER, takes the values out and writes them back as Base64URL.
The SPKI structure
The most common public-key PEM is SubjectPublicKeyInfo (SPKI):
SEQUENCE { SEQUENCE { OID algorithm, parameters }, BIT STRING publicKey }
- For RSA the algorithm OID is 1.2.840.113549.1.1.1, the parameters are NULL, and the BIT STRING contains SEQUENCE { INTEGER n, INTEGER e }.
- For EC the OID is 1.2.840.10045.2.1, the parameters carry the curve OID (P-256 is 1.2.840.10045.3.1.7), and the BIT STRING holds the uncompressed point starting with 0x04 followed by x and y.
- For OKP the OID is Ed25519 1.3.101.112 or X25519 1.3.101.110, and the BIT STRING holds the 32 raw public key bytes.
Where integer encoding goes wrong
A DER INTEGER is signed. When the top bit is set, a 0x00 must be prepended to show a positive value. JWK Base64URL values do not carry that 0x00, so inserting n or e as is makes them negative and produces a different key. Going the other way, the leading 0x00 must be removed to match the original value.
The other trap is length encoding. Lengths below 128 fit in one byte; longer ones need the 0x80 flag and the number of length bytes. A 2048-bit RSA modulus is 256 bytes, so it always takes the long form.
PKCS#8 private keys
Private keys are exported as PKCS#8 (PrivateKeyInfo): SEQUENCE { INTEGER 0, AlgorithmIdentifier, OCTET STRING privateKey }, where privateKey holds a PKCS#1 RSAPrivateKey for RSA, a SEC1 ECPrivateKey for EC, or a 32-byte seed for OKP.
- RSA private fields come in the order version, n, e, d, p, q, dp, dq, qi and optional otherPrimeInfos.
- EC SEC1 is version 1, privateKey, [0] curve OID, [1] public point, with [0] and [1] as explicit context tags.
- OKP PKCS#8 nests a 32-byte OCTET STRING inside the privateKey OCTET STRING.
An EC PKCS#8 built from a JWK without x and y cannot carry the public point. This tool includes the [1] point when x and y exist, and otherwise writes the curve information alone.
Reading PEM back into a JWK
Read the label first. PUBLIC KEY is SPKI, RSA PUBLIC KEY is PKCS#1, PRIVATE KEY is PKCS#8, RSA PRIVATE KEY is a PKCS#1 private key and EC PRIVATE KEY is SEC1. Ignoring the label and parsing the DER alone shifts the structure and yields the wrong values. This tool checks the label and the inner OID together and warns when they disagree.
Whether a converted public key is the same key can be confirmed by comparing the modulus or the coordinates. This tool shows the parameters of the JWK derived from a PEM next to the input, so cross-checking against another library is easy.
Questions
Is PEM a format or encryption?
A format. It is DER bytes turned into Base64 and wrapped in BEGIN and END labels; nothing is encrypted and anyone can decode it.
What if a public JWK still has d?
d is private key material. Remove d, p, q, dp, dq and qi before publishing it. This tool produces a public JWK with those fields removed.
Why do PEM labels differ?
-----BEGIN PUBLIC KEY----- is SPKI, -----BEGIN RSA PUBLIC KEY----- is a PKCS#1 public key, -----BEGIN PRIVATE KEY----- is PKCS#8, and -----BEGIN EC PRIVATE KEY----- is SEC1. Each label has its own inner structure.
The SPKI PEM button on a key card shows the conversion result directly.
Open the toolThis article follows RFC 7517 (JWK), RFC 7518 (JWA), RFC 7638 (JWK thumbprint), RFC 5280 (X.509), RFC 5958 (PKCS#8), RFC 5915 (SEC1) and RFC 8037 (OKP). Every example key is synthetic and was made for this site.