Wiener attack
Because the decryption on the server was working very slowly, the engineer made an "optimization": the private exponent d was deliberately chosen to be small (since the speed of pow(c, d, n) depends on the length of d). Now e is very large, and d is up to 200 bits.
In 1990, Michael Wiener proved that if d < ⅓·n1/4, RSA is completely broken. Reason: from the relationship e·d − k·φ(n) = 1, the fraction k/d becomes very close to e/n, so it will be one of the convergents of the continued fraction expansion (continued fraction) of e/n.
Attack steps:
- Expand
e/ninto a continued fraction (the quotient parts of the Euclidean algorithm). - For each convergent
k/d, calculateφ = (e·d − 1) / k(must be an integer). - Solve the equation
x² − (n − φ + 1)·x + n = 0; if the discriminant is a perfect square —dis found. - Now
m = pow(c, d, n).
The public key and ciphertext are in the server.txt file: n, e, c. The decrypted bytes directly give the flag.
Check the discriminant with math.isqrt; float is useless here too.