How Public-Key Cryptography Works

Fractional CTO · Founder of Injuria.ai
You use public-key cryptography thousands of times a day without noticing. It runs every time you load a site over HTTPS, log into a server over SSH, or install signed software. It solved a problem that had no good answer for most of recorded history, and the idea behind it is genuinely elegant once you strip away the math. You do not need to follow the equations to understand what it does and why it works.
The problem: sharing a secret you cannot share
Classic encryption is symmetric. Both sides use the same secret key, one to lock the message and the same one to unlock it. This works fine if the two parties can agree on that key privately. The trouble is agreeing on it. Imagine sending an encrypted message to someone you have never met, over the open internet, where anyone might be listening. To encrypt it you both need the shared key, but to share the key you need a secure channel, which is the very thing you do not have yet. This is the key distribution problem, and for centuries the only answer was to meet in person or trust a courier.
The breakthrough: two keys instead of one
Public-key cryptography, also called asymmetric cryptography, replaces the single shared key with a pair of mathematically linked keys, one public and one private. You publish the public key to the whole world and keep the private key secret. The two are linked, and critically, you cannot work out the private key from the public one in any practical amount of time.
The breakthrough has firm dates worth knowing. Whitfield Diffie and Martin Hellman published New Directions in Cryptography in 1976, and Rivest, Shamir, and Adleman followed with RSA in 1977.[1][2] There is a twist: researchers at the UK intelligence agency GCHQ had discovered the same ideas a few years earlier, but their work stayed classified until 1997, so the public credit, and the entire public internet built on it, went to the academics.[8]
Two things the key pair lets you do
That one arrangement gives you two abilities that come from the same idea but must be described carefully. The first is encryption: someone encrypts a message with your public key, and only your private key can decrypt it, so they never need to share a secret with you beforehand.
The second is digital signatures, and here a popular shortcut needs correcting. People often say a signature is "encrypting with your private key." That analogy roughly fits the oldest RSA scheme and is flatly wrong for the signature algorithms actually used today, like ECDSA and Ed25519, which perform a distinct mathematical operation that does not encrypt anything. The accurate statement is simpler: signing is a separate operation that uses your private key to produce a tag only your public key can verify. Because only you hold the private key, a valid signature proves the message came from you and was not altered. Encryption keeps a message secret. A signature proves who sent it and that it is intact. They are different tools, not the same tool run backward.
The intuition: easy one way, hard to reverse
The whole thing rests on trapdoor functions, easy to compute in one direction and extremely hard to reverse without a secret. The classic example is multiplication versus factoring. Multiplying two very large primes is fast even for hundred-digit numbers. Working backward from the product to the original primes is, as far as anyone knows, hopelessly slow without extra information. RSA builds on exactly that gap.[8] A newer family based on elliptic curves uses a different hard problem, the elliptic-curve discrete logarithm, and gets the same security with much smaller keys, which is why most modern systems have moved to it. Your private key is simply the shortcut that lets you go backward.
Why it works with symmetric crypto, not instead of it
Public-key operations are slow, far slower than symmetric ones, so you would not encrypt a whole web session with them directly. They are used to bootstrap, not to carry the load. In broad strokes, the two sides use asymmetric cryptography at the start to agree on a fresh shared secret, then switch to a fast symmetric cipher for the actual conversation. This is what happens in the first moments of an HTTPS connection, the TLS handshake.[3]
Modern TLS does this more cleverly than the "encrypt a secret and send it" story suggests, and the distinction is the difference between good and broken security. Rather than one side encrypting a secret with the other's public key, both sides run an ephemeral Diffie-Hellman key agreement, each contributing to a shared secret that is never transmitted and is thrown away after the session. The asymmetric keys are used not to carry the secret but to sign the handshake and prove identity. The payoff is forward secrecy: steal the server's private key tomorrow and you still cannot decrypt traffic you recorded today, because the session secret was never sent and no longer exists.
Where you meet it, and the trust problem underneath
HTTPS is the obvious place, though the famous padlock icon is on its way out: Chrome replaced it in 2023 precisely because it misled people into reading "encrypted" as "safe." Encryption solves privacy, but it raises a new problem. If you just received a public key over the network, how do you know it belongs to your bank and not to an attacker sitting in the middle, feeding you their key and relaying your traffic? That man-in-the-middle attack is exactly what certificate authorities exist to stop. A CA is a trusted third party that vouches, with its own signature, that a given public key really belongs to a given domain, and your browser ships with a list of CAs it trusts.[7] SSH solves the server trust (man-in-the-middle) problem using Trust On First Use (TOFU) and known_hosts files; proving you hold a private key instead of typing a password is used for client authentication, not server verification.[5]
The cloud on the horizon
One honest caveat belongs in any modern treatment. A sufficiently powerful quantum computer running Shor's algorithm would break both RSA and elliptic-curve cryptography, because it can factor and compute discrete logarithms efficiently. That machine does not exist yet, but the threat is taken seriously enough that NIST has standardized a new generation of post-quantum algorithms, and the industry has begun migrating to them.[4][6] The idea that you can hand your lock to the entire world and still be the only one who can open it reshaped what computers could safely do with strangers. Every private thing you do online rests on it, usually without a moment's thought, which is the mark of infrastructure that works, and the current task is quietly rebuilding its foundations before the math that protects it is someday undone.
Sources (8)
- Diffie & Hellman, New Directions in Cryptography (1976)
- Rivest, Shamir, Adleman, A Method for Obtaining Digital Signatures and Public-Key Cryptosystems (RSA, 1978)
- RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3
- NIST FIPS 186-5: Digital Signature Standard
- RFC 4253: The Secure Shell (SSH) Transport Layer Protocol
- NIST Post-Quantum Cryptography Standardization
- Let's Encrypt: How It Works
- Song Y. Yan, Primality Testing and Integer Factorization in Public-Key Cryptography (Springer Science & Business Media, 2013)