Im new to Crypto and I am stuck in this challenge where I need to be able to somehow extract p and q.
You are given a python script and an output.txt with the ecnrypted flag and other relevant elemnts
This is the script you are given:
from Crypto.PublicKey import RSA
RSAkeys = RSA.generate(2048)
p = RSAkeys.p
q = RSAkeys.q
n = RSAkeys.n
e = RSAkeys.e
m = b"LetsCTF{<REDACTED>}"
c = pow(int.from_bytes(m, "big"), e, n)
mask = int("55" * 128, 16)
r = p & mask
mask = mask << 1
r += q & mask
print(f"n = {n}")
print(f"e = {e}")
print(f"c = {c}")
print(f"r = {r}")
The problem is I am not able to extract p and q from “n”, “e”, “c” and “r”
I understand the concept behind RSA but I’m still not able to solve it.
So far I’ve tried to make a script and try to get p and q from r.
The initial mask after mask = int("55" * 128, 16)
is of the form 0101 0101 … 0101 (a series of 5 in binary). Then r at this point: r = p & mask
is an integer formed by the even bits of p. After rotating the mask to the left, the odd bits of q are added to r. Then r is an integer whose even bits are the even bits of q, and whose odd bits are the odd bits of q.
I wrote this script to obtain approximations of p and q (half the bits of each):
pa = ""
qa = ""
i = 0
for elem in bin(r).lstrip("0b"):
if i % 2 == 0:
pa+= elem
qa+= '0'
else:
qa+= elem
pa+= '0'
i += 1
pa = int(pa,2)
qa = int(qa,2)
The even bits of r are assigned to pa (approximation of p) and qa is assigned 0 and the other way around for the odd bits.
So far I have not been able to complete pa and qa up to p and q. I know that pq is equal to “n” but I do not know how to modify pa and qa so that paqa converts to n.
Thanks for your attention and sorry for my bad English.
user25421456 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.