Chris Spackman's NeoCities Page

Public-Private Key Pair Generation

This demonstration shows how public and private key pairs are created. Users can put in their own numbers to see the results, or just use the defaults. (The button is below Step 2.)

The Padlock Analogy

Imagine Alice buys 100 identical padlocks that all open with the same key. She keeps the key (her private key) and sends open padlocks to everyone she knows. When Bob wants to send Alice a secret message, he puts it in a box and snaps one of Alice's padlocks shut. Now only Alice can open it, because only she has the key. That is basically how public-key encryption works: the padlock is the public key (anyone can lock with it), and the key is the private key (only Alice can unlock).

Step 1: Input Values

Use the defaults, or enter your own values for the two prime numbers p and q. p and q MUST be prime numbers.

— use these small values to follow along with a calculator. The numbers are small enough to check by hand!

Here, we use 17 and 65537 for e because these meet some mathematical requirements and are frequently used.

Also, please note that the default values for p and q might look big but are actually too small for real security and are used for demonstration purposes only.

Step 2: Results

After doing the math (button below), the results will show:

Why This Works

The public key and private key are like a pair of linked locks. The public key is used to "lock" (encrypt) messages, and only the private key can "unlock" (decrypt) them. Similarly, the private key can "lock" (sign) messages, and the public key can verify the signature.

This connection between the public and private keys is based on some advanced math. Even if someone has the public key, it's nearly impossible to figure out the private key unless they know the two secret prime numbers (p and q) that were used to create the keys. Trying to find them by brute force would take millions of years.

Things to Try

In The Real World

In real-world key pair generation, the numbers used are much larger than what we use here, for security reasons:

p and q
There are very, very large prime numbers — from around 150 to about 600 digits long. Best practice is for them to be roughly the same length.
n
The product of p and q. A gigantic number with only 2 factors, aside from itself and 1. Even knowing that it is the product of two primes, for well-chosen primes, it would take millions of years to brute force factor n.

FAQ

Why prime numbers?
Prime numbers ensure that the computations result in keys that cannot be easily factored by an attacker.
Why large numbers?
The larger the numbers, the harder it is for an attacker to figure out the private key from the public key.
How do they find large primes to use?
Turns out, it is a lot easier to check if a number is prime or not than it is to try to actually factor a similarly large number. So, computers randomly come up with a huge number, and check if it is prime. If it is, great! If not, they can tweak the number and check again. The software continues until it has a number it is fairly certain is prime.
Where does the decryption key (d) come from?
It's created as part of the math that connects the public and private keys. It is like the partner to the encryption key (e). The (slightly complicated) math ensures that they work together.
Why can't someone figure out the private key from the public key?
The private key depends on the secret prime numbers (p and q) that were used to create the public key. Without knowing these primes, the math to find the private key would take so long that even the fastest computers couldn't do it.
Can I use the public / private keys from this page for real encryption?
NO!!!!!!
Okay, technically, maybe, but there is a lot more to creating real keys than we show here. This page demonstrates the math behind the keys. Real keys have a specific format that also includes information about the user, when the key should expire, and much more.

Related

See also: Diffie-Hellman Key Exchange — how two people can agree on a shared secret over an insecure channel, using similar math.

Or step back to the classical methods that came first: Traditional Cryptography · Early Modern Cryptanalysis.

Disclaimer: these pages are educational demos provided as-is, with no warranty of any kind. The author is not responsible for any consequences arising from their use.

Send comments and bug reports to chris@chrisspackman.com.

Last updated: 2026-02-21

This page is Copyright © 2025 Chris Spackman.
This web site developed entirely on GNU/Linux with Free / Open Source Software.

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Creative Commons License