#!/usr/bin/python3 from random import SystemRandom from math import gcd from…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
#!/usr/bin/python3
from random import SystemRandom
from math import gcd
from Crypto.Util.number import inverse
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from hashlib import sha3_256
m_prime = 11213
xi1 = 0.31
xi2 = 0.69
w = 10
rand = SystemRandom()
def hamming_weight(a): return a.bit_count()
def get_number(n, h): if not (1 <= h <= n): raise ValueError(f"Cannot set {h} bits in {n}-bit number") low_positions = rand.sample(range(n - 1), h - 1) positions = low_positions + [n - 1] a = 0 for pos in positions: a |= 1 << pos return a
def gen_params(n, w, xi1, xi2, af=1): p = 2**n - 1 bf = int(n * xi1) bg = int(n * xi2 * af) f = get_number(bf, w) g = get_number(bg, w) while gcd(f, g) != 1: g = get_number(bg, w) h = inverse(g, p) * f % p return p, f, g, h
def main(): p, f, g, h = gen_params(m_prime, w, xi1, xi2) secret = (f * g ) % p secret_bytes = secret.to_bytes((secret.bit_length() + 7)//8, byteorder='big') flag = open('flag.txt', 'rb').read() key = sha3_256(secret_bytes).digest() iv = get_random_bytes(16) cipher = AES.new(key, AES.MODE_CBC, iv) ciphertext_raw = iv +cipher.encrypt(pad(flag, 16)) ciphertext_hex = ciphertext_raw.hex() print(f"Ciphertext = {ciphertext_hex}") print(f"p = {p}") print(f"h = {h}") print(f"xi1 = {xi1}") print(f"xi2 = {xi2}") print(f"w = {w}")
if name == "main": main()
Ciphertext = 41b53384d92de5c678a2138a0da552d174d77c420591b29ccb7c7610310bf82bcb58f903a423d7d257e3ee4ae2c4da69 p = 2814112013697373133393152975842584191818662382013600787892419349345515176682276313810715094745633257074198789308535071537342445016418881801789390548709414391857257571565758706478418356747070674633497188053050875416821624325680555826071110691946607460873056965360830571590242774934226866183966309185433462514537484258655982386235046029227507801410907163348439547781093397260096909677091843944555754221115477343760206979650067087884993478012977277878532807432236554020931571802310429923167588432457036104110850960439769038450365514022349625383665751207169661697352732236111926846454751701734527011379148175107820821297628946795631098960767492250494834254073334414121627833939461539212528932010726136689293688815665491671395174710452663709175753603774156855766515313827613727281696692633529666363787286539769941609107777183593336002680124517633451490439598324823836457251219406391432635639225604556042396004307799361927379900586400420763092320813392262492942076312933268033818471555255820639308889948665570202403815856313578949779767046261845327956725767289205262311752014786247813331834015084475386760526612217340579721237414485803725355463022009536301008145867524704604618862039093555206195328240951895107040793284825095462530151872823997171764140663315804309008611942578380931064748991594407476328437785848825423921170614938294029483257162979299388940695877375448948081108345293394327808452729789834135140193912419661799488795210328238112742218700634541149743657287232843426369348804878993471962403393967857676150371600196650252168250117793178488012000505422821362550520509209724459895852366827477851619190503254853115029403132178989005195751194301340277282730390683651120587895060198753121882187788657024007291784186518589977788510306743945896108645258766415692825664174470616153305144852273884549635059255410606458427323864109506687636314447514269094932953219924212594695157655009158521173420923275882063327625408617963032962033572563553604056097832111547535908988433816919747615817161606620557307000377194730013431815560750159027842164901422544571224546936793234970894954668425436412347785376194310030139080568383420772628618722646109707506566928102800033961704343991962002059794565527774913883237756792720065543768640792177441559278272350823092843683534396679150229676101834243787820420087274028617212684576388733605769491224109866592577360666241467280158988605523486345880882227855505706309276349415034547677180618296352866263005509222254318459768194126727603047460344175581029298320171226355234439676816309919127574206334807719021875413891580871529049187829308412133400910419756313021540478436604178446757738998632083586207992234085162634375406771169707323213988284943779122171985953605897902291781768286548287878180415060635460047164104095483777201737468873324068550430695826210304316336385311384093490021332372463463373977427405896673827544203128574874581960335232005637229319592369288171375276702260450911735069504025016667755214932073643654199488477010363909372005757899989580775775126621113057905717449417222016070530243916116705990451304256206318289297738303095152430549772239514964821601838628861446301936017710546777503109263030994747397618576207373447725441427135362428360863669327157635983045447971816718801639869547525146305655571843717916875669140320724978568586718527586602439602335283513944980064327030278104224144971883680541689784796267391476087696392191 h = 1420555256339029007623997813064646001269162517128321148445315195505239735275630861823661566974806499472047280215484592996005803648513302169629626127099758282515738821101977445273485022910246569722391022977450955342222836145985252124058212342529128780170990021228730988558665064173954220322773988555167710669068750665776903981634200337373777404012466927646596680586333670581651645526694895600877689342038116459849183193823872501035663586605107067192354044210531807251755452156351983674662886645745394856941265207731156473167231778757731819787611903442134906892597442296936233823840108134806009542341564017395586357285132443867104900170964829691269535011088959513758953200725927512241315102588162307625667497293774446856607793870742116890747893541277522373302165118962976053575406705355764971195021874784514615007411950628751457901414286417358960010967221053822454908696424925405704175995633020493142678213202614937742894400381343076316089897622795515556015286002072322759700438579099970591676839009309031769399502594275266218377682472239872586976705452556133518395328415914503518652542017532651647731241407171312901187911076641932472943264583606924316675349565466488903831076073348850535782518384829652304040155890590587188783695482711889391210316569992875826864203896074373913044155630807488027391070097591354568591831261212998547450723243648908349081702648981754965087366716012704456844050856945098481648381066456654298504766274287677173531407712216638604928122194203916328841926799970191645315242073698356237463109990735562385573707846536974481579821301372474435457099406760484280999724263427442692583436069170036373949813257024671755403669821456270665060921956691382969799591246457852441573272563366612307625286201260042625086965961053006988659415151285688613563697564208949796608132657497688137512977726996868089866737746050625960033949688003905344289968553237468369562275970721124808922797498954729192402174080310105048553480796371124861551154608423542660872024811406457451424253705687979915395138199662324871095873255085721494088182389344068642956910343125988440788281536821574417589504214561018112652377091738873567384795002650440795826732903483284697533314215503203322729252515102929675782158033940939707173384735831945973131378767145549237414530035857282428664740004024186722896592693839808003379490048051781800528316131147063192114353380299163535474170148552078839155797722939143164848128170591789817861428901096912042379655572487529983245927123870716371357517142431645561532273325783362132723664729122853387243023889022825534772304668999948890306453633124290070865560117725343418936602004343258378292218254184989796563841886060342528155126255491479519793234521554762270234424568183556174229507271089194135988143493032829906811846783521409480751862383365285419925324896562580231684692411694312251240562954259361596977465804532938260753882101880890334741978448410119591665004422790211098229717537610959221523324756588024738544068846236205437760843840319798491939909330547143199854608585823646613660809454152858803614876632067827324289956927912056108902075641611668181460557770913959037715741018607941206784764550084749008826004455090269295539665469266276760215529247213893160911919455625283080509926624966775395334197154212462026901783136821516237970556846369147663455890608535960863730071819706481755582989771193307683239283077479511187437689338027648438450206074052 xi1 = 0.31 xi2 = 0.69 w = 10
solve this crytpo ctf challenge and give me the flag, flag format L3AK{}
Let's analyze the challenge step-by-step and see how to solve it.
You are given:
This looks like a variant of a hidden number problem or a sparse polynomial problem over a Mersenne prime ( p = 2^{m_prime} - 1 ).
Recover the secret ( f \cdot g \mod p ) to derive the AES key and decrypt the ciphertext.
We have:
[ h = f \cdot g^{-1} \mod p ]
Rearranged:
[ f = h \cdot g \mod p ]
Both ( f ) and ( g ) are sparse with exactly 10 bits set.
Since ( f = h \cdot g \mod p ), and both ( f ) and ( g ) are sparse, this is a sparse polynomial multiplication problem modulo ( p ).
Multiplication modulo ( p ) corresponds to multiplication of polynomials modulo ( x^{m_prime} - 1 ).
This is a known hard problem but with ( w=10 ) it might be feasible with some optimizations or heuristics.
from sympy import mod_inverse
# Given parameters
p = 2814112013697... # truncated for brevity
h = 1420555256339... # truncated for brevity
w = 10
bf = int(11213 * 0.31)
bg = int(11213 * 0.69)
# Convert h to polynomial representation (list of bit positions)
def poly_positions(x):
return [i for i in range(x.bit_length()) if (x >> i) & 1]
h_pos = poly_positions(h)
# Sparse polynomial multiplication mod (x^m_prime - 1)
def sparse_poly_mul(a_pos, b_pos, m):
result = {}
for i in a_pos:
for j in b_pos:
pos = (i + j) % m
result[pos] = result.get(pos, 0) ^ 1
return [k for k, v in result.items() if v == 1]
# Check if polynomial is w-sparse
def is_w_sparse(poly, w):
return len(poly) == w
# Try to find g_pos such that h * g is w-sparse
# This is a hard search problem, but you can try heuristics or partial brute force
# Example: try random 10-sparse polynomials for g_pos
import random
m = 11213
for _ in range(10**6):
g_pos = random.sample(range(bg), w)
fg_pos = sparse_poly_mul(h_pos, g_pos, m)
if is_w_sparse(fg_pos, w):
print("Found candidate g:", g_pos)
# Compute f from fg_pos
# Compute secret = f * g mod p
# Derive key and decrypt ciphertext
break
L3AK{...}.If you want me to help you write a full solver or decrypt the ciphertext given the recovered secret, please provide the ciphertext in raw bytes or hex, and I can help with the AES decryption.
If you want me to attempt the full recovery offline or with more computational resources, let me know!