How Public Key Cryptography Works in HTTPS
Public key cryptography, also called asymmetric cryptography, solves a hard problem: how do two parties who have never met agree on a secret over a network anyone can watch? The answer is a pair of mathematically linked keys. One is public and can be shared with everyone. The other is private and never leaves its owner.
Public key cryptography: the two main jobs
A key pair can do two different things, and HTTPS uses both:
- Digital signatures. The owner signs data with the private key. Anyone can check that signature with the public key. A valid signature proves the data came from the key owner and was not changed.
- Key agreement or encryption. Two parties use public keys to build a shared secret that an eavesdropper cannot work out.
A certificate is simply a public key plus an identity, signed by a CA. If that is new to you, start with what a CSR is and how it leads to a certificate.
Why not use it for everything?
Asymmetric operations are slow compared with symmetric ones such as AES or ChaCha20. So TLS uses public key methods only to authenticate the server and agree on a secret. After that, both sides switch to a fast symmetric key for the actual page data.
| Property | Asymmetric | Symmetric |
|---|---|---|
| Keys | Public and private pair | One shared secret |
| Speed | Slower | Much faster |
| Used in TLS for | Signatures, key exchange | Encrypting traffic |
| Examples | RSA, ECDSA, Ed25519, ECDHE | AES-GCM, ChaCha20-Poly1305 |
A small example you can run
This creates a key pair, signs a file and verifies the signature:
openssl genpkey -algorithm EC \
-pkeyopt ec_paramgen_curve:P-256 -out priv.pem
openssl pkey -in priv.pem -pubout -out pub.pem
openssl dgst -sha256 -sign priv.pem -out sig.bin msg.txt
openssl dgst -sha256 -verify pub.pem \
-signature sig.bin msg.txt
The last command prints "Verified OK." Change one byte in msg.txt and it fails. That is the same check a browser runs on a certificate.
How public key cryptography fits into the TLS handshake
- The server sends its certificate, which holds its public key.
- The browser verifies the CA signature chain and the domain name.
- The server proves it holds the private key by signing handshake data.
- Both sides run an ephemeral Diffie-Hellman exchange to create fresh session keys.
Because the session keys are ephemeral, recording the traffic today and stealing the server key later does not reveal past sessions. That property is called forward secrecy, explained in what perfect forward secrecy is.
RSA and elliptic curves
RSA relies on the difficulty of factoring large numbers. Elliptic curve schemes such as ECDSA rely on a different hard problem and reach similar strength with much smaller keys. Both are accepted by browsers today. Large quantum computers would break both, which is why post-quantum algorithms are being standardized.
Protecting the private key
Everything above depends on the private key staying private. Keep it out of version control, restrict file permissions to the service account, and never email it. If you suspect a leak, reissue the certificate with a new key and revoke the old one. Public keys, by contrast, are safe to publish.
Common misunderstandings
"The public key encrypts and the private key decrypts, always." That is true for classic RSA encryption, but it is not how HTTPS works today. Modern TLS uses the private key to sign, and uses a Diffie-Hellman exchange to agree on session keys. Encrypting the session secret directly with the server's RSA key was removed in TLS 1.3.
"A longer key is always better." Only up to a point. Past the recommended size, extra length costs speed and adds little. Elliptic curve keys are short by design.
"Anyone with the public key can impersonate me." No. The public key only verifies signatures. Impersonation needs the private key.
Related reading
To see these ideas in a certificate, read SSL vs TLS vs HTTPS. For a hands-on check of your own setup, follow how to test SSL configuration with OpenSSL. If you want to create keys yourself, the private key generator does it in the browser, though for production keys you should generate them on the server so they never travel.
A last practical point: key pairs are cheap to generate and certificates are cheap to reissue, so rotate keys when staff change or when a server is rebuilt. Old keys left on retired machines are a quiet risk.
Related reading: perfect forward secrecy and how CT logs record every certificate.
Frequently Asked Questions
Which key encrypts and which decrypts?
In classic RSA encryption the public key encrypts. In modern TLS the private key signs, and a Diffie-Hellman exchange builds the session keys.
Why not use public key cryptography for the whole session?
It is slow. TLS uses it to authenticate and agree a secret, then switches to a fast symmetric cipher for the data.
Can someone impersonate my site with my public key?
No. The public key only verifies signatures. Impersonation needs the private key.
Are longer keys always better?
Only to a point. Past the recommended size you pay in speed and gain little, and elliptic curve keys reach the same strength with far fewer bits.
What does a certificate actually contain?
A public key and an identity, signed by a certificate authority. The browser checks that signature and the name.
✅ Post-Installation Verification & SEO Checklist
After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:
- Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
- Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
- Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
- Confirm both root and
wwwversions resolve cleanly to one canonical HTTPS URL. - Verify your HTTPS property and
sitemap.xmlinside Google Search Console and Bing Webmaster Tools.