RSA Key Formats Explained: PKCS#1 vs PKCS#8 vs JWK and Conversion

Learn what BEGIN RSA PRIVATE KEY, BEGIN PRIVATE KEY and JWK mean, which RSA key size to pick, and how to convert between formats with OpenSSL and Node.js.

Updated September 29, 2026 7 min read

Use the free RSA Key Pair GeneratorGenerate RSA 2048/3072/4096 key pairs as PEM (PKCS#8, PKCS#1) or JWK

A library's documentation says "provide your RSA private key in PEM format". You have a PEM file, but the library throws an error about an unsupported key type. You open the file and the first line says -----BEGIN RSA PRIVATE KEY-----, while the example in the docs says -----BEGIN PRIVATE KEY-----. Both are RSA keys, both are PEM, and yet they are not interchangeable.

This guide untangles the formats an RSA key can be stored in, explains which one different tools expect, helps you choose a key size, and shows how to convert between formats with OpenSSL and Node.js. It also covers how to handle the private key safely once you have it.

One key, several encodings

An RSA key pair is a handful of large numbers: the modulus and public exponent (the public key), plus the private exponent and the primes used to build them. Formats differ only in how those numbers are wrapped.

Format PEM header Contains Typical users
PKCS#1 private BEGIN RSA PRIVATE KEY RSA numbers only Older OpenSSL 1.x output, some PHP and Ruby code
PKCS#1 public BEGIN RSA PUBLIC KEY Modulus and exponent only Some older libraries
PKCS#8 private BEGIN PRIVATE KEY Algorithm identifier plus the key OpenSSL 3, Node.js, Java, Go, Python cryptography
Encrypted PKCS#8 BEGIN ENCRYPTED PRIVATE KEY PKCS#8, encrypted with a passphrase Keys stored on disk
SPKI public BEGIN PUBLIC KEY Algorithm identifier plus the public key Most modern tools, JWT libraries, certificates
JWK JSON object Base64url numbers such as n, e, d Web Crypto, JWKS endpoints, OAuth and OpenID Connect

The key difference: PKCS#1 is RSA-only, so the header has to say "RSA". PKCS#8 and SPKI are generic and carry an algorithm identifier inside, so the same header works for RSA, EC or Ed25519 keys. That is why modern libraries prefer them.

PEM itself is just the outer wrapper: Base64 text between BEGIN and END lines. DER is the same data in raw binary. When someone says "a PEM key", you still need to know which structure is inside.

Choosing a key size

  • 2048-bit is the widely used minimum and is still broadly accepted.
  • 3072-bit gives roughly 128-bit security, a common target for keys meant to last many years.
  • 4096-bit is larger again, but key generation and signing are noticeably slower and signatures are bigger.

For signing JSON Web Tokens or similar short-lived use, 2048 or 3072 is typical. Check any standard or platform you must comply with, since some set their own minimums. If you are creating SSH keys rather than application keys, Ed25519 is usually a better choice than RSA; see how to set up SSH key authentication.

The public exponent is almost always 65537, the standard value used by OpenSSL, Web Crypto and most libraries.

Generate a key pair in the browser

The RSA Key Generator uses your browser's Web Crypto API, so the keys are created on your device and never uploaded. It's convenient for development, test fixtures and learning how the formats compare.

  1. Open the RSA Key Generator and choose a Key size: 2048-bit (common default), 3072-bit (128-bit security) or 4096-bit (slower, larger).
  2. Click Generate RSA key pair. A 4096-bit key may take a few seconds while two large primes are found.
  3. Check the summary line: key size, exponent 65537 and the public key's SHA-256 fingerprint, which you can use to confirm later that a public key matches.
  4. Pick a tab. PKCS#8 / SPKI gives BEGIN PUBLIC KEY and BEGIN PRIVATE KEY, the modern format. PKCS#1 gives the traditional BEGIN RSA ... versions. JWK gives the public and private keys as JSON.
  5. Copy or download the files you need (public.pem, private.pem and so on). All three tabs are the same key pair, so you can take the public key from one tab and the private key from another.

The private keys are not encrypted. For anything beyond local testing, generate the key on the machine or in the secrets system that will use it, and never commit it to a repository.

Generate and convert with OpenSSL

OpenSSL 3 creates PKCS#8 keys by default:

# Create a 3072-bit private key (PKCS#8)
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:3072 -out private.pem

# Extract the public key (SPKI)
openssl pkey -in private.pem -pubout -out public.pem

Convert between the private key formats:

# PKCS#8 -> PKCS#1 ("BEGIN RSA PRIVATE KEY")
openssl rsa -in private.pem -traditional -out private-rsa.pem

# PKCS#1 -> PKCS#8 ("BEGIN PRIVATE KEY")
openssl pkcs8 -topk8 -nocrypt -in private-rsa.pem -out private.pem

# Add a passphrase ("BEGIN ENCRYPTED PRIVATE KEY")
openssl pkey -in private.pem -aes256 -out private-encrypted.pem

And the public key formats:

# SPKI -> PKCS#1 ("BEGIN RSA PUBLIC KEY")
openssl rsa -pubin -in public.pem -RSAPublicKey_out -out public-rsa.pem

# Fingerprint of the public key (SHA-256 of its DER encoding)
openssl pkey -in private.pem -pubout -outform DER | openssl dgst -sha256 -c

Generate and convert with Node.js

Node's built-in crypto module handles every format without extra packages. To generate a pair with the private key encrypted by a passphrase from an environment variable:

const { generateKeyPairSync } = require("node:crypto");

const { publicKey, privateKey } = generateKeyPairSync("rsa", {
  modulusLength: 3072,
  publicKeyEncoding: { type: "spki", format: "pem" },
  privateKeyEncoding: {
    type: "pkcs8",
    format: "pem",
    cipher: "aes-256-cbc",
    passphrase: process.env.KEY_PASSPHRASE,
  },
});

To convert an existing unencrypted key to PKCS#1 and to a public JWK:

const fs = require("node:fs");
const { createPrivateKey, createPublicKey } = require("node:crypto");

const priv = createPrivateKey(fs.readFileSync("private.pem"));
const pkcs1 = priv.export({ type: "pkcs1", format: "pem" });
const jwk = createPublicKey(priv).export({ format: "jwk" });

console.log(pkcs1.split("\n")[0]); // -----BEGIN RSA PRIVATE KEY-----
console.log(jwk.kty, jwk.e);       // RSA AQAB

AQAB is simply 65537 in base64url. JWKs like this are what you publish at a JWKS endpoint so other services can verify tokens you sign; the JWT Generator and our guide on creating and signing a JWT show that workflow.

Troubleshooting format errors

"Unsupported key" or "invalid key format"

The library probably expects PKCS#8 and received PKCS#1, or the other way round. Check the header line and convert with one of the commands above.

The key has lost its line breaks

PEM is sensitive to structure. When a key is stored in an environment variable or JSON, newlines often become literal \n sequences. Restore them before parsing, or store the key Base64-encoded as a whole.

A public key "doesn't match" the private key

Compare fingerprints. Derive the public key from the private key and hash both; if the SHA-256 values differ, you have keys from different pairs.

A certificate request is involved

If you are ordering a TLS certificate, the private key is paired with a certificate signing request. The CSR Decoder shows what a CSR contains, and the SSL Certificate Decoder reads the certificate you get back.

Handling private keys safely

  • Keep private keys in a secrets manager, key vault or hardware module where possible, not in source code or a repository.
  • On disk, encrypt them with a passphrase or restrict permissions (chmod 600).
  • Use separate keys for development and production, and rotate a key immediately if it may have been exposed.
  • Don't paste production private keys into websites. Local-only tools are fine for test keys, but the safest production key is one that was generated where it lives.

Frequently asked questions

What is the difference between PKCS#1 and PKCS#8?

PKCS#1 is an RSA-specific structure with the header BEGIN RSA PRIVATE KEY. PKCS#8 is a generic container that includes an algorithm identifier, with the header BEGIN PRIVATE KEY, and is the default in modern tools.

Is a PEM file the same as a PKCS#8 key?

No. PEM is the Base64 text wrapper. The data inside can be PKCS#1, PKCS#8, SPKI, a certificate or other structures, which the header line tells you.

Should I use a 2048 or 4096-bit RSA key?

2048-bit is widely accepted and fast. 3072-bit offers a longer security margin, and 4096-bit is slower with limited practical benefit for most applications. Follow any standard your platform requires.

How do I get the public key from a private key?

With OpenSSL, run openssl pkey -in private.pem -pubout. In Node.js, pass the private key to crypto.createPublicKey() and export it.

Can I use an RSA key from this tool for SSH?

SSH uses its own public key format. The SSH Key Generator produces OpenSSH keys directly, or you can convert an SPKI public key with ssh-keygen -i -m PKCS8 -f public.pem.

Try it now: RSA Key Pair Generator

Loading tool…