Crooked Crown RSA private key too small, Wiener's attack

crypto
flag L3m0n{06c7963f81c3c1fdd8be524c0aa3d0cb}

A single output.txt with a 2048-bit RSA public key (n, e) and one ciphertext c. The flavor text does all the work if you read it as a spec sheet instead of a story: "the smith... made a small adjustment... nothing that showed from the outside... just something that let the key turn a little faster." A key that "turns faster" is a key with a smaller private exponent d - which is exactly the vulnerability Wiener's attack targets.

Hint

2048-bit modulus, ordinary-looking public key, but the story insists the private key is unusually "fast." Think about what makes RSA decryption fast: a small d.

The tell

output
n = 2537744770...0098004943    (2048-bit)
e = 1850496403...0060743259841  (full-size, nothing obviously wrong)

e looks completely ordinary - huge, no small factors jumping out. That's expected: Wiener's attack doesn't need a suspicious e, it needs a suspiciously small d, and a small d combined with a large e still produces an e that looks random. The structural weakness is invisible until you actually run the attack.

Insight

Wiener's attack recovers d from (n, e) alone whenever d < n^0.25 / 3 roughly, by expanding e/n as a continued fraction and testing each convergent k/d as a candidate for e*d - 1 = k*phi(n). Once a convergent makes phi come out as an integer, plugging phi = n - p - q + 1 back into the quadratic x^2 - (n - phi + 1)x + n = 0 gives p and q directly - no factoring needed, no brute force over primes.

python
1from fractions import Fraction
2import math
3
4def cf(a, b):
5    while b:
6        yield a // b
7        a, b = b, a % b
8
9def convergents(seq):
10    num, den = [0, 1], [1, 0]
11    for a in seq:
12        num.append(a * num[-1] + num[-2])
13        den.append(a * den[-1] + den[-2])
14    return list(zip(num[2:], den[2:]))
15
16for k, d in convergents(list(cf(e, n))):
17    if k == 0 or (e * d - 1) % k:
18        continue
19    phi = (e * d - 1) // k
20    b = n - phi + 1
21    disc = b * b - 4 * n
22    if disc < 0:
23        continue
24    s = math.isqrt(disc)
25    if s * s == disc:
26        p, q = (b + s) // 2, (b - s) // 2
27        if p * q == n:
28            print("d =", d, "\np =", p, "\nq =", q)
29            break

This hits on one of the earliest convergents - the private exponent really is small enough that continued-fraction expansion finds it in a handful of terms, well before the search space gets anywhere near brute-forceable territory.

Decrypting

With p, q recovered, the rest is textbook RSA:

python
1phi = (p - 1) * (q - 1)
2d = pow(e, -1, phi)
3m = pow(c, d, n)
4print(m.to_bytes((m.bit_length() + 7) // 8, 'big'))
output
CTF-VAULT/1.0
ID: CROOKED-CROWN-012
NOTE: a crown cut too short still fits the head that shaped it.
PAYLOAD-B64: TDNtMG57MDZjNzk2M2Y4MWMzYzFmZGQ4YmU1MjRjMGFhM2QwY2J9
CHECK: 24c10c070552a76f
END

Note

The "note" line is the author winking at the exact attack: a short key ("crown cut too short") still "fits the head that shaped it" - i.e. still decrypts fine, because small d was the smith's shortcut, not a flaw in the vault's shape (n is a normal 2048-bit modulus the whole time).

Base64-decode the payload field:

output
L3m0n{06c7963f81c3c1fdd8be524c0aa3d0cb}

Pitfall

There's no brute force and no partial-factorization trick here - resist the urge to reach for Fermat's method (close primes) just because it's also an "RSA plus flavor text" challenge. The giveaway phrase is about speed of the key, not closeness of the primes; that specifically points at Wiener, not Fermat.