Implementation of Wiener's RSA attack
Implementation of Wiener's RSA attack: recover e from d and N, recover d from e and N, and recover prime factors from (d, N) or from (e, d, N) when the RSA key is vulnerable to Wiener's continued-fraction attack.
This repository contains a straight-forward Python implementation of Wiener's attack on RSA. The attack exploits cases where the private exponent d is unusually small relative to the modulus N (more precisely, when d < N^{0.25} under certain conditions), using continued fractions to recover the private key or factors.
The code in this repository includes functions to:
- recover a small
dgiven(e, N); - recover a small
e(ifdis known and large) and the prime factorsp, q; - recover the prime factors
p, qgivene, d, Nby searching for a validksuch thate*d = k*phi(N) + 1.
- Pure Python implementation (no heavy external dependencies).
- Uses continued fractions to generate convergents and test candidate keys.
- Provides small helper functions to get
e,d, and prime factors when keys are vulnerable.
This README documents the main functions which were taken from the original implementation. The code expects two helper modules to be available in the import path:
ContinuedFractions— functions for converting rationals to continued fraction and computing convergents.Arithmetic— helper utilities such asis_perfect_square.
The original script used a local sys.path append (see the repository code). Ensure that the helper modules (
ContinuedFractions.pyandArithmetic.py) are present in the repo or available onPYTHONPATH.
- Python 3.8+
- Standard library modules:
sys,math. - Local modules:
ContinuedFractions,Arithmetic(provided with the repository or implemented separately).
No third-party packages are required by the core algorithm.
Clone the repository and ensure the helper modules are available:
git clone <your-repo-url>
cd WienerAttackRSA
# Make sure ContinuedFractions.py and Arithmetic.py are in the same folder or on PYTHONPATHYou can run the functions directly from the files or import them from a package entrypoint if you arrange the code as a proper Python package.
Import the functions from the module and call them from your script or an interactive shell.
# sample usage
from WienerAttackRSA import wiener_attack, get_small_d, get_small_e_and_factors, get_factor
# Example placeholders for e, d and N (replace with actual integers)
e = 65537
N = 0x00 # replace with modulus
# Try to find a small private exponent d from (e, N)
d = get_small_d(e, N)
if d is not None:
print("Found d:", d)
# If you already have d and N, try to recover e and/or p, q
# e_recovered, p, q = get_small_e_and_factors(d, N)
# If you have e and d, attempt to factor N
# p, q = get_factor(N, e, d)A simple illustrative example (this is not a real vulnerable RSA key — replace with values that are known to be vulnerable if you want to test):
from WienerAttackRSA import get_small_d, get_factor
# vulnerable example values (toy)
e = 17993
N = 90581
d = get_small_d(e, N)
print('Recovered d:', d)
if d:
p, q = get_factor(N, e, d)
print('p, q =', p, q)All functions return None (or tuples containing None) when they fail to find a vulnerable key.
-
Description: Convenience wrapper that attempts to recover
dand then factors. -
Parameters:
e— public exponent (int)N— RSA modulus (int)max_k— maximumkvalue to try when derivingphi(N)from the equatione*d = k*phi(N) + 1(int, default2**20in wrapper)
-
Returns:
(p, q)or(None, None)if unsuccessful.
- Description: Given a (large)
dandN, attempts to find a smalleand return(e, p, q). - Returns:
(e, p, q)on success, otherwise(None, None, None).
- Description: Applies Wiener continued fraction attack to find the private exponent
dgiven(e, n). - Returns:
d(int) if found, otherwiseNone.
- Description: Given
e,d, andN, tries smallkvalues to reconstructphi(N)and solve for prime factorsp, q. - Parameters:
max_k(default 1000) is the search bound fork. - Returns:
(p, q)on success, otherwise(None, None).
Wiener's attack uses continued fraction convergents of e/N (or d/N depending on which value is known) to produce rational approximations k/d (or k/e) that can reveal small private exponents. If the private exponent d is small enough compared to N (typically d < N^{0.25} under classical assumptions), one of the convergents will yield values satisfying e*d - 1 = k*phi(N) and from that phi(N) then factors p and q can be computed.
- This attack only works when the RSA key is weak in the sense described above (small
d). It does not break properly chosen RSA keys. - Use this code only for educational purposes, testing with your own keys, or authorized security research. Do not use it to attack keys you do not own or are not permitted to test.
Contributions are welcome! If you want to improve the code, please:
- Fork the repository.
- Create a feature branch.
- Add tests for new behavior.
- Open a pull request describing your changes.
This project is provided under the MIT License — please include an appropriate LICENSE file in the repository if you adopt this README.