Public key encryption, also called asymmetric encryption, uses a pair of mathematically linked keys: a public key you can share freely and a private key you keep secret. Anyone can encrypt a message with your public key, but only your private key can decrypt it. The same pair also powers digital signatures, which work the other way around.
Key takeaways
- A public key encrypts, a private key decrypts — and for signatures the roles reverse.
- In practice, public key encryption sets up a session, then faster symmetric encryption protects the actual data.
- Use RSA-2048 or larger, or elliptic-curve keys, and plan for post-quantum algorithms such as ML-KEM.
Every time you log in to your bank, send a message on WhatsApp or open an HTTPS website, public key encryption is working quietly in the background. It solves a problem that looks impossible at first: how do two people who have never met agree on a secret over a channel anyone can listen to?
What is public key encryption?
Public key encryption is a system that uses two mathematically linked keys instead of one shared secret. Your public key is published openly, and anyone can use it to encrypt a message for you, while your private key stays secret and is the only key that can decrypt it.
The classic analogy is a padlock. You hand out open padlocks (your public key) to anyone who wants to send you something. Anyone can snap a padlock shut on a box, but only you hold the key that opens it. Even the person who locked the box cannot open it again.
The system is also called asymmetric encryption, because the two keys are different. It was first described publicly in 1976, when Whitfield Diffie and Martin Hellman published the idea of public key cryptography, and the RSA algorithm followed in 1977 from Ron Rivest, Adi Shamir and Leonard Adleman.
How does public key encryption work?
Public key encryption works because the two keys are linked by a hard mathematical problem. Multiplying two large prime numbers is easy, but pulling the product back apart into its original primes is practically impossible at the sizes used in real keys.
Sending a secret message takes three steps. First, the recipient generates a key pair and publishes the public key. Second, the sender encrypts the message with that public key. Third, the recipient decrypts it with the private key. Anyone intercepting the message in transit sees only scrambled data.
For RSA, the public key is a pair of numbers: a modulus n, the product of two large primes, and a public exponent e, almost always 65537. Encryption raises the message to the power e modulo n. The private key holds a private exponent d that reverses that operation, and computing d requires knowing the two primes.
A worked example makes the math concrete. With tiny primes p = 61 and q = 53, the modulus is n = 3233 and the public exponent e = 17 gives a private exponent d = 2753. Encrypting the number 65 gives 2790, and raising 2790 to the power 2753 modulo 3233 returns 65. Real keys use primes hundreds of digits long, so the same steps are infeasible to reverse.
What is the difference between a public key and a private key?
The public key is meant to be shared with the whole world, while the private key must never leave your control. The public key encrypts data and verifies signatures; the private key decrypts data and creates signatures.
| Public key | Private key | |
|---|---|---|
| Shared with | Anyone | Nobody — kept secret by the owner |
| Encrypts messages | Yes | No |
| Decrypts messages | No | Yes |
| Creates signatures | No | Yes |
| Verifies signatures | Yes | No |
| If it leaks | Nothing happens — it is public by design | All messages can be read and your identity can be faked |
Think of the public key as your home address and the private key as the key to your front door. The address is public and tells people where to send things, but the door key must stay in your pocket. If someone copies your door key, they can walk in whenever they like.
One common mistake is treating the two keys as interchangeable. They are not. Encrypting with a private key does not keep a message secret, because anyone with the public key can reverse it. That operation is a signature, not encryption, and it proves who sent the message rather than hiding it.
Public key vs symmetric encryption: what's the difference?
Symmetric encryption uses one shared secret key for both encryption and decryption, while public key encryption uses a key pair. Symmetric ciphers such as AES are fast, so they protect the bulk of the data, while public key cryptography handles the key exchange and authentication around them.
| Symmetric encryption | Public key encryption | |
|---|---|---|
| Keys | One shared secret key | A public key and a private key |
| Speed | Very fast | Much slower — roughly 1,000 times slower for bulk data |
| Key distribution | Hard — both sides must share the secret safely first | Easy — the public key can travel over an insecure channel |
| Main uses | Encrypting files, disks and data in transit | Key exchange, digital signatures, certificates |
| Examples | AES, ChaCha20 | RSA, elliptic-curve cryptography, Diffie-Hellman |
In practice the two work together. When your browser opens an HTTPS site, public key cryptography authenticates the server and agrees on a session key. Then AES or ChaCha20 takes over and encrypts the actual web traffic, because it is fast enough for streaming video and large downloads. This hybrid design is used by TLS, VPNs and encrypted messaging apps.
The key distribution problem is what makes public key encryption so important. With symmetric encryption alone, two strangers cannot safely agree on a secret key, because any channel they use to share it could be watched. Public key cryptography breaks that deadlock: the public key can cross an insecure channel without harm.
What are the main types of public key encryption?
The main types are RSA, elliptic-curve cryptography (ECC) and Diffie-Hellman key exchange, including its elliptic-curve form. Each rests on a different hard mathematical problem, and each has different key sizes, speeds and uses.
RSA
RSA is the oldest widely used public key system, published in 1977, and it remains common in certificates and signatures. Its security rests on the difficulty of factoring a large number into its two prime factors. RSA keys should be at least 2048 bits, and the Canadian Centre for Cyber Security recommends moving to at least 3072 bits by the end of 2030.
RFC 8017, the current specification, defines two encryption schemes: RSAES-OAEP, which should be used for new applications, and the older RSAES-PKCS1-v1_5, kept only for compatibility. It also defines signature schemes, and states that RSASSA-PSS is required in new applications while RSASSA-PKCS1-v1_5 is included only for compatibility with existing ones.
Elliptic-curve cryptography (ECC)
ECC uses the mathematics of elliptic curves instead of factoring. It provides the same security as RSA with much shorter keys: a 256-bit elliptic-curve key offers roughly the same protection as a 3072-bit RSA key. That makes ECC popular on phones, smart cards and other devices with limited processing power.
Elliptic-curve Diffie-Hellman (ECDH) is the key exchange used in most modern TLS connections, and the elliptic-curve digital signature algorithm (ECDSA) signs many web certificates. The curve X25519, standardized in RFC 7748, is widely used in TLS 1.3, SSH and messaging apps.
Diffie-Hellman key exchange
Diffie-Hellman, published in 1976, lets two parties agree on a shared secret over a public channel without ever sending the secret itself. Each side combines their own private value with the other side's public value, and both arrive at the same shared secret. An eavesdropper who sees all the public values still cannot compute it.
Modern TLS uses ephemeral Diffie-Hellman, where fresh keys are generated for every session. This gives forward secrecy: even if a server's long-term private key is stolen later, past sessions cannot be decrypted, because their session keys were never stored.
How is public key encryption used in TLS and HTTPS?
TLS uses public key encryption to authenticate the server and agree on session keys, then switches to symmetric encryption for the data itself. When you visit an HTTPS site, your browser checks the site's certificate, which binds the site's public key to its domain name.
The handshake works in outline like this. The browser and server agree on a TLS version and cipher. The server presents its certificate, and the browser verifies the digital signature on it using the certificate authority's public key. The two sides then run an ephemeral Diffie-Hellman exchange to agree on session keys, and all further traffic is encrypted symmetrically.
You can see this in action on any HTTPS site. The padlock in your browser means the certificate checked out and the connection is encrypted, but it does not prove the site itself is honest — a phishing site can have a valid certificate too. Check your own site's certificate with our free SSL checker to see its key type, expiry date and chain.
Certificates are the weak point of the whole system. If a certificate authority is tricked into issuing a certificate for your domain, an attacker can impersonate you. That is why certificate transparency logs, short certificate lifetimes and monitoring matter. Our certificate monitoring watches for unexpected certificates issued for your domains.
What is a digital signature, and how does it relate?
A digital signature uses the same key pair in reverse: the sender signs with the private key, and anyone can verify the signature with the public key. A valid signature proves the message came from the private key holder and has not been changed since it was signed.
Signing works by hashing the message first, then applying the private key operation to the hash. Verification recomputes the hash and checks it against the value recovered with the public key. If even one byte of the message changed, the hash will not match and the signature fails.
Digital signatures are everywhere. Software updates are signed so your computer can reject tampered packages. Email can be signed with S/MIME or DKIM so recipients can verify the sender's domain. Code signing certificates prove that an application came from a known publisher. If you receive a file whose signature does not verify, do not run it.
Note the direction carefully, because it is the most common source of confusion. Encryption protects confidentiality: public key in, private key out. Signatures protect authenticity: private key in, public key out. Mixing them up is how systems end up encrypting with a private key and believing the result is secret.
How do you generate a public key?
You generate a key pair with a trusted tool, keep the private key somewhere safe, and share the public key freely. On most systems the standard tool is OpenSSL, and modern systems should prefer elliptic-curve keys or RSA-2048 at minimum.
- Generate the private key. For example,
openssl genpkey -algorithm ed25519 -out private.pemcreates a modern elliptic-curve key, or use-algorithm RSA -pkeyopt rsa_keygen_bits:2048for RSA. - Extract the public key. Run
openssl pkey -in private.pem -pubout -out public.pemto produce the public half. - Share the public key. Publish it, send it to contacts, or put it in a certificate. Anyone can use it to encrypt messages for you.
- Protect the private key. Set strict file permissions, encrypt it with a passphrase, and never email it, paste it into a chat, or commit it to a code repository.
- Back it up safely. Store an encrypted copy offline. If you lose the private key, every message encrypted to it is unreadable forever.
Never generate keys with an untrusted online tool. A website that generates your key pair in the browser could keep a copy of the private key. Use tools installed on your own machine, or a hardware security key, and verify the tool's reputation first.
For SSH, the same idea applies: ssh-keygen -t ed25519 creates a key pair, and you copy the public key to the server. If you still use an older RSA SSH key, consider moving to Ed25519, which is faster and uses shorter keys.
Is public key encryption still safe from quantum computers?
Public key encryption is safe against today's computers at proper key sizes, but a large enough quantum computer running Shor's algorithm would break RSA and elliptic-curve cryptography at any key size. That is why NIST finalized the first post-quantum standards in August 2024.
The replacements are ML-KEM for key establishment and ML-DSA and SLH-DSA for digital signatures. ML-KEM is a lattice-based key encapsulation mechanism, and it is the direct successor to RSA key transport and elliptic-curve Diffie-Hellman. NIST's guidance expects migration to happen over years, with hybrid deployments that combine classical and post-quantum algorithms during the transition.
The risk is not only future decryption. Attackers can record encrypted traffic today and decrypt it later, once a capable quantum computer exists — the "harvest now, decrypt later" problem. Data that must stay secret for decades, such as health records or state secrets, is the most exposed, which is why long-lived systems should start planning migration now.
For most small businesses, the practical advice is simple: keep software updated so you receive post-quantum support as vendors ship it, prefer TLS 1.3 and current cipher suites, and do not try to implement post-quantum cryptography yourself. The standards and libraries are still maturing, and a bad implementation is worse than a standard one.
What are the common mistakes with public key encryption?
Most public key failures are not broken mathematics — they are broken key management. The algorithm works, but the private key leaks, the key is too small, or nobody checks that the public key really belongs to the person it claims to.
- Sharing or leaking the private key. Committing a private key to GitHub, emailing it, or storing it in a shared drive hands your identity to anyone who finds it. Rotate the key immediately if this happens.
- Using keys that are too small. RSA-1024 is no longer acceptable. Use RSA-2048 or larger, or elliptic-curve keys such as P-256 or Ed25519.
- Using textbook RSA without padding. Raw RSA is deterministic and malleable, so real systems must use padding schemes such as OAEP for encryption and PSS for signatures, as specified in RFC 8017.
- Trusting a public key without verification. A public key is only useful if you know whose it is. Certificates, key fingerprints and out-of-band verification exist for exactly this reason.
- Reusing one key pair for everything. Use separate keys for encryption and signing, and separate keys per service, so one compromise does not affect everything.
- Forgetting expiry and revocation. Certificates expire and keys get compromised. Monitor expiry dates and revoke promptly when a key is lost.
Phishing remains the most common way attackers bypass encryption entirely. The FBI received 191,561 phishing and spoofing complaints in 2025, the most-reported internet crime category that year, according to the 2025 IC3 Annual Report. Encryption protects data in transit, but it cannot stop someone handing their password to a fake login page. Our guide to what spear phishing is explains how those attacks work.
The bottom line
Public key encryption is the reason strangers can communicate securely on the open internet. The public key lets anyone send you a secret, the private key lets only you open it, and the same pair in reverse proves that a message really came from you.
Use it correctly: RSA-2048 or larger or modern elliptic-curve keys, proper padding, verified certificates, and private keys that never leave your control. Then check that your own website is using it properly with the free SSL checker.
Frequently asked questions
What is public key encryption in simple terms?
It is a way to send secret messages using two keys instead of one. You publish a public key that anyone can use to lock a message for you, and you keep the matching private key that is the only thing that can unlock it.
What is the difference between a public key and a private key?
The public key is shared openly and is used to encrypt messages or verify signatures. The private key is kept secret by its owner and is used to decrypt messages or create signatures. Anyone who learns your private key can read your messages and impersonate you.
Is public key encryption the same as asymmetric encryption?
Yes. Asymmetric encryption is the technical name for public key encryption, because it uses two different keys. Symmetric encryption uses one shared secret key for both encryption and decryption.
What is public key encryption used for?
It secures HTTPS websites, encrypts email with PGP and S/MIME, protects software updates, verifies digital signatures on documents and code, and sets up the encrypted sessions used by VPNs and messaging apps.
Is RSA still safe to use?
Yes, against classical computers, when used at proper key sizes with correct padding. RSA keys should be at least 2048 bits, and RSA-2048 is scheduled for deprecation after 2030. A large enough quantum computer running Shor's algorithm would break RSA at any key size.
What will replace RSA and elliptic-curve encryption?
NIST finalized its first post-quantum standards in August 2024. ML-KEM replaces RSA and elliptic-curve key establishment, while ML-DSA and SLH-DSA replace them for digital signatures. Migration is expected to take years, and hybrid deployments are common during the transition.
Can someone work out my private key from my public key?
Not with any practical amount of computing power, if the keys are generated correctly. Deriving an RSA private key means factoring a huge number into its two primes, which is infeasible at 2048 bits or more with classical computers.
Do I need public key encryption for my small business?
You already use it every time staff visit an HTTPS website, send encrypted email or connect to a VPN. What your business needs is to use it correctly: valid TLS certificates, current software, strong key sizes and phishing-resistant login methods.
Sources
- IETF — RFC 8017: PKCS #1: RSA Cryptography Specifications Version 2.2
- NIST CSRC — Cryptographic Agility and Transition R&D Plans
- Canadian Centre for Cyber Security — Cryptographic Algorithms for UNCLASSIFIED, PROTECTED A and PROTECTED B Information (ITSP.40.111)
- FBI Internet Crime Complaint Center — 2025 IC3 Annual Report
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against RFC 8017, NIST SP 800-56B key-establishment guidance and the Canadian Centre for Cyber Security algorithm guidance.
How we write and fact-check →