from Crypto.Util.number import * from secrets import flag
p = 9259018534502783714631247560818133078409930397939705162361230465031580254504264713899169170790687716589100652406132800533397486109926387016562663961524649 a = 0 b = 6235467631650349040636525320446729529985562949423449382969614887116983248527693872546808737512375916974084741892428681798937790855872528526403738040908493 c = 4165903654767429195543540819098180314477702137507994424192636596518008877139978822038616746899053449640020812062736993008962585578921635697413459959685760 d = 1889382340373247565387211782596794283852946561870564309251998196824383297786878212641581641540685106266683503654620956037368416192796434147249748216284648 e = 3015564788819504594313842562882781366361783108618226049128986996153057550014499326419988348165744003693083108924831219996703133056523468396967900376388617
defadd(P1, P2): if P1 isNone: return P2
x1, y1 = P1 x2, y2 = P2
l = (y2 - y1) * pow(x2 - x1, -1, p) % p x3 = (l**2 + a * l - b - x1 - x2) % p y3 = (l * (x1 - x3) - y1 - a * x3 - c) % p return (x3, y3)
defdouble(P): if P isNone: returnNone
x, y = P
denom = (2 * y + a * x + c) % p num = (3 * x**2 + 2 * b * x + d - a * y) % p l = (num * pow(denom, -1, p)) % p x3 = (l**2 + a * l - b - 2 * x) % p y3 = (l * (x - x3) - y - a * x3 - c) % p return (x3, y3)
defmul(k, P): Q = None while k: if k & 1: Q = add(Q, P) P = double(P) k >>= 1 return Q
m = bytes_to_long(flag) G = (1244884551970947614719458919805713649754289814760243366205012699871413235954279930743612403791919112394457579170253990713250052822262255880036254772609156, 4579639528751113977115209571728128585569082149696598770106934145500742785077382446292613925719404433141749168427443122707253164477493499731016883616496009) P = mul(m, G) print(P)
from sage.allimport * from Crypto.Util.number import long_to_bytes
p = 9259018534502783714631247560818133078409930397939705162361230465031580254504264713899169170790687716589100652406132800533397486109926387016562663961524649 b = 6235467631650349040636525320446729529985562949423449382969614887116983248527693872546808737512375916974084741892428681798937790855872528526403738040908493 c = 4165903654767429195543540819098180314477702137507994424192636596518008877139978822038616746899053449640020812062736993008962585578921635697413459959685760 d = 1889382340373247565387211782596794283852946561870564309251998196824383297786878212641581641540685106266683503654620956037368416192796434147249748216284648 e = 3015564788819504594313842562882781366361783108618226049128986996153057550014499326419988348165744003693083108924831219996703133056523468396967900376388617
m = ZZ(tp / tg) print(m) print(long_to_bytes(int(m)))
truck
题目
from hashlib import md5 from secret import flag
_H = lambda m: md5(m).digest()
S = set()
for _ inrange(10): A, B, C = bytes.fromhex(input('A > ')), bytes.fromhex(input('B > ')), bytes.fromhex(input('C > ')) ha, hb, hc = _H(A), _H(B), _H(C) assert ha == hb == hc
D, E, F = bytes.fromhex(input('D > ')), bytes.fromhex(input('E > ')), bytes.fromhex(input('F > ')) hd, he, hf = _H(ha + D), _H(hb + E), _H(hc + F) assert hd == he == hf
defrun_fastcoll(prefix: bytes): with tempfile.TemporaryDirectory() as td: pf = os.path.join(td, "prefix.bin") a = os.path.join(td, "a.bin") b = os.path.join(td, "b.bin") withopen(pf, "wb") as f: f.write(prefix)
subprocess.check_call( [FASTCOLL, "-p", pf, "-o", a, b], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, )
withopen(a, "rb") as f: x = f.read() withopen(b, "rb") as f: y = f.read()
assert x != y assert H(x) == H(y) assert x.startswith(prefix) and y.startswith(prefix) return x, y
defbuild_round(r: int): prefix1 = f"round-{r:02d}|".encode() A, B, C = three_full_msgs(prefix1) ha = H(A) assert H(B) == ha and H(C) == ha
D, E, F = three_suffixes(ha) hd = H(ha + D) assert H(ha + E) == hd and H(ha + F) == hd
G, HH, I = three_suffixes(hd) last = H(hd + G) assert H(hd + HH) == last and H(hd + I) == last
cur = [A, B, C, D, E, F, G, HH, I] assertlen(set(cur)) == 9 return cur
defmain(): used = set() rounds = []
for r inrange(10): whileTrue: vals = build_round(r) ifany(x in used for x in vals): continue used.update(vals) rounds.append(vals) print(f"[+] built round {r}") break
io = remote(HOST, PORT)
for vals in rounds: A, B, C, D, E, F, G, HH, I = vals send_hex(io, b"A > ", A) send_hex(io, b"B > ", B) send_hex(io, b"C > ", C) send_hex(io, b"D > ", D) send_hex(io, b"E > ", E) send_hex(io, b"F > ", F) send_hex(io, b"G > ", G) send_hex(io, b"H > ", HH) send_hex(io, b"I > ", I)
io.interactive()
if __name__ == "__main__": main()
sda
题目
from sage.allimport * from Crypto.Util.number import * from Crypto.Util.Padding import pad from Crypto.Cipher import AES from secret import flag,x1,x2,x3,y,z1,z2,z3,alpha import hashlib
from sympy import Matrix, factorint from Crypto.Util.number import long_to_bytes from Crypto.Util.Padding import unpad from Crypto.Cipher import AES import hashlib
if __name__ == "__main__": challenge() signal.alarm(4) guess_hex = input("secret (hex): ").strip() guess = bytes.fromhex(guess_hex) if guess == secret: print(f"Good! Here is your flag: {FLAG}")
分析
这题看着像 RSA,其实重点不在分解 N,也不在硬开 e 次根,而是在利用几个 RSA 幂值之间的关系把内部状态抠出来。
inv = [0] + [pow(i, -1, N) for i inrange(1, E + 1)] block = [x % N for x in BLOCKS] block_inv = [0] + [pow(block[i], -1, N) for i inrange(1, E + 1)]
power_sums = [0] for m inrange(1, E + 1): s = 0 comb = 1 for j inrange(m + 1): term = comb * pows_c[m - j] % N * pows_y2[j] % N s = (s - term) % N if (m - j) & 1else (s + term) % N if j != m: comb = comb * block[m - j] % N * block_inv[j + 1] % N power_sums.append(s * E % N)
cur = power_sums[m] for i inrange(1, m): cur += coeffs[i - 1] * power_sums[m - i] coeffs.append((-cur * inv[m]) % N) return coeffs
defrecover_secret(N, a, c, y1, y2): ring = Integers(N) coeffs = build_phi_coeffs(c, y2, N)
RX = PolynomialRing(ring, "X") X = RX.gen() fx = X**E - ring(y1) w = ring(pow(a, E, N)) * (X + 1) ** E % fx
gx = RX.one() for coef in coeffs: gx = (gx * w + RX(coef)) % fx gx = monic_gcd(fx, gx) x1 = int(-gx[0]) % N
RY = PolynomialRing(ring, "Y") Y = RY.gen() gd = monic_gcd(Y**E - ring(c), (Y + ring(a) * (ring(x1) + 1)) ** E - ring(y2)) d = int(-gd[0]) % N