CIDADAXSEC CTF
← Back to CTF
CryptoHard500 points

RSA with neighbouring primes

The two RSA primes are too close, so Fermat factorisation works quickly.

The Nordvik hardware team generated an RSA keypair in a hurry. They picked two 256-bit primes that are close together. The values <code>n</code>, <code>e</code>, and the ciphertext are available; the key is not.

Objective

Factor <code>n</code>, compute the private exponent, then decrypt the ciphertext into the flag.

Useful tools
Python (math.isqrt)RsaCtfToolSageMath (optional)factordb
Files
rsa.txt
Download
Hints
Hint 1
Open rsa.txt. n is the product of two close 256-bit primes. Fermat factorisation tries a = ceil(sqrt(n)) and b² = a² - n.
Hint 2
Python: a = isqrt(n); b2 = a*a - n, increase a until b2 is a perfect square. Then p = a-b, q = a+b, d = pow(e, -1, (p-1)*(q-1)).
Hint 3
Decrypt: m = pow(c, d, n), then m.to_bytes((m.bit_length()+7)//8, "big").decode(). The result is the flag. Alternative: RsaCtfTool in Fermat mode.