Prime Factors

FactorDB

Always try factoring the modulus N using factordb-python:

from Crypto.Util.number import inverse, long_to_bytes
from factordb.factordb import FactorDB

#--------Data--------#

N = <N>
e = <e>
c = <c>

#--------FactorDB--------#

f = FactorDB(N)
f.connect()
factors = f.get_factor_list()

#--------RSA decryption--------#

phi = 1
for factor in factors:
    phi *= factor - 1

d = inverse(e, phi)
m = pow(c, d, N)
flag = long_to_bytes(m).decode()

print(flag)

Modulus Itself is a Prime

  • If the modulus NN is a prime, then we can directly compute Ο•=Nβˆ’1\phi = N - 1.

  • How to verify whether NN is a prime or not? Use sympy.ntheory.primetest.isprime.

Repeated Prime Factor

  • If the modulus is a perfect square of some prime pp, i.e. N=p2N = p^2, then we can simply compute p=Np = \sqrt{N}.

  • If pp is found, then we have Ο•=pβ‹…(pβˆ’1)\phi = p \cdot (p - 1).

  • How to verify whether NN is a perfect square or not? Use sympy.ntheory.primetest.is_square.

Twin Primes

"Twin primes" means N=pβ‹…qN = p \cdot q and q=p+2 q = p + 2. The value of ppand qqcan be easily computed using quadratic formula. The detailed derivation can be found here:

Twin primes

Multi-prime

If the modulus NNis the product of multiple primes, it is possible to factor it in a short amount of time:

This factorization can be done with the ecm.factor() function from Sage.

Last updated

Was this helpful?