# WRITE UP - CSTV CTF 2023 ## Crypto: ### RSA: > Solve by i4mh3nry (aka tuankiet.trinh0r) Here, the exam show you **ciphertext** vĂ  **mykey.pem** and after read python file you'll know that the datas were stored in **ciphertext**. Now, opening the **mykey.pem** i saw the `PUBLIC KEY` ![image](https://hackmd.io/_uploads/B1u2MhLOa.png) Researched for few minutes, i found this [WEB](https://travistidwell.com/blog/2013/09/06/an-online-rsa-public-and-private-key-generator/) ![image](https://hackmd.io/_uploads/SJhXX28dp.png) And i know that `PUBLIC KEY` has been made from `SECRET KEY` now you just find the `SECRET KEY` The program create `SECRET KEY` from `PUBLIC KEY` ```py= from Crypto.PublicKey import RSA import sympy with open('mykey.pem', 'rb') as f: key = RSA.import_key(f.read()) n = key.n p, q = sympy.factorint(n) phi = (p - 1) * (q - 1) d = pow(key.e, -1, phi) private_key = RSA.construct((n, key.e, d, p, q)) # Export with open('private_key.pem', 'wb') as f: f.write(private_key.export_key('PEM')) ``` Done ! Now, just decrypt RSA ```python= from Crypto.Cipher import PKCS1_v1_5 from Crypto.PublicKey import RSA with open('private_key.pem', 'rb') as key_file: private_key = RSA.import_key(key_file.read()) with open('ciphertext', 'rb') as cipher_file: ciphertext = cipher_file.read() cipher = PKCS1_v1_5.new(private_key) decrypted_message = cipher.decrypt(ciphertext, None) print(decrypted_message.decode('utf-8')) ``` You had the **FLAG** lol~ ![image](https://hackmd.io/_uploads/rJzwBhLOa.png) > Flag: CSTV_2023_{408f8e7210d60d78caac8ff6b204459276411da1}