A mini RSA
The problem
Implement the RSA cipher from scratch with small primes: make a key pair, encrypt and decrypt messages, and show why knowing only the public key is not enough once the primes are large.
Requirements
- Generate a key pair from two primes and a public exponent coprime to phi
- Compute the private key with the extended Euclidean algorithm
- Encrypt and decrypt a message made of numbers below n, using fast modular powers
- Break a key with very small primes by trial division and report how long the work takes in steps
Technologies
- Python 3
- Extended Euclidean algorithm
- pow(a, e, m)
- Prime testing by trial division
Step by step
- Write a prime test and pick two primes p and q, then compute n and phi.
- Write the extended Euclidean algorithm and use it to find d.
- Encrypt and decrypt a list of numbers and check that the messages return.
- Factor n by trial division for small keys and count the steps for primes of growing size.
Expected result
A working toy RSA with a table that shows how the work of breaking the key grows with the size of the primes.
How it is evaluated
- Decrypting an encrypted number returns the original number
- The private key is checked: e times d is 1 modulo phi
- Keys where gcd(e, phi) is not 1 are rejected
- The report shows that the number of steps to factor grows quickly with the size of n
Ways to improve it
- Turn text into numbers and encrypt it in blocks
- Add a digital signature: sign with d and verify with e
- Compare with Fermat's little theorem for finding inverses modulo a prime