CTF writeup: RACTF 2020's Really Simple Algorithm ships RSA with p and q included. Compute the private exponent and decrypt; the factoring department has the day off.
Now, following wikipedia article, one could understand, in order to solve the challenge, one need to follow the following steps :-
calculate n = p*q
calculate phi of n which is euler’s totient which denotes the number of positive integers, which are relatively prime (GCD=1) with n.
In our case, it is calculated as phi = (p - 1) * (q - 1)
calculate the decryption key d which is modular inverse of encryption key e over phi
It is calculated using extended gcd. For our case, we can find the implementation in gmpy2
d = gmpy2.invert(e, phi)
Once you have d, one can easily calculate plaintext as ciphertext raised to the power d modulo n
pt = pow(ct, d, n)
converting pt from integer representation to a string or byte-string prepresentation
plaintext = bytes.fromhex(hex(pt)[2:]) or equivalently plaintext = int.to_bytes(pt,(pt.bit_length()+7)//8,'big')
print the plaintext, print(plaintext.decode())
Now, this process can be automated too, but since time is not concerned with this task as with the next Really Speedy Algorithm, I like to do it anyways