← Back to section · Foundations of Cryptography
RSA Key Generation: How Euler's Totient Forges a Key Pair
Walk through every step of RSA key generation, from choosing primes to deriving the private exponent: why φ(n) must stay secret, why e is almost always 65537, how d comes from a modular inverse — with a small example you can verify by hand.
RSA is the best-known public-key cryptosystem, and all of its magic happens at key generation: between the public encryption exponent and the secret decryption exponent there stands exactly one value of Euler’s totient function. This article takes the generation process apart step by step, explaining where each parameter comes from and what trade-offs it embodies.
Step one: choose two large primes
The generator picks two large primes and at random and sets . In current practice is usually 2048 bits or longer, so and are about 1024 bits each. Choosing a prime is not “pick any large odd number”: candidates must first pass a primality test. In practice this is usually a probabilistic test such as Miller–Rabin — it gives no absolute mathematical proof, but it drives the error probability far below the rate of hardware faults, which is good enough for real systems.
The two primes must also be independent and genuinely random. If two different keys’ moduli share a prime factor — say a random number generator with too little entropy at first boot produced the same — then anyone can take the greatest common divisor of the two public moduli and factor both at once. Two large-scale scanning studies in 2012 did in fact find batches of such vulnerable keys across the internet, all traced to poor randomness on embedded devices. The first lesson of key generation is therefore not mathematics but randomness.
Step two: the totient is the most secret number in the room
Euler’s totient counts the integers between and that are coprime to . When is a product of two distinct primes, there is a clean formula: . The generator computes it trivially, because the generator knows and ; an outsider who knows only must factor first in order to compute .
This deserves emphasis: computing is equivalent in difficulty to factoring . Given , one has together with , and solving the resulting quadratic recovers and ; conversely, anyone who can factor can immediately compute . RSA’s security can thus be summarised in one sentence: is a number only someone who knows the factorisation can compute, and it is exactly the raw material from which the private key is minted.
Step three: choose e, solve for d
The generator picks an integer coprime to as the encryption exponent, then computes , that is, . This modular inverse is found with the extended Euclidean algorithm at negligible cost. The public key is ; the private key’s core is , together with , and , all of which must be guarded carefully.
In practice the choice of is highly convergent: almost every system uses . There are two reasons. First, 65537 is prime, so the chance that it fails to be coprime to is minuscule and retries are almost never needed. Second, 65537 in binary is 10000000000000001, containing only two 1 bits, so modular exponentiation by square-and-multiply needs only about 16 squarings and 1 multiplication — encryption and verification are consequently very fast. The corresponding , by contrast, is an essentially “random” large number modulo , making decryption and signing much slower. This is the source of RSA’s famous asymmetry: verification is quick, signing is slow.
Some historical systems used for even faster encryption, but a small exponent amplifies a family of broadcast and related-message attacks (covered in a later article in this column); unless the padding scheme is applied with extreme care, the trade is not worth it. 65537 is the balance point that forty years of practice has selected between speed and safety.
A side question: how hard is it to find a thousand-bit prime?
First-time readers of RSA often wonder: a prime of a thousand-plus bits sounds as rare as an oasis in a desert — won’t key generation take forever? Not at all, and the reason lies in the prime number theorem: the density of primes near is about . For a 1024-bit integer, is roughly 710, so a randomly chosen odd number is prime with probability on the order of one in seven hundred. A few hundred random draws will hit a prime; trial division by small primes first discards most composites, and a few rounds of Miller–Rabin confirm the survivors. On modern hardware, generating a 2048-bit RSA key pair typically takes well under a second to a few seconds. Primes are sparse on the linear scale but comfortably dense on the logarithmic scale — key generation has never been the engineering bottleneck. The real bottleneck is always randomness quality: candidates must come from an unpredictable source, or “randomly chosen primes” degenerates into “primes chosen from a pool the attacker can also enumerate”, which is exactly how the shared-factor keys of 2012 came about.
A small example you can check by hand
Take and , so and . Choose (coprime to 40) and solve for : , so . Encrypt the message : . Decrypt: . By hand, split it with the Chinese remainder theorem: modulo 5, and ; modulo 11, and . The number that is 2 mod 5 and 7 mod 11 is exactly 7 — the plaintext is back. Every step of this toy example corresponds to the identical arithmetic in the large version; only the scale differs.
In real implementations, the private key is more than d
A real RSA private key file usually stores not just but also , and precomputed Chinese-remainder parameters. Decryption then performs smaller exponentiations modulo and modulo separately and recombines the two results into the answer modulo , for a total speed-up of roughly four times over working directly modulo . The price is that the implementation must be careful: if one step of the CRT computation goes wrong — whether through a hardware glitch or a deliberately injected fault — the difference between the faulty and correct results can leak the factorisation. This is the well-known Bellcore fault attack. Production implementations therefore verify the output of a private-key operation with the public key before releasing it.
One further step is easily skipped: the consistency self-check. Standards (such as the FIPS 140 series’ requirements for cryptographic modules) generally require that a freshly generated key pair be verified internally before use — encrypt a test value with the new public key and decrypt it with the private key, or run a sign-and-verify round trip — and released only if the check passes. This guards against transient faults in the generation process itself: a hardware glitch, a memory error or an implementation defect can produce a pair that “looks normal but does not actually match”, and discovering that only in production, when decryption fails, costs far more than the few milliseconds the check takes at generation time.
Finally, key generation raises custody questions: where should the private key be generated, may it be exported, how are backups encrypted? The algorithm itself only mints the mathematically matched pair of exponents; keeping , , and uncompromised for the key’s whole lifetime is a matter of key management, not mathematics. Once the generation process is understood, every security discussion about RSA returns to the same sentence: factoring is hard, and every shortcut around that fact has to be closed off one by one.
Tags: RSAkey generationEuler totient