A Python script to perform a small plaintext attack on RSA encryption. It recovers the plaintext (m) from a ciphertext (c) when the plaintext and public exponent (e) are small. Useful for CTF challenges or educational purposes.
- Python 3.x
gmpy2library:pip install gmpy2
- Set
c(ciphertext),e(public exponent), andn(modulus) in the script. - Run:
python small_plaintext_attack.py - Output: Plaintext as an integer and, if possible, an ASCII string (e.g., a CTF flag).
from gmpy2 import iroot
c = 27 # Ciphertext
e = 3 # Exponent
n = 100 # Modulus
m, exact = iroot(c, e)
if exact and pow(m, e, n) == c:
print("Plaintext:", m)
else:
print("Attack failed.")