Skip to content

dalctf 2026

Fun With RSA

Crypto challenge from DALCTF 2026.

Challenge Description:

Made my own rsa implementation. Hoping its safe enough that no one can crack it. I even xored things to make it safer

the second someone says "I even xored things to make it safer" you know the xor is bullshit and the real bug is somewhere else. and yeah, it was.

the files

output.txt

n = 5216528649875826746373274623709911527907930166016984164533272620692599945581004244622652640509156720922738730017397970929856922904389747851230187154375161
e = 65537
ct = 1769561623039780835813534940346776273175000099891521520378974868184264352157081415449182707720490727295376770632579942515719007563037959358184643750159804
s = 1220125626995232022797746032625764407124557835619605483513423136472322667738087444211114063128377505634674691423794338554652601901132201128484832327199237
spz = 2144640816488997996880355081959057062388718728036090601522167913668193535885347340870230640225749237841665830391473546117213483723045473075426308182703379

the source (trimmed)

chall.py

e = 65537
m = bytes_to_long(FLAG)

p, q = getPrime(256), getPrime(256)
n = p*q
phi = (p-1)*(q-1)
d = inverse(e, phi)

dp, dq = d % (p-1), d % (q-1)
qinv = inverse(q, p)

sp = pow(m, dp, p)
sq = pow(m, dq, q)

h = (qinv * (sp - sq)) % p
s = sq + h*q              # correct full CRT signature of m

spz = sp ^ 1              # flip a bit in sp
hf  = (qinv * (spz - sq)) % p
spz = sq + hf*q            # faulted CRT signature

c  = pow(m, e, n)
ct = int(xor(bin(c), bin(p), bin(q)), 2)   # the "safer" xor

what's actually wrong here

Two separate fuck-ups, either one breaks it.

1. s is just a signature of the flag

The author rebuilt CRT recombination by hand:

$$h = q_{\text{inv}} \cdot (sp - sq) \bmod p$$ $$s = sq + h \cdot q$$

that's the CRT-RSA signing formula. so $s = m^d \bmod n$. and since e and d are inverses, $s^e \bmod n = m$. you don't factor anything, you don't touch the xor, you just do:

lazy win

m = pow(s, e, n)
long_to_bytes(m)

and that's the flag. shitty mistake, instant win.

2. spz is a Bellcore CRT fault

This is the Boneh–DeMillo–Lipton fault attack on CRT-RSA, and the author basically did the fault for you by flipping a bit in sp before recombining.

spz is correct mod q (sq untouched) but wrong mod p (sp corrupted). s is correct mod both. so:

$$s - spz \equiv 0 \pmod q$$ $$s - spz \not\equiv 0 \pmod p$$ $$\gcd(s - spz, n) = q$$

one gcd and n is dead. from there you've got p, q, phi, d.

3. the xor

ct = int(xor(bin(c), bin(p), bin(q)), 2). pwntools xor is involutive over the same operands, so once you've got p and q you xor them straight back out:

undo

c = int(xor(bin(ct), bin(p), bin(q)), 2)

then decrypt c with d. the xor added nothing, btw, exactly like the description hinted.

solve.py

solve.py

#!/usr/bin/env python3
from Crypto.Util.number import long_to_bytes, GCD, inverse
from pwn import xor

n   = 5216528649875826746373274623709911527907930166016984164533272620692599945581004244622652640509156720922738730017397970929856922904389747851230187154375161
e   = 65537
ct  = 1769561623039780835813534940346776273175000099891521520378974868184264352157081415449182707720490727295376770632579942515719007563037959358184643750159804
s   = 1220125626995232022797746032625764407124557835619605483513423136472322667738087444211114063128377505634674691423794338554652601901132201128484832327199237
spz = 2144640816488997996880355081959057062388718728036090601522167913668193535885347340870230640225749237841665830391473546117213483723045473075426308182703379

print("[*] n is %d bits" % n.bit_length())

# Method 1: s is a valid signature => m = s^e mod n
print("\n[1] lazy path: m = s^e mod n")
print("    flag ->", long_to_bytes(pow(s, e, n)).decode())

# Method 2: intended Bellcore fault. gcd(s - spz, n) leaks q.
print("\n[2] bellcore fault: q = gcd(s - spz, n)")
q = GCD(s - spz, n)
assert 1 < q < n and n % q == 0
p = n // q
print("    p =", p)
print("    q =", q)

d = inverse(e, (p - 1) * (q - 1))
c = int(xor(bin(ct), bin(p), bin(q)), 2)
print("    flag ->", long_to_bytes(pow(c, d, n)).decode())

run

terminal

$ python3 solve.py
[*] n is 511 bits

[1] lazy path: m = s^e mod n
    flag -> dalctf{s3gf4u17_r54_m1x3d_w17h_x0r}

[2] bellcore fault: q = gcd(s - spz, n)
    p = 89502268891356780583739141342659683840814856190204487124714152500750274331249
    q = 58283758774964263077689530394775429444499650453384515190628347257294498606089
    flag -> dalctf{s3gf4u17_r54_m1x3d_w17h_x0r}

both paths spit out the same flag.

Flag

dalctf{s3gf4u17_r54_m1x3d_w17h_x0r}

Discussion

Loading discussion…

Related solves

Keep reading

Complete archive