Rating:
Just use Fermat's theorem decomposition and you will get the answer:
```
import gmpy2
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import Crypto.Util.number as number
with open("public.pem", "rb") as f:
key = RSA.import_key(f.read())
public_key = key.publickey()
def fermat(n):
a = gmpy2.isqrt(n) + 1
b = a**2 - n
while not gmpy2.iroot(b, 2)[1]:
a += 1
b = a**2 - n
b = gmpy2.iroot(b, 2)[0]
return (a + b, a - b)
# p, q = fermat(public_key.n)
q = 4035344634524837717521915201305975516098722420219128355538063452416706649582040976771180219125686195204822338707859330665951615120601874544633270967788027074091717031306682541328304029835373501410605229741692482939694335870993275374022062842280710959945654503477963936519342817858077479358738644573785487521029281727169737762573882938206926732178158574479009658125467551018805835614097299871918962876012823726564585700892649184624360581540320684057939677927710690697605112273648424114803479675168145732275761455167827091548475299338153944131864072448859112796081669111927011416022032734279963320442672954117725635057
p = 4035344634524837717521915201305975516098722420219128355538063452416706649582040976771180219125686195204822338707859330665951615120601874544633270967788027074091717031306682541328304029835373501410605229741692482939694335870993275374022062842280710959945654503477963936519342817858077479358738644573785487521032731949949672190534185116624273887980672650136436463485817603675820435108916629224182933010010760147581441906729024860231015150938247223056724681089282171956429028890246653926215568565285817362035961064914955470989239448342478747578795441253265938399505855471220563759562310196608723984550303265351501013993
e = 7
# Calculate n and d, where n is the RSA modulus and d is the private key index
n = p * q
phi = (p - 1) * (q - 1)
d = pow(e, -1, phi) # Using the pow function to calculate the inverse of e
#Generate RSA key pairs
key_pair = RSA.construct((n, e, d, p, q))
# Using RSA to decrypt ciphertext
cipher_rsa = PKCS1_OAEP.new(key_pair)
with open("key_gen_flag.bin", "rb") as f:
cipher_text = f.read()
decrypted_text = cipher_rsa.decrypt(cipher_text)
print(decrypted_text)
```