WeCodeAcademy

Public-Key Cryptography

Public-key (asymmetric) cryptography uses a pair of keys. What one locks, only the other unlocks.

  • Anyone can lock a message with your public key; only your private key opens it. That solves key sharing: you can publish the public key openly.
  • Reverse the roles for a digital signature: you sign with the private key, and anyone with the public key can verify the signature came from you.

It rests on maths that is easy one way and hard to reverse, for example multiplying two big primes is easy but factoring the product is not. Below is toy RSA with tiny numbers so you can see the idea. Real keys use primes hundreds of digits long.

Examplepython
Open in editor »
Quick check

Which key do you keep secret in public-key cryptography?

Your turn: write the code

Using the same toy keys (n = 3233, e = 17), encrypt the number 100 and print the result.

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.