Asymmetric Cryptography Explained: Public Keys, Private Keys, and RSA
Asymmetric cryptography solves one of the hardest problems in security: how two parties can communicate securely without first sharing the same secret. Instead of a single shared key, it uses a mathematically linked pair of keys: one public, one private.
The public key can be distributed widely. The private key must remain secret. What makes the system useful is the trapdoor property: the public-key operation is easy to perform, but reversing it without the private key is computationally infeasible.
1. What asymmetric cryptography is really doing
Asymmetric cryptography is used for three big jobs:
- Confidentiality: encrypt with a public key, decrypt with the matching private key.
- Authenticity: sign with a private key, verify with the matching public key.
- Key exchange: safely establish a shared secret that can later be used with a fast symmetric cipher like AES.
Confidentiality
Alice locks with Bob's public key
Bob publishes a public key. Alice uses it to encrypt a message. Only Bob's private key can open that ciphertext.
Authenticity
Bob signs with his private key
Bob signs a digest of the message with his private key. Anyone with Bob's public key can verify it really came from him and was not modified.
2. How the public key and private key are created
The exact process depends on the algorithm. In RSA, both keys are derived from the same hidden arithmetic structure. In elliptic curve systems, the private key is a random number and the public key is a point computed from it. The crucial pattern is the same: generate a secret first, then derive a public value that is easy to share but hard to invert.
For RSA, key generation works like this:
1. Prime generation
A cryptographically secure random number generator picks large candidate numbers, and probabilistic primality tests such as Miller-Rabin are used until two strong primes p and q are found.
2. Build the modulus
Multiply the primes to form n = p x q. This modulus becomes part of both keys and defines the arithmetic space used by RSA.
3. Compute phi(n)
For textbook RSA, phi(n) = (p - 1)(q - 1). Real libraries may use Carmichael's function instead, but the idea is the same: we need the size of the invertible set modulo n.
4. Choose the public exponent
Pick e so that gcd(e, phi(n)) = 1. The common choice is 65537 because it is large enough for safety and small enough for fast verification and encryption.
5. Compute the private exponent
Solve d = e^-1 mod phi(n) with the Extended Euclidean Algorithm. This value is secret because it lets us reverse the public-key operation.
6. Package the keys
The public key is (n, e). The private key is (n, d), often with extra CRT values dp, dq, and qInv to speed up decryption and signing.
Private key = (n, d)
That shared modulus n is not the secret. The secret is the knowledge needed to derive d, which comes from the hidden factorization of n. If an attacker can factor n into p and q, the system is broken. That is why RSA uses very large numbers.
How ECC key pairs are created
Elliptic Curve Cryptography is conceptually simpler to describe:
- Pick a random private scalar
d. - Multiply the curve's base point
Gbydto get the public keyQ = dG. - Publish
Qand keepdsecret.
Recovering d from Q is the elliptic curve discrete logarithm problem, which is believed to be hard. This is why ECC can often deliver similar security with much smaller keys than RSA.
3. Common asymmetric algorithms and where they fit
RSA
Encryption, key transport, and digital signatures
Security comes from the difficulty of factoring a very large modulus n back into p and q.
Elliptic Curve Cryptography
TLS, Signal, cryptocurrencies, and compact signatures
A random private scalar is chosen, and the public key is produced by scalar multiplication on an elliptic-curve base point.
Diffie-Hellman / ECDH
Shared secret establishment
Each party publishes a public value derived from a private secret, and both sides independently derive the same shared key without sending it directly.
In practice, asymmetric algorithms are usually not used to encrypt entire files or long chat histories directly. They are slower than symmetric ciphers. A common pattern is hybrid cryptography: use RSA or ECDH to protect a temporary secret key, then use AES or ChaCha20 to encrypt the bulk data efficiently.
4. RSA in detail
RSA is the classic example because its structure is visible and easy to teach. Real RSA uses very large integers and padding schemes such as OAEP and PSS, but the core arithmetic is still the same.
Encryption
Anyone with the public key can transform plaintext M into ciphertext C, but only the private key holder should be able to reverse it safely.
Decryption
The private exponent d undoes the public-key operation because d was chosen as the modular inverse of e.
Signing
The signer applies the private key to a hash of the message, proving possession of the secret key without revealing it.
Verification
Anyone can use the public key to verify that the signature matches the expected digest and was not forged or altered.
Worked RSA example with actual numbers
The values below are intentionally tiny so we can calculate them by hand. They are not secure, but they clearly show how the math fits together.
That gives us:
- Public key:
(3233, 17) - Private key:
(3233, 2753)
Encryption example
Let the plaintext be M = 65.
So the ciphertext sent across the network is 2790.
Decryption example
The private exponent reverses the public operation and recovers the original message.
Signature example
Suppose the hash of a message is 42. Bob signs that digest with the private key:
Anyone can verify using the public key:
Because the verified value matches the expected digest, the signature is valid.
5. Detailed algorithm summary
RSA key generation algorithm
- Choose large random primes p and q.
- Compute n = p x q.
- Compute phi(n) = (p - 1)(q - 1).
- Choose e such that 1 < e < phi(n) and gcd(e, phi(n)) = 1.
- Compute d such that d x e mod phi(n) = 1.
- Publish (n, e) and keep (n, d) secret.
RSA encryption algorithm
- Encode the plaintext as an integer M in the range 0 to n - 1.
- Apply secure padding such as OAEP in real systems.
- Compute C = M^e mod n.
- Send ciphertext C.
RSA decryption algorithm
- Receive ciphertext C.
- Compute M = C^d mod n.
- Remove the padding structure.
- Decode M back into the original plaintext bytes.
RSA signing and verification algorithm
- Hash the message with a secure hash function.
- Sign the hash using the private key: S = H(M)^d mod n.
- Verify by computing S^e mod n with the public key.
- Accept only if the result matches the expected hash and padding format.
6. Important security notes
- Never use textbook RSA directly. Modern systems require padding such as OAEP for encryption and PSS for signatures.
- Use large keys. Tiny values are useful for teaching only. Production RSA commonly uses 2048-bit or 3072-bit moduli.
- Protect the private key operationally. If the key leaks through malware, logs, backups, or side channels, the mathematics no longer helps.
- Use asymmetric crypto as part of a system. TLS, SSH, code signing, and messaging protocols combine it with hashes, certificates, random number generation, and symmetric encryption.
Conclusion
Asymmetric cryptography works because one direction of the math is easy and the reverse direction is hard without secret information. That idea gives us public-key encryption, digital signatures, and secure key exchange.
RSA is the clearest place to see the mechanism: generate two primes, derive n, choose e, compute d, then use the public key for open operations and the private key for secret ones. Once that foundation clicks, the rest of modern cryptography becomes much easier to reason about.