by goulov and L0rdComm4nder and mandlebro

Points: 226 (Dynamic, 56 solves)


Primes are fun, don’t google translate me bro.

Given: gyckel.txt



  1. Understand the given 7 lines of code, A prime p with 500 digits is generated. From it a second prime q is constructed by taking the first 250 digits of the first and swaping them with the other 250. Encrypt the text with RSA.

  2. Since the second prime is constructed from the first, we can exploit this dependency, as this structure will be reflected in n=p*q. We can factor n.

  3. Factoring n solves RSA and we can decrypt the flag.


This is a very nice little problem. First, one can see that the primes have the following structure:

  • p = a*10^250 + b
  • q = b*10^250 + a.

As such, n = a*b*10^500 + (a^2 + b^2)*10^250 + a*b.

Schematically, this is:

n = |   1   |   2   |   3   |   4   |
  1000     750     500     250      0   Digits
  • block-1: left-digits of a*b
  • block-2: left-digits of a^2 + b^2 plus right-digits of a*b
  • block-3: right-digits of a^2 + b^2 plus left-digits of a*b
  • block-4: right-digits of a*b

We concatenate block-1 to block-4 to get a*b, and subtract block-4 from block-2 and block-1 from block-3, to recover a^2 + b^2. The only catch to this is that there are carries that can go from block to block (except block-4), but these are not many cases and we can try them all. Only one case will be a solution to the system of equations.

So, by exploiting this structure, we can recover a*b and a^2 + b^2. Solving the system, get a and b, we compute p and q, successfully factoring n. And we can solve RSA!



Solution in