If the modulus N is a prime, then we can directly compute ϕ=N−1.
How to verify whether N is a prime or not? Use sympy.ntheory.primetest.isprime.
from Crypto.Util.number import inverse, long_to_bytesfrom sympy.ntheory.primetest import isprimefrom sys importexit#--------Data--------#N =<N>e =<e>c =<c>#--------Primality test--------#ifisprime(N):print("N is a prime.")else:print("N is not a prime.")exit()#--------RSA decryption--------#phi = N -1d =inverse(e, phi)m =pow(c, d, N)flag =long_to_bytes(m).decode()print(flag)
Repeated Prime Factor
If the modulus is a perfect square of some prime p, i.e. N=p2, then we can simply compute p=N.
If p is found, then we have ϕ=p⋅(p−1).
How to verify whether N is a perfect square or not? Use sympy.ntheory.primetest.is_square.
from Crypto.Util.number import inverse, long_to_bytesfrom sympy.ntheory.primetest import is_squarefrom sympy import rootfrom sys importexit#--------Data--------#N =<N>e =<e>c =<c>#--------Perfect square test--------#ifis_square(N):print("N is the square of some prime p.")else:print("N is not a square.")exit()#--------RSA decryption--------#p =int(root(N, 2))print(f"{p = }")phi = p * (p -1)d =inverse(e, phi)m =pow(c, d, N)flag =long_to_bytes(m).decode()print(flag)
Twin Primes
"Twin primes" means N=p⋅q and q=p+2. The value of pand qcan be easily computed using quadratic formula. The detailed derivation can be found here: