What Is Public Key Cryptography?

Public key cryptography uses paired public and private keys for encryption, digital signatures, and key agreement across web and blockchain systems.

New to Nervos? Start here.

Explore the guide
What Is Public Key Cryptography?

Public key cryptography, also called asymmetric cryptography, uses pairs of mathematically related keys: a public key that can be shared and a private key that stays secret. Different public-key schemes use these keys to encrypt data, verify digital signatures, or establish shared secrets.

For example, someone can use your public encryption key to send you a confidential message that you decrypt with your private key. With a signature scheme, you use your private signing key to sign data, and others use the corresponding public key to check the signature.

The particular algorithm determines what a key pair can do. A key intended for signing is not automatically suitable for encryption.

Why use public and private keys?

With symmetric encryption, the sender and recipient use the same secret key to encrypt and decrypt data. That works well when they already share a key, but creates a setup problem when they have never communicated securely before.

Public-key methods let them protect communication without first sharing the same secret. They also make it possible to verify signatures without sharing the ability to create them.

The mathematics allows software to generate matching keys while making it computationally impractical to recover the private key from the public key, provided the scheme and its parameters are secure. Publishing the public key therefore does not reveal the corresponding private capability.

What is public key cryptography used for?

The three uses below assign different roles to the keys.

UseRole of the public keyRole of the private key
Public-key encryptionLets someone encrypt data for the key’s ownerLets the owner decrypt it
Digital signaturesLets others verify a signatureLets the owner create a signature
Diffie–Hellman key agreementEach party shares a public value with the otherEach party combines its private value with the other’s public value to calculate a shared secret

Encryption: keeping a message confidential

Suppose Alice wants to send Bob a private message. Using a suitable public-key encryption scheme, she encrypts it with Bob’s public key. Bob decrypts the resulting ciphertext with his private key.

Alice can encrypt for Bob without learning his private key or sharing a secret with him beforehand.

However, anyone with Bob’s public key can also send him an encrypted message. Encryption alone does not establish who sent it. Authenticating the sender requires an additional mechanism, such as a digital signature.

Digital signatures: checking signed data

A digital signature lets someone connect data to a particular signing key. The signer creates a signature using their private key and the data being signed.

A verifier checks it using three inputs: the data, the signature, and the corresponding public key. Altering the signed data causes the original signature to fail verification.

For example, a software publisher can sign a release. Users who have the publisher’s trusted public key can check that the downloaded file matches what was signed.

The signature does not hide the file’s contents. It provides evidence about the signing key and the integrity of the signed data.

Key agreement: establishing a shared secret

A key-agreement method such as Diffie–Hellman lets two parties calculate the same secret without transmitting that secret over the network.

Alice and Bob each generate a private value and a corresponding public value. They exchange the public values. Alice combines her private value with Bob’s public value, while Bob combines his private value with Alice’s public value.

The calculations produce the same shared secret. An eavesdropper who only observes the public values cannot feasibly calculate it when the method is used securely.

Their software can then derive encryption keys from that secret. Key agreement supplies the secret material; a separate encryption algorithm protects the conversation.

How public-key cryptography works

Public-key algorithms rely on one-way mathematical functions: deriving a public key from a private key is fast, while reversing the calculation to recover the private key from the public key is computationally infeasible.

To see how this works in practice, suppose Alice wants to send 10 tokens to Bob on a public blockchain. Every node on the network needs to confirm that Alice actually authorized the transfer, and that no one altered the amount in transit, without Alice ever revealing her private key.

The process runs in three stages:

1. Key generation

Alice's wallet generates a random private key and runs a one-way algorithm to derive her matching public key. She keeps the private key secret and shares the public key, or an address derived from it.

2. Signing

To send 10 tokens to Bob, Alice's wallet feeds the transaction data and her private key into a signing algorithm, producing a unique digital signature. She broadcasts the transaction, her signature, and her public key to the network.

3. Verification

Bob and other nodes on the network take the transaction data, Alice's signature, and Alice's public key to verify the math. If the signature matches and the data is unchanged, the transaction passes.

Common public key algorithms

Different public-key algorithms rely on different underlying mathematical problems.

RSA (Rivest–Shamir–Adleman) supports encryption and signature schemes, using different procedures for each. Its security depends on computational difficulty of factoring a massive composite number into its prime factors.

ECC (Elliptic-curve cryptography) is a family that includes the ECDSA (Elliptic Curve Digital Signature Algorithm) for signatures and ECDH (Elliptic-Curve Diffie–Hellman) for key agreement. ECC achieves comparable classical security to RSA with much smaller key sizes, making it efficient and widely used across modern communications and blockchains.

PQC (Post-quantum cryptography) develops alternatives designed to resist attacks by quantum computers. This is necessary because a sufficiently capable quantum computer running Shor's algorithm can break conventional RSA and ECC. These post-quantum alternatives rely on lattice-based, hash-based, code-based, and multivariate approaches, each with different security assumptions and practical trade-offs.

Public-key cryptography in practice

Hybrid encryption

Public-key operations are mathematically heavier than symmetric encryption algorithms like AES (Advanced Encryption Standard), which use a single shared secret key to encrypt large volumes of data efficiently.

To get the best of both, modern systems use hybrid encryption: they rely on public-key cryptography to authenticate parties and agree on a shared secret, then switch to symmetric encryption for the actual data payload.

In a certificate-authenticated TLS (Transport Layer Security) 1.3 connection, public key agreement establishes the shared material and signatures authenticate the server. Once established, symmetric encryption takes over to protect the actual web traffic.

Public-key cryptography in blockchain

A central use of public-key cryptography in blockchains is to make spending authorization publicly verifiable. A participant signs transaction data with a private key, and the nodes or validators check the signature against the corresponding public key. The blockchain's spending rules determine which keys can authorize a transaction: a valid signature from an unrelated key gives no right to spend the funds.

Bitcoin records amounts and spending conditions in unspent transaction outputs (UTXOs). One common format, such as Pay-to-Public-Key-Hash (P2PKH), records a RIPEMD-160 hash of a SHA-256 hash of a public key. Spending requires revealing the public key that matches that hash, plus a valid signature from that key. Traditional Bitcoin spending paths (legacy and SegWit v0) use ECDSA on the secp256k1 curve, while later Taproot (SegWit v1) key-path spending uses Schnorr signatures on the same curve.

Nervos CKB attaches spending conditions to data records called Cells, through programs called Lock Scripts. Its default lock checks ECDSA signatures on secp256k1 and ensures that the signing key matches the lock's requirements. Because of CKB's flexible virtual machine, developers can deploy custom lock scripts to implement entirely different signature schemes or multi-signature setups.

Closing thoughts

Public key cryptography lets people protect messages, verify signed data and establish shared secrets without disclosing their private keys. Secure web connections combine these capabilities with symmetric encryption, while blockchains use signatures to make spending authorization publicly verifiable. As quantum computing advances, post-quantum methods seek to preserve these capabilities against new threats, using different mathematical foundations.

Frequently asked questions

Is it possible to decrypt with a public key?

No. A public key can only encrypt data or verify a signature, but cannot decrypt ciphertext. The recipient needs the corresponding private key to decrypt a message. A common misconception is that verifying a digital signature is "decrypting with a public key," but cryptographic verification is a separate mathematical process that checks the data's integrity, rather than recovering a hidden message.

Is AES better than RSA?

They cannot be directly compared because they serve different roles. AES is a symmetric algorithm that protects data using a shared secret. RSA is an asymmetric algorithm used for public-key encryption and digital signature schemes. Modern systems use hybrid encryption, combining RSA (or ECC) to safely set up the connection, and AES to protect the actual data payload.

Can you encrypt with just a public key?

Yes, if you have the recipient's public key for a suitable encryption scheme, you can encrypt a message intended for them. Encryption software also needs the message, scheme parameters, and any required randomness. However, a public key intended only for digital signatures (like an ECDSA or Ed25519 key) cannot be used to encrypt messages.

Is public key cryptography quantum-resistant?

Conventional public key algorithms (RSA and ECC) are not quantum-resistant. However, a new generation of Post-Quantum Cryptography (PQC) is specifically designed to resist quantum attacks. For blockchains, crypto agility concerns how a system can adopt alternative schemes.

Are a wallet address and a public key the same thing?

No, they serve different roles. A public key is a cryptographic code derived mathematically from a private key and used directly in operations, like verifying digital signatures. A wallet address often contains a hash of a public key, but is specifically designed to be easily shared for receiving funds. A CKB address encodes a lock script specifying the conditions to spend the funds.

Share
CKB

What Is Ed25519?

Ed25519 is a fast, deterministic digital signature algorithm with compact keys, used in systems like OpenSSH and blockchains like Solana.

5 min read

What Is a Hash Function?

8 min read

What is a Digital Signature in Blockchain?

Digital signatures are essential for securing transactions and maintaining trust in blockchain networks.

5 min read
See all articles
↑