How Public-Key Cryptography Works

Matt George

Fractional CTO · Founder of Injuria.ai

•6 min read•

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)
  1. Diffie & Hellman, New Directions in Cryptography (1976)
  2. Rivest, Shamir, Adleman, A Method for Obtaining Digital Signatures and Public-Key Cryptosystems (RSA, 1978)
  3. RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3
  4. NIST FIPS 186-5: Digital Signature Standard
  5. RFC 4253: The Secure Shell (SSH) Transport Layer Protocol
  6. NIST Post-Quantum Cryptography Standardization
  7. Let's Encrypt: How It Works
  8. Song Y. Yan, Primality Testing and Integer Factorization in Public-Key Cryptography (Springer Science & Business Media, 2013)

Recommended reading

How HTTPS Actually Works: TLS, Certificates, and Trust6 min readThe padlock in your address bar is the visible end of a quiet negotiation involving two kinds of cryptography, a signed certificate, and a chain of trust that reaches back to your operating system. Here is what is really happening, including the part everyone gets slightly wrong.Passkeys and WebAuthn: The End of Passwords?16 min readA comprehensive technical reference on WebAuthn and passkeys, explaining the cryptographic mechanisms, history, implementation trade-offs, and strategic considerations for engineers and technical leaders.SSO, SAML, and OpenID Connect: How Enterprise Login Works14 min readA comprehensive technical guide to Single Sign-On (SSO), explaining the mechanisms, history, and tradeoffs of the SAML and OpenID Connect protocols for engineers and technical leaders.The OWASP Top 10: The Web Vulnerabilities Every CTO Should Know13 min readA comprehensive reference for technical leaders on the OWASP Top 10, explaining the mechanisms, risks, and strategic decisions behind today's most critical web application vulnerabilities.Software Supply Chain Security: Your Dependencies Are an Attack Surface12 min readModern software is assembled from open-source dependencies, creating a complex supply chain that has become a primary target for attackers. Securing this supply chain requires a multi-layered approach including vulnerability scanning, artifact signing, and verifiable build processes.GDPR and CCPA for Engineers: Data Rights in Practice12 min readA technical guide to implementing data subject rights like access and erasure under GDPR and CCPA, focusing on architecture, tradeoffs, and practical code examples.