Primes Part 2

Infinite Descent

We are given this source code.

#!/usr/bin/env python3

import random
from Crypto.Util.number import bytes_to_long, isPrime

FLAG = b"crypto{???????????????????}"


def getPrimes(bitsize):
    r = random.getrandbits(bitsize)
    p, q = r, r
    while not isPrime(p):
        p += random.getrandbits(bitsize//4)
    while not isPrime(q):
        q += random.getrandbits(bitsize//8)
    return p, q


m = bytes_to_long(FLAG)
p, q = getPrimes(2048)
n = p * q
e = 0x10001
c = pow(m, e, n)

print(f"n = {n}")
print(f"e = {e}")
print(f"c = {c}")

It seems that the chosen prime are close to each other. This prime generation is susceptible to Fermat's attack. If p and q are selected so that (p-q) < N^(1/4), Fermat's attack is feasible.

Here is the code I used to verify Fermat's attack feasibility.

try.py

And here is the solver code.

solve.py

Flag: crypto{f3rm47_w45_4_g3n1u5}

Marin's Secrets

From the title and the source code below, we can infer that the secrets list refers to Mersenne primesarrow-up-right.

Now, we can take the fact that there is only 52 known Mersenne primes out there. Even with the limited options, it is possible for us to limit the options to only the pair whose sum of bit lengths matches the bit length of n. Here is the solver code for this challenge.

Flag: crypto{Th3se_Pr1m3s_4r3_t00_r4r3}

Last updated