Recover the File Key
medium · 100 XP · flag · free · solves: 0
A records office sealed a transfer note in a hybrid envelope. A fresh AES file key encrypts the note, and RSA wraps the file key for the office. The office key generator picked two primes that are close together.
Envelope format (envelope.json, binary fields are Base64):
rsa_public_key: the 2048-bit modulusn(decimal string) ande = 65537.key_wrap: the 32-byte file key, wrapped with RSA-OAEP. OAEP uses SHA-256, MGF1 with SHA-256, and an empty label.payload: AES-256-GCM with a 12-byte nonce, a 16-byte tag, and associated data. The associated data is the 37 ASCII bytesarchive-envelope-v1|to=records-office.
Your task
- Factor
nwith Fermat's method. Start ata = isqrt(n) + 1. A perfect square appears in fewer than 10000 steps. - Rebuild the private key:
d = inverse(e, (p - 1)(q - 1)). - Unwrap the file key, then check the GCM tag and decrypt the payload.
- Open
envelope_bad_tag.jsonwith the same file key. Its tag has one changed bit, so decryption must fail withInvalidTag. Never use output from a failed tag check.
envelope_tools.py loads the envelope, unwraps the key, and opens the payload. It needs the Python package cryptography (pip install cryptography). You write the Fermat step and the private exponent yourself.
Check each step with verify.py: factors <p> <q>, key <hex>, and plaintext <file>. The decrypted note contains the flag in the form CSEMA{...}. Submit it.
Tags: learn
Try next
- A Centuries-Old Message (easy)
- The Board Nobody Read (easy)
- Trail of Ciphers (easy)