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 is a prime, then we can directly compute .
How to verify whether is a prime or not? Use
sympy.ntheory.primetest.isprime.
Repeated Prime Factor
If the modulus is a perfect square of some prime , i.e. , then we can simply compute .
If is found, then we have .
How to verify whether is a perfect square or not? Use
sympy.ntheory.primetest.is_square.
Twin Primes
"Twin primes" means and . The value of and can be easily computed using quadratic formula. The detailed derivation can be found here:
Multi-prime
If the modulus is 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?
