Public-Key Encryption


Public-Key Encryption

Corey
Today we’re unpacking public-key cryptography: the problems it relies on, the main kinds of keys, and how the technology is used for encryption, key exchange, and digital signatures. The central idea is a puzzle that’s difficult to solve without a secret, but much easier when you have that secret.

Hope
That secret is the trapdoor. The mathematical problem can be public, so anyone can try to use the system, but knowing the private information makes a particular operation practical. The foundations of public-key cryptography were set out by Whitfield Diffie and Martin Hellman in a 1976 paper on key exchange. In 1977, Ron Rivest, Adi Shamir, and Leonard Adleman introduced RSA, which remains an important method.

Corey
Each user has a pair of related keys: a public key that can be shared, and a private key that must be kept secret. Bob and Alice each have their own pair. If Bob wants to send something confidentially to Alice, he uses Alice’s public key, and Alice uses her private key to decrypt it.

Hope
There are three broad mathematical families to know: integer factorization, discrete logarithms, and elliptic-curve methods. RSA is based on the difficulty of factoring a large product of primes. ElGamal is a discrete-log-based method, and discrete-log mathematics also appears in key exchange and signatures. Elliptic-curve cryptography uses operations on points on a curve.

Corey
For RSA, the public modulus is made by multiplying two large prime numbers, p and q. It’s straightforward to multiply them, but recovering the original primes from their product is intended to be computationally infeasible when the numbers are large. That difference—easy in one direction, hard in the other—is the core of the trapdoor idea.

Hope
A discrete-log example starts with a generator g and a large prime p. Someone computes y as g raised to a secret exponent x, modulo p. The values g, y, and p can be public, but finding x from them is intended to be difficult. In practice, the details of the parameters matter a lot to the security.

Corey
Elliptic-curve systems can provide comparable security with much smaller numbers than traditional RSA or finite-field discrete-log systems. The material gives 160- or 256-bit examples for elliptic curves, compared with RSA moduli that can be thousands of bits. Smaller key sizes can make these systems more efficient.

Hope
Two familiar curve names are Curve25519 and secp256k1, the latter used in Bitcoin transactions. But the source raises an important future concern: sufficiently capable quantum computers could undermine the mathematical problems behind these commonly used public-key methods. That’s why researchers are working toward approaches designed to resist quantum attacks.

Corey
Now let’s separate the three jobs public-key cryptography can help with. First is confidentiality: Bob encrypts for Alice using Alice’s public key, and Alice decrypts using her private key. Second is key exchange or key transport: Bob can protect a symmetric key for Alice. Third is proof of identity through a digital signature.

Hope
Why not just use Alice’s public key to encrypt every byte of a huge file? Public-key operations are relatively computationally expensive, so they’re generally not the practical choice for large amounts of data. Instead, systems commonly use symmetric encryption for the bulk data and public-key cryptography to protect or establish the symmetric key.

Corey
For a signature, Bob hashes the message and signs that hash with his private key. Alice independently hashes the message she received, then verifies Bob’s signature using Bob’s public key. If verification succeeds and the hashes correspond, she has evidence that the message was signed using the private key associated with Bob’s public key, and that the signed content has not been altered.

Hope
That’s different from encrypting for confidentiality. A signature is checked with the signer’s public key; it doesn’t hide the message. Also, the public key needs to be reliably linked to the person or organization it claims to represent. Otherwise, an impostor could simply give Alice a different public key and claim it belongs to Bob.

Corey
That’s where digital certificates come in. A certificate binds information such as an identity to a public key, and it is digitally signed by an entity the recipient trusts. If Alice trusts that signing authority, she can check the certificate’s signature and use the certified public key as Bob’s.

Hope
The private key is not something Bob should distribute in a public certificate. He keeps it secret, often in protected storage. He can distribute a certificate containing his public key, while Alice can keep her own private key securely and maintain a collection, or key ring, of public keys she uses.

Corey
A key ring can save Alice from having to request a public key every time she communicates with someone. She might obtain Bob’s key from a trusted certificate, or from a repository or direct exchange, but she still needs a reliable way to check that the key really belongs to Bob.

Hope
Let’s work through the RSA setup. Choose two primes, p and q, and multiply them to get n, the modulus. The security relies on the difficulty of factoring n when p and q are sufficiently large. The source’s simplified example uses tiny primes so we can follow the arithmetic, but those sizes would not be secure in real use.

Corey
RSA encryption raises a message number m to an exponent e and reduces the result modulo n: the ciphertext is m to the power e, modulo n. Decryption raises the ciphertext to a different exponent d, again modulo n, to recover the message. Modulo n means we keep the remainder after division by n.

Hope
To choose the exponents, calculate phi of n as (p minus 1) times (q minus 1). Choose e so that it has no common factor with phi; in other words, their greatest common divisor is 1. Then choose d so that e times d leaves remainder 1 when divided by phi. So d is the modular inverse of e modulo phi.

Corey
Here’s the small example. Let p equal 3 and q equal 11. Then n is 33, and phi of n is 2 times 10, or 20. Choose e equal to 3; since 3 and 20 share no factor, that choice works.

Hope
Now choose d equal to 7, because 3 times 7 is 21, which leaves remainder 1 modulo 20. The public key in this example is the pair (3, 33), and the private key is (7, 33). Encrypt by raising the message to 3 modulo 33, then decrypt by raising the result to 7 modulo 33.

Corey
The numbers are deliberately small enough to understand, not to protect anything. Real RSA requires carefully chosen large primes and secure implementation. The example shows the relationship between the exponents, but it isn’t a recipe for building a secure real-world system.

Hope
Elliptic-curve cryptography takes a different route. One common form of the curve equation is y squared equals x cubed plus a times x plus b, modulo a prime p. Working modulo p means the arithmetic is performed over a finite set of values, rather than treating the curve as an ordinary continuous graph.

Corey
The curve is defined by parameters that include a, b, and p, along with a base point G. The order describes the size of the relevant point group. Bob and Alice can agree on these public parameters and the base point, while each keeps a private number secret.

Hope
Bob’s public point P is calculated as his private number multiplied by G, written P equals n times G. That multiplication means repeatedly adding the point G according to the curve’s rules. Calculating P from n and G is practical; recovering n from P and G is the hard problem the system relies on.

Corey
That private number can be used for signing. In ECDSA, the Elliptic Curve Digital Signature Algorithm, the signer uses a private key to create a signature, and others use the corresponding public key to verify it. Secure implementations also need appropriate random values for signing.

Hope
Bitcoin uses secp256k1 for its elliptic-curve operations. When someone creates a transaction, they sign it with a private key; others can verify the signature using the corresponding public key. The public-key information lets the transaction be checked without revealing the private key.

Corey
One terminology distinction is worth keeping clear: ECDSA is a digital-signature scheme, while ECDH—Elliptic Curve Diffie–Hellman—is a key-agreement method. Both use elliptic-curve mathematics, but they have different jobs. ECDH lets two parties establish shared secret material; ECDSA lets a signer create signatures that others can verify.

Hope
Finally, let’s combine the ideas in a PGP-style example. Bob starts with a message, hashes it, and signs the hash with his private key. He attaches that signature to the message. The signature supports verification of the sender and the message’s integrity, while encryption will provide confidentiality.

Corey
Because the message might be large, Bob generates a fresh symmetric key for this message and encrypts the message-plus-signature with that key. Then he encrypts the symmetric key using Alice’s public key. He sends Alice both the encrypted data and the encrypted symmetric key.

Hope
Alice uses her private key to recover the symmetric key, then uses that symmetric key to decrypt the message and signature. She hashes the message herself and verifies Bob’s signature with Bob’s trusted public key. Successful verification tells her the signed message matches what Bob signed, while the symmetric encryption kept the contents confidential in transit.

Corey
So the whole picture is: public-key methods establish trust, protect or establish symmetric keys, and support signatures; symmetric encryption handles large data efficiently. RSA, discrete-log methods, and elliptic-curve methods rely on different hard mathematical problems, and their safe use depends on suitable parameters, protected private keys, and trustworthy public-key distribution.

Ask anything
We've read this whole study set, so questions about it get the best answers.