Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 41 ยท Cryptography Foundations

Public-Key Cryptography Basics

Understand key pairs, signing versus encryption, why a key replaces an account on a blockchain, and why key size and randomness matter, with a toy RSA example.

A traditional bank account is a row in a database that the bank controls. You prove who you are with a password, and the bank decides what you may do. A blockchain has no bank. Nobody checks your password, and there is no account row to protect. Instead, ownership is a mathematical fact: if you can produce a valid signature for a coin, the coin is yours to move. The tool that makes this possible is public-key cryptography, also called asymmetric cryptography.

This lesson explains the idea without heavy math: what a key pair is, how signing differs from encrypting, why a key can stand in for an account, and why key size and randomness decide whether any of it is safe. You will also run a deliberately tiny RSA example so the modular arithmetic is not magic. The toy numbers here are for learning only and must never protect anything real.

Two keys instead of one

Older, symmetric cryptography uses one secret key for both locking and unlocking. Both sides must already share that secret, which is a chicken-and-egg problem when strangers meet on the internet.

Public-key cryptography splits the secret into a key pair:

  • The private key is a secret number only you know.
  • The public key is derived from the private key and can be shared with everyone.

The pair is built so that the public key is easy to compute from the private key, but going the other way is believed to be infeasible. That one-way gap is the whole trick. It is similar to the one-way behavior of a hash function, except that here a "door" exists for the owner: whoever holds the private key can do things nobody else can.

flowchart LR
    S["Private key<br/>(secret)"] -->|"easy to compute"| P["Public key<br/>(shared)"]
    P -.->|"infeasible to reverse"| S
    S --> SIG["Sign a message"]
    P --> VER["Verify the signature"]

A private key produces the public key easily; reversing that step is the hard problem the whole system rests on.

Signing versus encrypting

Public-key systems offer two mirror-image services, and people often mix them up.

Goal Who uses which key Result
Confidentiality (encrypt) Anyone encrypts with your public key; only you decrypt with your private key Only the owner can read the message
Authenticity (sign) You sign with your private key; anyone verifies with your public key Anyone can check that the owner approved this exact message

Blockchains care almost entirely about the second row. Everything on a public chain is visible to everyone, so there is nothing to keep secret in transit. What the network needs is proof that the owner of some funds authorized a specific transfer. That proof is a digital signature, which you will build in Digital Signatures with a Toy Scheme.

Note: "Encrypting with the private key" is sometimes used as an informal description of signing. It is a misleading shortcut. Real signature schemes are designed for signing, hash the message first, and are not the same operation as encryption. Treat them as two separate tools.

Why a key can replace an account

On a blockchain there is no registration step. You generate a private key on your own device. From it you derive a public key and an address (a short fingerprint of the public key, covered in Keys, Addresses and Wallets). Other people send coins to your address. To spend them, you publish a transaction signed with your private key. Every node can verify the signature using only public information.

This has three consequences worth remembering:

  1. No permission needed. Anyone can create a key pair offline in a moment.
  2. Whoever holds the key is the owner. The network cannot tell a legitimate owner from a thief who copied the key. There is no "forgot password" button.
  3. Losing the key means losing the funds. No administrator can restore access.

Some blockchains also let you rotate keys through smart contracts or recovery schemes, but the base layer works exactly as described.

A toy RSA key pair

RSA is the classic public-key algorithm. Bitcoin and Ethereum do not use RSA (they use elliptic curves, see Elliptic Curves in Plain Language), but RSA is the easiest to demonstrate with plain integers. The recipe, with tiny primes:

  1. Pick two primes p and q. Multiply them to get the modulus n = p * q.
  2. Compute phi = (p - 1) * (q - 1).
  3. Pick a public exponent e that shares no factor with phi.
  4. Compute the private exponent d so that e * d leaves remainder 1 when divided by phi (a modular inverse).

The public key is (n, e). The private key is d (plus the secret primes). Operations are modular exponentiation: raise a number to a power, then keep the remainder after dividing by n. Python's three-argument pow(base, exp, mod) does exactly that efficiently.

Python A toy RSA key pair

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two directions use the same math with the keys swapped. Encrypting with e and decrypting with d hides the message. Signing with d and checking with e proves who produced it. Real RSA adds padding and hashing around these steps, because the bare version leaks structure and can be forged. The goal here is only to show the shape.

Key size: why bigger is safer

The private key is protected only by the difficulty of a math problem. For RSA that problem is factoring n back into p and q. With n = 3233 you can do it in your head. The effort grows quickly as the numbers grow, which is why real RSA moduli have 2,048 bits or more (over 600 decimal digits).

The next example counts how many trial divisions a naive attacker needs for larger and larger moduli built from known primes.

Python Brute-force factoring gets expensive

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each extra digit of the primes multiplies the work by about ten for this naive method. Smarter factoring algorithms exist and are far faster than trial division, so real key sizes must be chosen with them in mind, not with this toy. The lesson to keep is that security is a work factor: a key is safe only if breaking it would take more computing than any attacker can afford.

Elliptic curve keys reach the same security with much shorter keys. A 256-bit elliptic curve key is generally considered comparable to a 3,072-bit RSA key, which is why blockchains prefer them: smaller keys and signatures mean smaller transactions.

Randomness is part of the key

A key is only secret if an attacker cannot guess it. A 256-bit private key has about 10^77 possibilities, but only if all 256 bits are truly unpredictable. If your random number generator is weak, the effective key space can collapse to something an attacker can search.

Python has two very different sources:

  • random is a pseudo-random generator meant for simulations. Given the same seed it produces the same sequence every time.
  • secrets asks the operating system for cryptographically strong randomness and is the right choice for keys.
Python Predictable versus unpredictable randomness

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Throughout this course our simulations use fixed seeds so that outputs repeat and you can compare them with the text. That is fine for teaching and wrong for real keys. Several real-world thefts happened because wallets generated keys from weak or reused randomness.

Warning: never write your own key generator from random, never reuse a key across unrelated systems, and never paste a real private key into a website or a tutorial editor. Use a vetted wallet or library that draws from the operating system.

What public-key cryptography does not do

  • It does not hide the contents of a public blockchain. Anyone can read every transaction.
  • It does not prove who you are in the real world, only that someone holds a given key. Linking a key to a person is a separate problem.
  • It does not protect a key that you store carelessly. Most losses come from stolen or lost keys, not from broken mathematics.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a blockchain, what does a digital signature prove?

    A signature ties a message to a private key. Anyone can check it with the public key. It says nothing about secrecy, real-world identity or inclusion in a block.
  2. 2. Which key do you share publicly?

    The public key (or an address derived from it) is meant to be shared so others can verify your signatures and send you funds. The private key stays secret.
  3. 3. Why is a weak random number generator dangerous for key creation?

    A key is secret only if it is unpredictable. A guessable seed shrinks the search space, so an attacker can regenerate the same key.
  4. 4. What is the main reason real RSA keys use thousands of bits?

    RSA security is a work factor: the larger the modulus, the more computing an attacker needs to factor it. Tiny moduli like 3233 fall instantly.

Summary

  • A key pair has a secret private key and a shareable public key; going from private to public is easy, the reverse is infeasible.
  • Encrypting provides confidentiality; signing provides authenticity. Blockchains rely mostly on signing.
  • A key effectively replaces an account: whoever holds the private key controls the funds, and there is no recovery desk.
  • Security is a work factor, so key size matters. Elliptic curves give strong security with short keys.
  • Keys must come from a secure random source such as secrets, never from a seeded simulation generator.

Next: Digital Signatures with a Toy Scheme