Challenges › Cryptography

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):

Your task

  1. Factor n with Fermat's method. Start at a = isqrt(n) + 1. A perfect square appears in fewer than 10000 steps.
  2. Rebuild the private key: d = inverse(e, (p - 1)(q - 1)).
  3. Unwrap the file key, then check the GCM tag and decrypt the payload.
  4. Open envelope_bad_tag.json with the same file key. Its tag has one changed bit, so decryption must fail with InvalidTag. 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