How Does Public-Key Cryptography Work?
How does public-key cryptography work? It uses two mathematically related keys instead of one shared secret. You can distribute the public key, but you must keep the private key secret. Data protected for a recipient with the public key can be recovered only through the corresponding private-key operation, while a digital signature created with a private key can be checked with the public key.
That simple idea solves a difficult problem: two people or systems can start a secure exchange without first meeting to share a secret key. In practice, modern systems combine public-key cryptography with fast symmetric encryption, certificates, hashes, and carefully designed protocols. This guide explains each part without assuming that you already understand the mathematics.
Quick Answer
Public-key cryptography, also called asymmetric cryptography, creates a pair of linked keys. The public key may be shared openly, while the private key remains under its owner’s control. Depending on the algorithm and protocol, the pair can establish a shared secret, encrypt a small value, verify a digital signature, or authenticate a device or person.
It is important not to reduce the entire process to “one key locks and the other unlocks.” Encryption, key agreement, and digital signatures are distinct operations with different security goals. Most real applications use the key pair during setup or authentication, then use a symmetric session key for the actual data.
Key Takeaways
- A public key may be shared; a private key must remain secret.
- Public-key encryption provides confidentiality only when the correct recipient’s public key is used.
- Digital signatures use the signer’s private key and let others verify origin and integrity with the public key.
- A certificate connects a public key to a named website, organisation, person, or device.
- Public-key algorithms and AES perform different jobs, so one is not a direct replacement for the other.
- Real systems usually use hybrid encryption because symmetric encryption is much faster for large amounts of data.
- Key protection and identity verification matter as much as the underlying mathematics.
Public Key and Private Key Cryptography With an Example
Imagine that Maya wants to receive a confidential file from Noah. Maya first creates a public and private key pair. She sends Noah the public key and keeps the private key on her own device.
Noah’s software uses Maya’s public key as part of a secure encryption or key-establishment process. Noah can send the protected result across an untrusted network because an observer does not possess Maya’s private key. Maya’s software uses her private key to recover the secret or derive the same session key, then decrypts the file.
The public key is not a password. Knowing it does not reveal the private key when a secure algorithm, suitable parameters, and a correct implementation are used. NIST defines public-key cryptography as a system in which users keep a private key secret and make the related public key available to others.
| Feature | Public key | Private key |
|---|---|---|
| Who can possess it? | Anyone who needs it | Only the owner or an authorized system |
| Main encryption role | Protects data or a session secret for the recipient | Recovers the protected data or secret |
| Main signature role | Verifies a digital signature | Creates a digital signature |
| Must it be confidential? | No, but its authenticity should be verified | Yes |
| What happens if it is exposed? | Exposure is expected; unauthorized replacement is dangerous | The key pair and dependent credentials may be compromised |
How Does Public-Key Cryptography Work Step by Step?
The exact messages differ among RSA, elliptic-curve systems, post-quantum algorithms, and individual protocols. However, the broad workflow is consistent.
- Generate a key pair. Software uses a cryptographically secure source of randomness and an approved algorithm to create related public and private keys.
- Protect the private key. The owner stores it in a secure operating-system keystore, hardware security module, security key, smart card, or another controlled environment.
- Distribute the public key. The key may be included in a certificate, directory, application profile, QR code, or direct exchange.
- Verify who owns the public key. The recipient checks a certificate chain, fingerprint, trusted directory, or another authenticated channel. Skipping this step can enable a person-in-the-middle attack.
- Perform the required public-key operation. The parties use the key pair for key establishment, encryption, authentication, or a digital signature.
- Use symmetric encryption for the data. In most modern protocols, a newly created session key encrypts the actual conversation or file efficiently.
- Discard or rotate temporary secrets. Short-lived session keys reduce the amount of data exposed if a later compromise occurs.
This is why secure browsing does not simply encrypt every page, image, and video directly with RSA. TLS 1.3 uses an authenticated handshake to establish traffic secrets, then protects application data with symmetric authenticated encryption.
Encryption and Digital Signatures Are Not the Same
Public-key cryptography supports two goals that people often mix together. Encryption protects confidentiality. Digital signatures support authentication, integrity, and evidence that the holder of a private key approved specific data.
For confidentiality, the sender targets the intended recipient’s public key. Only the corresponding private key should be able to complete the required decryption or decapsulation operation. This prevents other recipients from opening the protected content.
For a digital signature, the signer’s software first processes the message through a cryptographic hash function. A signature algorithm then uses the signer’s private key to produce a signature linked to that message. A verifier uses the signer’s public key to check it. If the message changes, verification should fail.
You may hear that signing is “encrypting with the private key.” That shortcut is misleading and is not a safe general description of modern signature schemes. Encryption and signing use different constructions, padding rules, and security checks. NIST’s Digital Signature Standard specifies approved approaches for generating and verifying signatures.
| Goal | Key used first | Key used to check or recover | What it provides |
|---|---|---|---|
| Confidentiality | Recipient’s public key | Recipient’s private key | Keeps protected content from unauthorized readers |
| Digital signature | Signer’s private key | Signer’s public key | Helps verify origin and detect alteration |
| Key agreement | Each party contributes key material | Both sides derive a shared secret | Creates a common session secret without sending it directly |
How Does RSA Work Step by Step?
RSA is one of the best-known public-key cryptography algorithms. Its security is based on the practical difficulty of factoring a sufficiently large number produced from secret prime numbers. The mathematical details matter, but you can understand the flow without calculating huge values.
RSA Key Generation
- The system selects two large random prime numbers.
- It multiplies them to produce a modulus used in both keys.
- It calculates related values that allow compatible public and private exponents to be chosen.
- The modulus and public exponent form the public key.
- The private exponent, along with protected supporting values, forms the private key.
RSA Encryption and Decryption
The sender encodes data using a standardised padding construction and performs the public-key calculation. The private-key holder performs the corresponding private calculation and validates the padding before accepting the result. Raw or “textbook” RSA is deterministic and unsafe, so real implementations must use approved schemes such as RSA-OAEP for compatible encryption use cases.
Modern software does not normally use RSA to encrypt a large file directly. It can protect a randomly generated symmetric key, while AES or another authenticated symmetric cypher handles the bulk data. NIST’s RSA key-establishment guidance documents approved techniques and implementation requirements.
A tiny classroom example may use small primes to make the arithmetic visible, but it offers no security. Production RSA uses large keys, secure randomness, padding, side-channel defences, and mature libraries. I recommend learning the toy mathematics for understanding, never copying it into an application.
Is RSA Better Than AES?
No. RSA and AES solve different problems, so “better” is the wrong comparison. RSA is an asymmetric algorithm used for tasks such as signatures or certain forms of key establishment. AES is a symmetric cypher that uses the same secret key to encrypt and decrypt data efficiently.
| Question | RSA | AES |
|---|---|---|
| Key model | Public and private key pair | One shared secret key |
| Typical role | Signatures or key-establishment functions | Bulk data encryption |
| Relative speed | Slower | Much faster |
| Suitable for large files or streams? | Not directly | Yes, with an appropriate authenticated mode or construction |
| Quantum concern | Large quantum computers would threaten current RSA | Quantum search reduces the security margin, but does not break AES in the same way |
In a hybrid system, the public-key method solves the initial trust or key-distribution problem, and AES protects the continuing data flow. They complement each other. One detail I would not overlook is authentication: encryption without reliable identity checking can create a private connection to the wrong party.
How Does PKI Work for Dummies?
Public Key Infrastructure, or PKI, is the trust system around public keys. A public key alone does not tell you who owns it. PKI uses digital certificates, certificate authorities, registration processes, policies, repositories, and revocation information to connect a key with an identity.
When your browser visits an HTTPS website, the server presents a certificate containing its public key and identity information. The browser checks whether the certificate is valid for that hostname, whether its dates are acceptable, and whether it chains to a trusted certificate authority. It may also evaluate revocation information and other security rules.
If those checks succeed, the browser can authenticate the server during the TLS handshake. It then establishes symmetric traffic keys for the session. The federal PKI 101 guide summarises how asymmetric cryptography, certificates, certificate authorities, and trust relationships fit together.
PKI does not make every certified website honest or safe. A certificate mainly helps confirm that the browser is communicating with the named domain through a protected connection. You should still inspect the domain carefully, especially when similar-looking letters could disguise a fake address. TechMezz’s guide to telling I, l, and 1 apart explains why visual similarity can be risky when checking identifiers or copied text.
Common Public Key Cryptography Algorithms
“Public-key cryptography” describes a family of techniques rather than one algorithm. Each algorithm is designed for specific operations, and secure use depends on current standards and protocol support.
- RSA: Used for digital signatures and legacy or specialised key-establishment cases. Correct padding and adequate key sizes are essential.
- Diffie-Hellman and finite-field DH: Allow parties to derive a shared secret over an open network. Authentication must be added to prevent an active attacker from impersonating both sides.
- Elliptic-curve cryptography: Provides key agreement and signatures with smaller keys than traditional RSA at comparable classical security levels. Common families include ECDH, ECDSA, X25519, and EdDSA, though they are not interchangeable.
- Post-quantum algorithms: Use mathematical problems intended to resist attacks by both classical and quantum computers. NIST finalised ML-KEM, ML-DSA, and SLH-DSA standards in 2024 for key encapsulation and digital signatures.
Do not choose an algorithm only because its name is familiar. The protocol, parameter set, library, update path, interoperability needs, and expected lifetime of the protected data all affect the decision. For application development, use a maintained cryptographic library and a well-reviewed protocol instead of designing your own format.
Where You Use Public-Key Cryptography Every Day
You probably rely on public-key systems even if you never handle a key file. Secure web connections, software updates, passkeys, signed documents, encrypted messaging, device enrollment, and remote administration all use asymmetric techniques in some form.
HTTPS and Secure Browsing
TLS uses public-key techniques during the handshake to authenticate endpoints and establish shared secrets. Symmetric authenticated encryption then protects the session. The padlock indicates a protected connection to the displayed domain, not a guarantee that every claim on the site is trustworthy.
Passkeys and Security Keys
A passkey uses a public-key credential scoped to a service. The service stores a public key, while the authenticator controls the corresponding private key and produces a signature for a fresh challenge. The WebAuthn specification defines how web applications create and use these public-key credentials for strong authentication.
Signed Software and Updates
Developers or publishers sign software packages and update metadata. Your device verifies the signature with a trusted public key before accepting the content. This check helps detect alteration and unauthorised publishers, but the surrounding update system must also protect key storage, version rules, and recovery processes.
Secure Messaging
Messaging systems can use public keys to establish shared secrets and authenticate participants. Mature end-to-end encrypted protocols add key rotation, forward secrecy, device verification, and recovery rules. A basic key pair alone does not provide all of those properties.
Online security also relies on layers beyond cryptography. For example, a site may combine encrypted transport with the CAPTCHA challenge-response process to reduce automated abuse. Encryption protects the connection, while CAPTCHA addresses a different problem: deciding whether an interaction appears human.
Risks, Limitations, and Common Mistakes
Strong mathematics cannot rescue a weak implementation or careless key handling. Most failures happen around the cryptography rather than through a direct attack on the underlying mathematical problem.
Trusting an Unverified Public Key
An attacker can replace a public key during distribution and make both parties communicate through the attacker. Certificates, verified fingerprints, trusted directories, and authenticated key-transparency systems help reduce this risk.
Exposing the Private Key
If someone copies a private signing key, they may be able to impersonate its owner until the credential is revoked and replaced. Encrypt private-key storage, restrict access, use hardware-backed protection when appropriate, and maintain a documented rotation and recovery process.
Never paste a private key into an unfamiliar checker, chatbot, form, or troubleshooting website. Treat it like a high-value credential. Even a legitimate-looking tool may log submissions, browser history, or analytics data.
Using Outdated or Incorrect Constructions
Raw RSA, weak randomness, obsolete hash functions, unauthenticated encryption, reused nonces, and home-made protocols can destroy security. Use supported libraries with safe defaults and follow the current documentation for the protocol you are implementing.
Assuming Encryption Proves Identity
Encryption can hide data from observers while still connecting you to an impostor. Authentication proves which key belongs to which identity. Secure protocols usually need both.
Ignoring the Quantum Transition
Large, fault-tolerant quantum computers would threaten widely deployed RSA and elliptic-curve public-key systems. The exact timetable is uncertain, but migrations take years and long-lived confidential data may need protection sooner. Organisations should inventory cryptographic dependencies, follow standards bodies, and plan upgrades rather than inventing their own post-quantum scheme.
Frequently Asked Questions
Can a public key decrypt data?
In ordinary public-key encryption, the recipient’s public key is used to protect data or a session secret, and the matching private key performs the recovery operation. A public key can verify a digital signature, but signature verification is not the same as decrypting a message.
What happens if someone steals a private key?
The consequences depend on how the key was used. An attacker may be able to impersonate the owner, decrypt compatible captured data, or sign malicious content, so the owner should revoke affected certificates, replace the key pair, investigate access, and update relying systems.
Is RSA better than AES?
No, because they have different roles. RSA is a public-key algorithm used mainly for signatures and certain key-establishment applications, while AES efficiently encrypts bulk data with a shared secret key. Secure protocols often use asymmetric and symmetric cryptography together.
How does PKI work for dummies?
PKI is a system that links public keys to identities through digital certificates and trusted certificate authorities. Your browser checks the website certificate and its trust chain before using the authenticated key exchange to establish a protected session.
What is the difference between public key and private key cryptography with an example?
Public-key cryptography uses a public and private key pair. For example, a sender can use the recipient’s public key in an encryption process, while only the recipient’s private key can complete the recovery step. Symmetric, or secret-key, cryptography instead uses one shared key for both encryption and decryption.
Conclusion
So, how does public-key cryptography work? It separates what may be shared from what must remain secret. The public key lets other systems protect information for you or verify your signatures, while the private key lets you recover protected secrets or create valid signatures.
The strongest real-world designs add identity checks, certificates, secure key storage, trusted libraries, authenticated symmetric encryption, rotation, and recovery. Start by identifying the exact goal, such as confidentiality, authentication, a signature, or key agreement, then choose a maintained protocol that already solves it. For more practical explanations of security, software, and everyday technology, continue exploring TechMezz.

