Public key vs private key explained - cover art

Crypto and hashing 15 min read

Public key vs private key

August 19, 2026 · 15 min read

In asymmetric cryptography, each party has a mathematically linked key pair. The private key must stay secret; the public key can be published. What one key encrypts (or what the private key signs), only the other key can decrypt or verify - depending on the operation.

RSA, ECDSA, and Ed25519 power TLS certificates, SSH login, JWT RS256 signatures, and PGP email. Understanding which key stays offline prevents catastrophic leaks.

Key pairs

RSA keys are large integers derived from primes; ECC keys are points on curves with smaller sizes for equivalent security. A 2048-bit RSA or 256-bit ECDSA key is a common minimum for new systems in 2026; prefer P-256 or Ed25519 for greenfield JWT and SSH.

Generate keys locally or in an HSM - never accept private keys from untrusted generators on the public internet for production.

Private key duties

The private key signs messages (proving origin) and decrypts messages encrypted to your public key. Compromise of the private key means attackers can impersonate you or read confidential data sent to you. Store private keys encrypted at rest (PEM + passphrase), with least-privilege access and hardware protection where possible.

Public key duties

The public key verifies signatures and encrypts small secrets sent to the owner. You distribute it via JWKS endpoints, certificate chains, or authorized_keys. Leaking the public key is fine; confusing it with the private key is not.

PEM public key begins:  -----BEGIN PUBLIC KEY-----
PEM private key begins: -----BEGIN PRIVATE KEY-----
                      (or ENCRYPTED PRIVATE KEY)

Signing vs encryption

Digital signatures use the private key to produce a tag verifiers check with the public key - integrity and authenticity. Public-key encryption uses the public key to encrypt (confidentiality to the holder) and the private key to decrypt. RSA-OAEP and ECIES handle key transport; bulk data uses symmetric session keys (TLS hybrid handshake).

JWT RS256 is signing only - the payload remains readable. For confidential claims, use JWE or transport-layer TLS.

Practical hygiene

Rotate keys on schedule and after personnel changes. Use separate keys for dev and prod. Never commit private keys to Git - scan repos with secret detection. For learning, generate disposable pairs in the browser; for production, use your cloud KMS or certificate authority.

// Verify JWT with public key (conceptual)
import { jwtVerify, importSPKI } from "jose";

const key = await importSPKI(publicPem, "RS256");
const { payload } = await jwtVerify(token, key);

FAQ

Can I derive the private key from the public key?
Computationally infeasible for properly generated RSA/ECC keys of recommended sizes.
Should I email my public key?
Yes, that is normal. Never email the private key.
RSA vs ECDSA for JWT?
Both work. ES256 (ECDSA P-256) yields smaller tokens and faster ops than RS256 for many workloads.
What is a PEM file?
Base64-encoded DER key material with header/footer lines - a text container format, not encryption by itself.

Related: What is HMAC?

Browse all tools