Wesloo
All topicsTopics

Computer security

How does public-key cryptography work?

Short answer

Public-key cryptography relies on a paired set of keys: one public key for encrypting messages or checking signatures, and one private key, kept secret, for decrypting messages or creating signatures. This makes secure communication and authentication possible without sharing a secret key in advance. It underpins TLS/SSL and other secure internet protocols, though it does not hide metadata such as sender, recipient, or timing.

Key points

  • A public key can be shared openly; the matching private key must stay secret.
  • The public key encrypts messages and verifies signatures; the private key decrypts and signs.
  • Trapdoor functions make one direction easy and the reverse hard without the secret key.
  • TLS/SSL uses public-key cryptography for encryption and website authentication.
  • Basic public-key encryption hides content but not metadata or sender identity.

How does public-key cryptography work? It relies on a pair of mathematically linked keys: a public key that can be shared with anyone and a private key that must remain secret. The public key encrypts data or checks a signature, while the private key decrypts data or creates a signature. Because the two keys are linked by a trapdoor function, knowing the public key does not reveal the private key.

What a key pair is

Public-key cryptography, also called public key encryption, relies on two matched keys: one public and one private. The public key may be shared openly, while the matching private key must stay secret. This removes the need to share a secret key before secure communication can begin.

The two keys are not arbitrary strings. They are generated together so that an operation performed with one key can be reversed or checked only with the other. A trapdoor function makes this practical: computing the forward direction is easy, but reversing it is hard unless you possess the secret trapdoor, which is the private key.

How encryption and decryption work

To send a confidential message, a sender gets the recipient's public key, encrypts the plaintext with it, and transmits the ciphertext over a network. The recipient then uses the matching private key to decrypt it. Only the holder of that private key can recover the original message.

This design gives confidentiality without a pre-shared secret. The sender does not need to know the recipient's private key, and an eavesdropper who sees the public key and the encrypted message still cannot read the content. The security rests on keeping the private key secret and on choosing a sound asymmetric key algorithm with adequate key length.

KeyWho can know itMain uses
Public keyAnyoneEncrypt messages, verify signatures
Private keyOwner onlyDecrypt messages, sign documents

Digital signatures and authentication

The same key pair works in reverse for authenticity. The owner signs a message with the private key, and anyone with the public key can verify that signature. A valid result shows that the signature was produced by the holder of the matching private key and that the signed content has not been changed in a way that breaks the signature.

This is why public-key cryptography supports both confidentiality and authentication. It can hide a message from everyone except the intended recipient, and it can show that a message or document came from the claimed sender. Software that relies on public-key cryptography combines these two properties.

How does public-key cryptography work in practice?

In practice, public-key cryptography is rarely used to encrypt an entire long conversation directly. Public-key operations usually establish or protect a shared secret, and that shared secret then protects the bulk of the data. This keeps the connection both secure and practical.

For example, TLS/SSL relies on public-key cryptography to secure internet connections in two main ways. First, encryption: when you visit an HTTPS site, TLS/SSL encrypts data exchanged between your browser and the server. The two sides agree on a session secret key, which keeps data safe from eavesdroppers. Second, authentication: the site offers a certificate issued by a trusted authority, and your browser checks it to confirm that you are connecting to the genuine site rather than an impostor.

Through public-key cryptography, TLS/SSL helps protect online privacy and supports confidence that websites are genuine and trustworthy. For related browser behavior, see How Do Web Cookies Work? A Practical Guide and How Does Browser Cache Work? A Practical Guide.

What it does not hide

Public-key encryption by itself does not reveal who sent a message; it hides only the content. It also leaves metadata visible, such as the source computer, sending time, and message length. Most public-key encryption software does not hide header metadata, which can include sender and recipient identities, sending date, subject, and software used. Only the message body is hidden, and it can be decrypted with the intended recipient's private key.

Because of this, an observer could build a detailed picture of who communicates with whom and what topics are discussed, even when the message body stays hidden. Anyone who needs stronger privacy must add separate protections for metadata and sender identity.

Risks and mitigations

Like other security systems, public-key cryptography has potential weaknesses. Besides choosing a weak asymmetric key algorithm or too short a key length, the main risk is that a private key becomes known. If that happens, messages, authentication, and other data tied to that key are no longer secure.

This risk is commonly reduced by using forward-secrecy schemes that create temporary keys during communication. Those temporary keys must also be compromised for the communication to be exposed, so revealing a long-term private key does not automatically expose past sessions.

Protecting the private key

The private key is the secret that makes the whole system work, so its protection matters more than the choice of public key. Store it in a secure location, restrict access, and replace it if there is any sign of compromise. Because public keys can be shared openly, losing control of a public key is not the same as losing control of the matching private key.

Verification in practice

When a recipient receives a signed message, their software uses the sender's public key to check the signature against the message. If the check succeeds, the recipient knows that the signature was created with the matching private key and that the message content used in the check is intact. If the check fails, the message should be treated as untrusted.

This process does not by itself prove the real-world identity of the signer. It proves control of a private key. Identity is usually established separately, for example through a digital certificate signed by a trusted authority.

National standards

Beyond algorithms from open academic and standards groups, some countries maintain national public-key cryptography standards. Examples include China's SM2 and SM9, Russia's GOST R 34.10-2012, South Korea's EC-KCDSA, and Ukraine's DSTU 4145.

Common mistakes

  • Sharing or copying a private key as if it were a public key.
  • Using a key length or algorithm that is too weak for the intended lifetime of the data.
  • Assuming that encryption hides who is talking to whom.
  • Treating a successful signature check as proof of identity without checking the certificate or key source.

Questions people also ask

What is the difference between a public key and a private key?

A public key can be shared with anyone and is used to encrypt messages or verify signatures. A private key must stay secret and is used to decrypt messages or create signatures. The two keys are generated as a matched pair.

Does public-key cryptography hide who sent a message?

No. Basic public-key encryption conceals the message content but not metadata such as sender, recipient, date, or message length. A third party may still learn a lot about the communication pattern.

Why is TLS/SSL considered secure?

TLS/SSL uses public-key cryptography for encryption and authentication. It helps browser and server agree on a session key and checks the website's certificate against a trusted authority. This protects data in transit and helps confirm the site's identity.

What happens if a private key is exposed?

Security tied to that key is lost, including messages and authentication that depend on it. This is why private keys must be protected and why some systems use ephemeral keys for forward secrecy.

Related pages

  • How do you create a strong password?

    A strong password is long, random, and used only once. A password manager can generate and store such passwords, while a memorable master password or passphrase protects the manager itself.

  • What is TLS traffic and how it protects web traffic

    TLS traffic is ordinary web traffic protected by the Transport Layer Security protocol. TLS encrypts the payload, verifies the server's identity with a certificate, and adds a message integrity check. The result is that eavesdroppers cannot read the data and tampering is detected.

  • What is ransomware attack? Definition and risks

    Ransomware is malware that blocks access to files, systems, or networks and demands payment for their return. Attackers often encrypt data. Backups, patching, and careful handling of email and remote access reduce the chance of a costly incident.

  • What is zero trust security model? A practical guide

    A zero trust security model is a framework that checks every access request instead of trusting users or devices by location. It uses identity, device health, and context to grant limited, temporary access. The goal is to reduce breach impact across cloud, remote, and on-premises systems.

  • How does a firewall work? Network security basics

    A firewall monitors and controls incoming and outgoing traffic using configurable rules. It commonly relies on packet filtering, proxying, and connection-state tracking to block unauthorized access. Hardware, software, and virtual forms can protect a single device or an entire network.

  • What is a digital signature in Adobe Acrobat?

    A digital signature in Adobe is a certificate-based electronic signature used to sign and verify PDF documents. It confirms who signed, shows whether the file changed after signing, and supports compliance with strict regulations. Signers need a digital ID from a cloud provider or a local system before applying it.

How this page was prepared

Before writing, the topic was researched in the sources below. The page then passed automatic quality checks.

Report a mistake on this page