⬅ HOME
N1CTF 2023 - e2D1p

N1CTF 2023 - e2D1p

Based on N1CTF 2022 "ezdlp". Easy dlp too, try again : ) .

from Crypto.Util.number import *
import os
FLAG = os.environ.get('FLAG', b'n1ctf{XXXXFAKE_FLAGXXXX}')
assert FLAG[:6] == b'n1ctf{' and FLAG[-1:] == b'}'
FLAG = FLAG[6:-1]


def keygen(nbits):
    while True:
        q = getPrime(nbits)
        if isPrime(2*q+1):
            if pow(0x10001, q, 2*q+1) == 1:
                return 2*q+1

def encrypt(key, message, mask):
    return pow(0x10001, message^mask, key)


p = keygen(512)
flag = bytes_to_long(FLAG)
messages = [getRandomNBitInteger(flag.bit_length()) for i in range(200)]
enc = [encrypt(p, message, flag) for message in messages]

print(f'message = {messages}')
print(f'enc = {enc}')
message = [693664659977076429441721581800631821031318210374, 641151780938584517641795955698442398343919851663, 442260876510675762713915571945455628516320686117, 467847046669767750439402201866521436762499792753, 576299596039034185945586660464629214766725406313, 566547807312007176018551275285152923461145482278, 549633812326436187103149800041310071046834922223, 508462643386772575091520684203423214004845153777, 691074111704362749554767168266907444196803512431, 413121753215882872520886371919276517674126791912, 671145625949654123907421975392897238339761791254, 546304114747282685685218544920351984032270537488, 572780653997969762702124793964613415339346752213, 601373795885303211716613191912983946252569198078, 527457235225748871937722141407754625294051177749, 380622950183993088387599684834391000840637381947, 583727318782381432149579486946306795580366829825, 550837922303467803018782253055269311324458561180, 385101366933395181675168212725823770156029740822, 641662384614179293679952446950337274222427848632, 459311266266455563068657153682168201121877184739, 405550971665403620381984645469291222673485137149, 371597133369010596084476169385480058426468392326, 698678234769682576843834712549894054633369207255, 534796953640957148331151803381195637842781992445, 374936326772655449966416360745332041868502670742, 492442184422404030075920988632473787949518169214, 705923038408943019023751711766519191467850943084, 650418249017530986700502965003590412936753741846, 708204894591959628376934066632376225372082446153, 454790589406461321336243672198169513923574702304, 627499866789984935690247339547179386637859636919, 423678557987416507826536874284013955560714100793, 401362357036860382844815149593397237631885352243, 686269123693910379749535722753583335597789391578, 488241406301490152491456279144104185166974642895, 666222682488279761488926584268313831995479860799, 692145649792961796987317716770881441381399293905, 511897367235087351921397820910917375986191141349, 654419167502388442568348117907648494584449757209, 589680762232106721032843012116071294451415244568, 590590207988570475395658679957479087724562211841, 482668740919135498248431363803277605451127673913, 715758814258825393741600549482721735218537228645, 653743257230844443635435315901957203427244538602, 454126548145155528162935266605065682187337810299, 620125593471327015013336402531350089722186042701, 428403095612631556748653400063552923087980336909, 569833218747535301058312386702286604678561696137, 660641067559405073494223266631949683860476028737, 449333369658841632304639977694459512245792175293, 467447064234054616809460782875174319877189366516, 393392269675253413458565105507251914214556082922, 635439223607461693963028922726597992660256892935, 407257940459092553785526240326103877335073498631, 381231448748213440272407453073595931506045861957, 448382519038394202568993190054538810660260113592, 716237629706513272445605077735843513110855433522, 433219862219763639756688352959066818152426999972, 488764960789069735381119865733481770059949823842, 398986758108332939403968410236244001743123162261, 514387803898406220776059914714034818208278780873, 590245583566834300419167731150776966766200770705, 590724033036348394343155963128980442493895457996, 699490998943971192667984537274675479813696863626, 718925420408011036460460010425688434382693330922, 564390207482563741249126161293788463115528495908, 383354015121386408912694746374501038216201495056, 453849425687686366671300434509173848985593265416, 622995416706287005921220058167770841333338488067, 729213479452648274706136621948325451122763373528, 686219967297487467790233668226515789529640521917, 609974347033307852444992427283454955701842212277, 370158844254585266588115081907949361562932018924, 378464020678799135304125444772764971463345133327, 605565552445756697479889302627729048203881636722, 519143635838540994109753692182095612109343297845, 688876612469722395107083285555790977268569381013, 604227078464364754329111819031061370420097739909, 534671596093515272642286709098549719295929965019, 664831166196721835534478130764218724610835587432, 370452484041643991366059890003096580693243995922, 635650390595052286897798989359093265932472510317, 474997911571957583578721446486724673814800511337, 666833277280375729438844251406729792923367638939, 549677870902843766274788153586286518685330579562, 403750218768865885432712671206348197497530306372, 648379532443486089700518395089379649272741585525, 689541532202569673029629405206130534769384927362, 722445024372163982105004106438816891709928448096, 651521702599906693092861908583686837945611996078, 557356404670196377948939713130768356044451243592, 510824624387430888406068855655757852565160753932, 658231530504564057468259235412951310486077867029, 393083779876125914188752133433568000895654711785, 435999179120454174265045299731759216901649861140, 644876922291307264679792353211737278436054622754, 654605478964642677023183414228201689324600422799, 397649662675149197445274635932527578292613189899, 702088771582732231620812257232406557644896851508, 545261333943706593808958565590571598643485929648, 489300803173612208186235448929616939354730274661, 395511266610538604983103943278950553412390523869, 711947679621473737738681582835019422064505766533, 655108057209376186592395422788298419234194200055, 400705920821964978204336301018727145817030829641, 557847732069294650719566682927477464549118697011, 445694155266978761199593911327898648797225408780, 509327506013931501965346606465657512886985426818, 411850960097922418945293054971759769984931215613, 553203005031008690580165150300994966169367896789, 530652527364856475981794090271660534447325397356, 525552708426502254882436208110384887625868992533, 641879452010210304086700323582439256278227536757, 719333107198079092685962949838882921753050646649, 386627758848915223040683304270060201736287203139, 660572447042280200496062468158721459189007179803, 706166201320394154541860241557721395270085123453, 579633541498196490661544008242060825478642451000, 643458562688391496707824299186183154011530826172, 562142686022206934731356187745324382416591655497, 501400657520170516459592165171248744753209682136, 623179140778467841916936315748838474803462205575, 504787712625467688129967338367063834713939025797, 482952924860910284331849841113579512061673276859, 589612419005702787664614495809087275765593515200, 708173051157959302912662792155066683391599037088, 499394787474995103922444359715199061114279267634, 585799668229640216254905145106137698139903275746, 554956455635671309874584189793597316684332723414, 704495380359953118982056288192634660302747228975, 636379585192267210170978906373833897679237710149, 539590219032822751351344838148437910734578598790, 674873985654794234341421832706660211498415988742, 432362243233185725959861560932406943572491068540, 536473431426878157062323693838540671362576717277, 720098895832205294868444431769296835443455760636, 579073281776280740906199562458924597541656581619, 591032880071346881498514455634288291076704788575, 654789159747620000763010802203406259372788031654, 695800436297544355627391558054808814174980859981, 605273358282103759599881268207380518657105655632, 433718513437131134678461889459557119114484188332, 384513276557472375558555525856176556368870671402, 600343354385880278644181099801877472312572076963, 695496659236976726427423916216959888943631989241, 438503333436853133988922986561344630255994011897, 425583123306963978710964324292310291477836005305, 476903564467286403602532752184528609004675223004, 459157576050880954066773204066244310642453599360, 376401850156074190331534780592274518042125807945, 542946159546607081254194786204673920263273355503, 456484229337560874335567189110133777645095163385, 626352224071160336097429749136171060147576844515, 665767035251663764776607094101046606586127905011, 517362006352789655575954568218486403517354554358, 437386355363895799735332485388083032234695880055, 514973814138948886475769447233812559312528612766, 374637417457608036809312199403225256699715449299, 677229378532248986407775002200926658772641937438, 612597502270790552054141926184983093742294240069, 431202153828443848949034172654336515755953360105, 622258420899328481789159816535244650713851409293, 618164531021337774672583713954837010690689891975, 513006116907472830120587422699103491498026309133, 555490800950103459194334700707609775850979114865, 704107402534458090132604172739864873726067936161, 690124474957543394281783383866874758144804934007, 472003742277692896073172902648253923981970876514, 635778201676533793375618948558377860181120127094, 611397339934029358865719127411045379740178725128, 576853757049166344546205037719216850371659181706, 408800247021189853377167765805326049327289045993, 729972203090847385745874125707239019582657799980, 525070335256752275837973097539994838060573958877, 411651947363914921986894114762917126749374581231, 519881958261166355010001443948648454218821743842, 573673363481273132247997212479938027609914135108, 493337841572344267307690944171025803909991475790, 617022485264784179620793403861546701423406249938, 525276740282898665434355035125257355830169089057, 680364968656971377813138970950094887958834675303, 504113398809310055995463656972855134024508066403, 465230311414519992240700301605280432857184968020, 720105585854687931310125801670114637584488449678, 668566443891585965518771441393436345329726160143, 580638719984977907256606511678781988397408113575, 697116677830535781701823672224529182422159485954, 640500255323132969623638079044946746254179278138, 658010946200053561403704734852272215619688495637, 718587383921960186228563517703837540604785215716, 433682677761338254004494392849033049058996864418, 475977381288578995417794556437438171639072049493, 412602197708386261331557922762709486995912847552, 682569317224227964165295123361310108054530143437, 425901573874339622630320602490984143376337673005, 722861770527687134345857669333452992788818127442, 682616408191071307473294670590693815960428150376, 696328074485185099123217149377310905174615036228, 455731197482160380784380578906107173308643405028]
enc = [12802431364130645112998386130428005769825107729037697939666001936018624382634102608929729844155359874429890877171241333037279356074151441218381792849257989, 15841931479507910988248305805319960918896666038593569669890009249261359553758127922930494463797629341571426279716572950430532882454202020690026575100177987, 1221898424695532993342738372629546326869084739337065847979484862871978525211953284669644380344082740417474903844644139598107160119553596635470934138036958, 7485441539351227382830427169292005068374766431980627537104803552768432200018282712200763340103759680330985996358783490150871002540761468210506816175401778, 16490427834560779793720496520742149393153032499433215465759099190321694661330124963046121764909345746761779082834462165062018294625890490453836371924613349, 8031625224539527792473708158284785450887152304616884645587504755453928923882770330478061935031851311110848687638036202616335897665362259462603738040684685, 21756436953082356215056741790811013055528330688107471832901006551691682144488287700074435883882759471935216610848090962450531710784608415077031354467481505, 16694267994493152858810276818794756596905889041056847640833399667145180705382746495819004966769911134724566851591603916593611379778119582337187183523369719, 17261909191553914970864431404011635960299019745416311091885553985601039670349345236976867786250811924029125718357264223633200369953175226035173539996554177, 15147461734657135244308111183088252763050417076381443297013089869439583946836743564527129545847648293974780065041378941819082971187483900208985626071887723, 17037081810085070818713345968014617123788910212134025649820174018138425756879891322480370833851145576915999085329155670879431668842783161461771833127520551, 18394793488209799974692802626463695468214671742172206745555436605308904510867809441758699152472391396856293860451374390965229770407357496371519550823208165, 3873238795226279190871777484033249322417236520806723411587583375967314077366815823280638353734552822341146930305057073777955318019184349391849377137564083, 4120648555903242796182853816950546229318366442366051570952003666874980963983152868274735544580542053549951286757083509993285559829361259065334961526796054, 19499727546856392106032124857778338216108062102479779485494249157365912695532105834514239304166776475129721621905859085104767085115683985674244706527050959, 14446880999602357984725861828648389471997566654666984259173920255505914785710209681691782226471221321629899315949050874936976350968237444699706608768887375, 18822381743745716420992275055759822079007667188210040556421450325623865572413713065933957625809266131682092455143571538548880294440467397273141132102598234, 2078993667892636464889910489744040237030609908973999120480344928269019879035285877464379035875800543345822320420985581634014463538438224837261763861586677, 15275755178335032885417760216978828856709120238851548322123314773428069056299549395836869484947879782196020696529458333227629420100865680232982016719046091, 17893925081980092265297049830976436089002895796412083353410656875394835907232213625928698145323254845306125237252332149738887731521773631920155902038840860, 10138286435644961911566653106217650561417998466723794129832123510577708817875722230498244219533540776776478784395392571967301144701954892087255342537154300, 23781898287838801909708558120564370028836358940960071960363431359965656683655555820742071080617655585885544001833186806047872602158806389618424590912169885, 14618829488237452071771111368599368239253058711463829694295880720667209162354083218730170563000389265754138038718099139060343875226210060541819770671248124, 10712086881982643953429542224834368987351550849327471346794965965224403452624660810825982342304599584482720958091163971530609537903235371699644617285750762, 21356875515893845401176571478662130073136777267077873593314796593580271733075908762896359463536562357281340090179236626447788553152256145393820118871774730, 3830637517539282448885291272661611495422186049253711731984076851102467298051214080106742001679228702909263732721276994310474957428746129767219592551567181, 10333558401870114095537756884943206076891212848493125699491519015251928231789362919640661587973190735520950701755905671000926396917379477660013162458218943, 23736432549415238131963606477938519282268147218094644471727959695815042704074586776170303423567364026452302155789029476244161284822561225614020906663667619, 4644035995563829644939494587724565628305992281984495580301269917240849605484280970351133520632655566843288588417952863093674362092942131667871124985212949, 9642416297776960541135321962175444366098216979471685942298188175551752825175491457074346957995345004017102896503988201998943745062204770110761727494371574, 8335281213946275046746666813951128380084042101516669166231507116865049894281067976273808411588391391707025179256087366105062726452523556116606195336452882, 6530482653620093114082077034469049556274883561754077279775967229401023691502197712280297610361000195954087770586736553875743993339035147162150152353163663, 7150257780223505586060206132524943680043121512964630575634713073163634197101527587710840221581931193246376858435843192145530408854242421895304370936143902, 1254304665313866561226799266014735121771402903769064851833306395653682476616235958281360569340510646325819562076484318689539404100396647203891379810634552, 11601359684712978218675279426020451533292101801458488280053551808996493318302131299838753355871218633429176093749706552699172145683740134299400277843087952, 2700268593567907930265525751728407104875489061254260830711986575931937613921970948483079251747123468537195847939302891888911014935339737073186860899524234, 4080942317301967246869353485589617962551375685385196685050993136896262140906796490082447944250346337882888740872592717913339560790911023823533568928551720, 10234615262823560674392728188914569214149481624446413098803831353379508760432524271220726391202917405644902199462785274073427297698683339311211359142656818, 1125945224176025006936418171699674663992407710510497313495825994942895059437070310257916189827215007718021447945978872379864023384952129696506282120224233, 19914951263263531260306740780816159802017584854654281115511082220749163406629807575948156272065625770694237040881326533300003713510971032393055223554165327, 7686726590737660417714025911090741352758643280869745623382180579882049157234662436338789973680009711192460669573563363766062491130800150339511278803498162, 2812310951817903373614255128398787904857476465006239956594366687664880218698741393086998222807296075696952927611888322818057008597991854866726461128969468, 20887882693699664364144618888765816850883562077448268683230920432311999372756485724085587262647860072442748746222954721130293038123612804878687752631794955, 8690569862192462441987449277068927883527910593838503637825344354427820529433735737673215111082490181126283902726715562302819442682675682691713550146809217, 2413131813103467343020477456052518926268983422512279452901211002270260929616020781942952836584010050874483495134937518017096754313775874974673309589212876, 17653193398815101469433909955381528195689636963827968889973646235658572329644007341130442643570429711525946608851822368357115422210481444362996301489761638, 6126402475217327038147046836526705820129689848988279077018693131470204399611580729268939693042421835117198568308235394530366160585670735969006141874087909, 1361128666525231933315783415492733304459600468449681095296110523330036541473721059694239647835933188805769164168350436537797909619517809347947578299834983, 15059407696531860261952862222724919338641403367712593611042260353451344687572443853334492364170670497671440836801221481423750482195869015424093446365401226, 10976713473563499337776465336779692130223005233236840678969170258376260575574769161313777913504720809132003763107716283743214929193579365425796564276946842, 11081947374550148771282567948895442662527812634988105558098435841637076648810409871700246514369718493393011927927896713735208402422091488333353341830845464, 6937198964762673854714880614936119073380436631653258537726983808732793164460020088337426980235314018386009206843995652007995993886949759445935984520426539, 23389436516453522925794907098599734607803553657584285436830698552652531819039549799140706776065354424342390054062290701956377826946910420682660811844476129, 17681559952037923756588828627481395634387669809040100468770525411545819720405926885099001853903627519432633395220855310191015387668249237043108683624945936, 19614092552109487507881840761377121780556234988890288606954923573701039036649780798533055485769039166458390594626929311506826522581195035097443799958981462, 1969965423585002617581019079843930914841409141501759384153688329005895508778123109121569656204271335979256631800250883492440478893638235640486379641341912, 2320923515332921135233329152869590220217977189131802760938423056312256802414245040014586118704088521816385659167639539616620317219266139758755680389006096, 17588004628122309296502610689295481708628562809392740841475764115512024952166181872316436923110831668361652058782925551182843530678115797978222760736666238, 12276911584609786052605903287623189473659209191837581228725403180663097102369721625414426709207293565309436614221981004214660151597244816606366512436603888, 11842566505821585287077172058891087801957095353557583275131878603953034079741151606263803208531091675339528593876833418868838816231200398213922261755166290, 1529596015817286204813206622226632913633158454747501387562331999390003712898118774683119943186889618312684844589218376383467709327323537925085589998117988, 10407127648219250304234420960923103591893079931323394353041341104068061256527815372998465666344176169496459397660199997700931290482945461727586654384188475, 12171839625174918652312185222121279277195150383989436291554667164031685168931563853344009947267982107162026619478996825264436432030561510880790809782870351, 9764846242816439267073338733640602642651188560762231729157851945292183051726838055701392030891393483535364065616457809689410888561279266106291619680997067, 24658652616381776966922721580692944866579555389558010553451463891512715368629193251598921893266732381995084521634182137772594546931592451091510312965529989, 18034671156460777354633486748746132390108720362883751341057710983742216633273553626788057023427030477202576860607085773904695785543922033356257561565761190, 23514883843662384605893754228693669366276629260863879585064019209013254994198481202557815240083982686887723599123715229423738535849049285941081931771684814, 1747520357080615389460274312326696265928969442628712562895485798186997358245763882095801440755637276066287112856047762018183151862865466044100655995926117, 17036192714815443263146107422613378466942940398030917971809438253761056066228199268473758362808733907603866351804210736806923875908009267954251577567876891, 8817273795729670470709896558862743020850509015701684718109791967677483616209913512463947182376413295776696406422262674467676066410818249213233389876461237, 21740211273844298343085907957015919209873637486354367208984207893341477067421726282587915968531749527156839276940169986152223301104340781071943695244086371, 11844651794657896961806050065760722017412193238300504289774096849572596825834213680754275335384541420223939946615132838613007551214018906789525441147774669, 8129749889785159012980609231084214491054172675665615832171131797248695468856517932072550203894799333191680714979859939331296398887109145705970250901691896, 11695284185972004767472101667638230329606628357864644991939913811582990258656052774672752760244856918169125862249098558929134220119600821960804176908283611, 16314933840668305067461376335742247877580287785696269486180016909389236067647558779841089316267290705566054884817418790096108734282582335310368205182363957, 18606516085817577620926634775282398789301479338855515729104575654484819850511053504766428883184656358892632923083331579007785683669057343704956281433875606, 3226150489196877576351264295039169829417188655970560589526256179314299314145087894973100049887558692376014757485426095296358822466156239623460874472154966, 19534989012686390559891716527215262191308727009738449630371047418888859350414586095948946196286861685205849121365741388675867221686675375417838854083657052, 24208375116930230353845019032990098217492613704921535889510987019902385179521710254544675658322149432204071218047418025005499788467251625135521738977377160, 1763062804940690513237191855264315623380667406594406642817481156255487289320165404463687386134054563370482755737128663506704736832153569316847754069405584, 13477110168666248725923505351231751857689496110710619206192460756250248270839808210569058599946511767951523997839847060309035478535589716215322228543904251, 5624620622885539600338670460553458005264154040857656868841669752657863883303657466453665936750842733219239568523671917836966979774581561292319305772461192, 19064441976314148901764282789344170798883594556597975920757024834294159793812419109503148362960949146618490344245176605188340780604280934344521436102814749, 22070871235776504435913068366559174507765849576931611448352363340153471058785679647442872769642429292617699970917473531474618167614029594959057685575495379, 23370054776122376779925074705492246721746545483631232351290602993951341910464937566067282775445803424574510374029656646144623209538913563958329684792553142, 14244662604510763885444101260115085023561049241222635764349530977744660515106866164932118064850021891115535386537093113037624420423858928676744563446211415, 5841733857714297347219990884181013382713368982889830599095635752590860646998491504554598394017790519925310705594593191603791211162861358312358526132943252, 9454574014996505451231441149671419862568230574743571287883480388931232389984080516166289920548971970608649472577881022089115994467795263288420060626161978, 12025550601795597290545078739303305876747558748984721592357751821443390583303181175562052016631122081436858995571970324794600999776957629693395175748856429, 3309704373687045096987897327419000026776603992083428068492847000969082995309863192943886104513564868487085916127011224987932860891429100829662502681931329, 4126559406681147927859776019048977946029184376356634959604923270848265559783017634131367404003858616331072576859854123506386543046642426290990700837549742, 6590240198074082039879588369134793297702025696892876376250529508121208790634960410462619718095172857033286526044615435192886345833234336697820942136514354, 10838195866701247623418285573964343559784514114170440567275051025443328217966578453459462162524285127459554292533128256876474230951282912394977241207332359, 1657433158026975711712737761749081514673496426523940089579710565508243620737248951061950302655495961584442216993881832603658465573648757337008028738960018, 2739064754353892787493554343216893846377165157672312029043253005117404976439042722855706057398905854228429606569964362640662381138755237643589473131551418, 21172215884938268043411370585184527518014458716794671161126867851250094829334241781195535750323787216910901085180593906605438912332975622019565766193125878, 16159716548897693375554748797256891319931268523891519254489722072480590140253118173522150323767310657959800584719691319940024179874929995865674021993091540, 20250520134850640112115308560262332612070366086610795246395238530938233342059051193158083184052196493610497896152779577023739774579330327865497664687874010, 24812962305556048839067391806837149348700619263727304765352910849500886514806114753649847608472424693741123244517125547165365925347647206011022269420461251, 7393081665086988681362638024072789041848367519937012907912550836672067108109063408149251420765103964012038491139817753573053447579333266044813279469144944, 8993969975239015578387410833135455294237497078869801288389929971408445752945649904224911721056675439481701605126696094427870322455350177646318049858772216, 24108681250269405150153741128745963533778600907659887826089346204751510147001111720476973529351720161364370633914934038834648739963431983106507221608105071, 17230554578959854421955355069881828896735934620878042494142200550697417194318742283031647309890174129928211994892878809445435227626495020516322965419812680, 14185443515874763106601396273883329813077277068745133129803834767576095065155245404985391354848493575886016733471182945429867346944403492319423488175487464, 9051684348164242937984157010446996237177025178044858752789108951563454432114309081490680880143188933202537701647187989270080696291695027202031114965504465, 15968644604452324093316323003253501274138419445724525454279030439452704415557621688500911691411401621629566881021571525248375387214578029947839833403006684, 16209131277806518314040740815655681115008312473463041932471505874478456556806055124972010544702515371917055994711254920209521821739238442778606242595689136, 14267451374230545872133460219037824530628846855683835187862297779661446038199929435385555110855969683622859538335789338954156580111583149459446787930846201, 23790336084251908928403238066046481908602096415990504166293872338092719230524342734539924012164569225788355119046346387321121130686830027124362025783463477, 21345234479026082149298106192127440977918090426774140705788242552669067157980544072266728883118765227703221162553462368133460470045349179753160971039094200, 9305127848529933694787026320831737395075573847763715753225101066114433780988250329990887614584957373494686049724019878162879332112961806593149488344546215, 13842046255087557926406164968912459332329325359004163688284202096665051971905148063300580673777285901342794578086480674762923634751266380032184312519548373, 22927421582905354003438719232393269012752635264592813004697824352110193126888760005373391702574759851903178363003394606055637835560272815362392783666511931, 23666494627444662087603328016048247849971327006880350229129531990464095777421407489395875036160933091855635536829926502298481793502040901088565481619066614, 6931511897435916229199231394935413627759479266461117934549644719780030790378740958467642473455461452829619376274452980198157297854824972520346709708100360, 24555249832727281025314532419181706720577546097037365645201904083198639727267669096091035808959490255144065150418326186063938996057663502541714586097361344, 1664097221650307863216140243177799990793785791808088230836822966491481115306184052602669419603060064720775759267177846297541075547557655346584835791978623, 10385701732358699575525681464177756584452751570741415381005081100372844616353810632260049905532228008819382207426817347174214630601238914041050606268384535, 19811109989872751363812468058009318171037607926334987005725092234755635747473836116182682798385978991335210859739079762916443624642218520685915728721427279, 24686173673215959951693708105644815416729869793502070002725937288833838058791784277609006544654203724891001415323092506652863087293477003935804296212537757, 4464571027847916541453093798431797623276257071722540821886225614102719840795331474986494793553517596773693152755645622387218885351980348025724452528043481, 20529060187083072085144382682688430291536724394170108235471190686666162701889071059664969210959080015862866309115158763820927328840900703747200681108940698, 10196352115221716967545879161215742024314632108421777683533640781128869500968439480722768182459652703944307390531824891898249359470211528086701729051536035, 17075979517113408674799265351449042018326593738341805865814246800054039350741169487898589541954225492144559694371395909019451872532953716956348203460188664, 8729312652402643991680156396577031631642045055801337031264815922017287160087927618794335512517845630003033385738334601568490445068052291995372269029264312, 18815416831192714208010452521277703614783383182936408564329378951294172995260437272675494793572596924301362800575616466225382089201089386708138005923488820, 9601057668526877155386403235142590275448939487805305657688602383259372748680150489385791799864009699200686312964549630814608767441426930176574637164172338, 24869598836011530151141929593666158251123597039772319201075561172631966715408923659188348433472168344815037722956532532354889485498638323613921381448893216, 5909951884305530036902098907697774580818474819173299165535478446009286447878979168139115500554123770810141869458826760573389985915957048274079373810978418, 11371251917603094933160774161669447215817045003071558412329882897191943409033448913400498524347992888232351709628705176689108831194690704074092176780938897, 551785620532509852643950129716155581783681586172243143964645401360176082986662742714915946522713862053951803679993581672949497548830540173323515114257474, 5451096750683883126256346729981696741947096539573638544818573800787385002596401510067958743559691552970642317993613653281724363562305142935841454998639523, 17123920028121678335638297328605772708344520673581531559964483325267837828507553881521259866583948335654168242931809985011250636123496953227284761874011265, 7874523196293154389935682128883780886675576168409698448649545633352544309389693386666016314218665927868179109693388811490206823864527216673266238964970897, 23649065270512587392574754158723920638331966781166536410420979439668405474440672814799264262228547112176020068381847686523573884407941899054814599233261164, 24190031687673495347838406683475206994924260431258986610598537416225564244807449968419903989962585058789476275433445564290248486242387816608688871236973528, 13605998663626606954484321600177761063010656143576713116692506645508407493690591666021676729568099510170655315280476509103012875735370890671513263255764671, 16571553211334132895793416809789965262122400989077697003250533948433467354398963089553253989416870000543019464536933138723858366219874623115814641108662188, 9826640183995099837472540428686687456215964233823201273519452572229151835593129774903437988612794076982559008486399715520871375380950033218257088198898698, 1905044234624066879727261938968902529486358885313965708003760389017540141309830338474826711391994992129372747501284279021906217162777292479084599062665129, 12188221789553949580075515688892990014269270562072575922461826680352220391856431272216820004268121858028515271829940614853812453749528956021976390985897806, 15563042329589083597904437386924162667321087802050823417598451968963544078806857451868426729273097548188899555576765472424214756797430989962757570674652868, 18433220039257868742885516207269963381685282645339909641430675485503508409367458940114127842165557398472175510511780868970179022389043487101407516560125569, 11411951786693646929690853907051923593889441404105225945834051543686056581198629329107009399445292958496416683548700022213101789529633991030079960038393328, 14275382278224163040764578924093505439842946766825794705667598992919824407980132381313336434900435234445385604968838234542220362352992877383900260614711743, 7791671412509615737028145209394955250240813590804569452204678722198419102394519093932438053812603849378646597771436948597899334381694514286418697582174740, 20834835144817880335846051447242933162241244015011411408218182181432485847095586500030044137552653695021960255872962521476918031022541730595717283784814440, 12592718228501991540699125036468769482682045871135859182376003500227431914697215594570110712821451100229243753846161014661352515039022678333094542691767794, 10112279527032757598472304083330486355272588021164181341150084704436682697582885829400651624410189975320458125395030399398673944658005038362023720256313928, 11983333061418944073002263207483534917298266918037799877092059478422506361914469374167120898889704600289970251808546219908097639560463769258858597173003195, 6519052705523889056711008173953376394552834531935243639326704687591225824107737890744789511960488882361662489129870655486012337556484158376173821625259059, 6288506640560941187182199365728604207278162286235331192853243553079645517191479787255211647533798877007340661544336283995458016845677445960674283371538691, 2097235742927475763627967722850271966822996875892027817874355158239487074748064354705183609740739015880829927504734515955660612173848028137969009137592993, 5492805342348725314539540923214083275242889867612180056704674658241506674463534006264099314906314376486584542769565356248371098081249637137999437387733952, 2934371084707806914315288513277551720782408329370992811034278434108842183178308951648145735318885239509164810159720671982457834638980022674518223202531733, 18684236807129269752691763175972328955211901906567168757253528407713740272852303230251804671981319562425425626825027402176716755310802147833851761790234098, 18415510968188711739818418553724104490486241862302602443849687456493393699626783821495277208837760850509971475323452482867545140964882388053127450574945257, 527057507665412612336596514476043926607938316361814105716509429492918282813356254253160391969345587300398848904582993263418901845588888132460683363647394, 15464618088748223814914277596617125869445005768053281959446532375946253314072378429969360041201161695095335956898429317410440939875691642424385160360332621, 18535673136368940401954784411906084529183200894531677783320114210572951990126374739283597717106537554653749550412465766451656614218055324146165801577997898, 6602347861694341844323078139998165600822103822625553017901700556438426702979892021085138980817031115685010436897705066920817629843161087619325651884841494, 5038184728162810477213923890175581461109894659383219897474939673434396056304913844113351001788872892451611791218230927404950110919483296245215266912036544, 20116466928008546363140122896149075356532805758708154331686449172524730342060145139021190826994917170580612621680205821594497089591778732462627899358510819, 19856067344203744574737420047476046835252886924818574074582426054632538663205990713206123100154834043256321647589186488996253965338599282387348949952452235, 6489169968555497290422187291255665128590625519286630274296777977328572574596942204552469782026829571327504142778545984614412224378360983412702040284873290, 4553780456863427610915884316891768089809046222284839826371646208684075168725343922380654798366743468571290353645511824206694239764312033164870976363593226, 23959066018152767645119851439958735671040513763474387516821433219080273476231641757896723067763708675113323859657587116141364114219119850930618650125129009, 11802654191643700790216297644061907967746158176077248308127562232078484346647736660539949022309498348627204805721593584775422394309013796130700092900938815, 5878417000780331798857557986948960441346454077777322673554492074557738723820678687258340632752903121339534981383754099436716471058805982328132476449941215, 6880685058088923360454978331496372535541661645462067499560473528440802336974353955146677751380306992934684346246375289762689840689886873086589146810253670, 15146619207080728721431458393485078963816476244333097953347229639115792867741651752502450634986215936423898371111845955206352077502413276865026693558237006, 24561722000140158259875681907751882896455273583426085483068435374059719582107181339559714944784457729629630905505829288142673667003135252718347624436999992, 6010611884765225018462632647496238859722174131255561001382929914235844040213056949996415280767936305317356224069768977951698543854166744388967466830014993, 8604205503844497975382087491471261691500399179059828399299104296451588437548671975425809986346145749171661080060732418214787755409974796257958457079446169, 17849614355900063808676124131118524997648354206170318621124380526614805924516446357913811050522125018804909585953790699698679819231112783249469477815502583, 6234389585613390991135923692863461447834739229259508233031228485187148629433094293262051827315840441484915246883666958651829552776346022719552733639725859, 7182121509006557499447407077345122412252018433797810895029451443225148321291242333489049265373361789052227738364702526754237606296484295417138966575888620, 19443880794014015420496260746566073718343224434504374185068813973081417513109615969506188667312188330969026502853036477823357874546093944860308749378123928, 8610303641124717889797443991956065248854876119400852317050875401537697893222693042608150518545123647005489128320318966059078814197980338543782093101929224, 18189657344522443517634178377315677739510993410467142695783340446334277100937541656038062664291573406146510812487138250282486667881441610169897763672518408, 12769524103064286110212485914744234557661966027173832097660581933836555038208706049841496544538548805882474347935602024737964843349772194256842191540878086, 8837434246318895916502293154110755972578717505950918004207279182412126578654858700979631762130823953557115254349577580538115563409772264268979590595878524, 19324198363953550925352268785176812194820419776972184980422284004516526551007702906947369233188769325685359100956902685342281892761746398096459283611700095, 6544445567585062391464238427740831918741801645901527392514359075570599386771893563973143304579733482518738394336251452408151054963276267458950864806421915, 19782164233646334359930792109811656791481571002302250069139549412842329364251080190727840483682229477400959169150663946355487669296131135025201043628456335, 24654727725994844473826157374327181005851067287265041892193560204158822169009982864716885228605931835154647930183426777271783324333532822986533226774265086, 11367700001184727702364259455710809696637145104881210683804803367907200276580882752977849415635463172339466011893449395308545212142141640106372598386717261, 7723357198545349821668979812671919910417796683836887116266560251282651027720737135635979056834665568049760420980280728405309337034762896171899580477002480, 21398687418697553578835459574840055346093502401109142628407521730770084196036749208195098924950079373674925283343188798074486306048809474454183994166974180, 16997038318816588635791819758541930890463030604684975995073453198114844532260969787929803990886438751158525116987184266958684749309609812361482795034864096, 4339399156132604150366164967401687799312125851272253539871718904995918292027308581839413327378932125753131966061907025562614788546630253997949558324058448, 3369244715664390907205531559411845140890026328037629605723521148667020393715678923998551404622131810559215463034330330056308421521633103928524691746117099, 1431673693086038692849537020631139597717739498569241557398192918130148683292292541696331760602755635390901527814481898826581528948318112412965942130662713, 12234491823547381535997745515100483987995055441921296380992354195976960740646886352979059304804649147370413146364909702622931536826695947352658073790958002, 20486544185673750188304644898717013329823792586834957536128145997362582227897781470171626317727958238504761010301781904015009865573784192019736553383956769, 19827763312631414631212341139803235237154761708691725840571357267274608976060420778014253451501335223474450356164637855273778227266125134233045237066067434, 6225563269029485616119328001022633060438304908294870484531262879807569345416136295093046641268484528633153018409094729356768533533009982065511740218322723, 20851355942845687286463902725276963337328262850967710843229819166674693086805559557131450566280824216213603045942500787564210001753540293538067758105213824, 19394814006961873736995866791673787881673571723123879095396834450906913938500266583807089149936580351387233972803876110326286848054332995476891966322678221, 22452100713584112241141868307578989327346305836951368987277589917832465013986855428179035785439557617531138257918255772862364701748005219968215969119420307]

Solution

This challenge is somewhat a sequel to the challenge "ezdlp" from N1CTF 2022, whose source was:

from Crypto.Util.number import *
from math import prod
from secret import flag

def keygen(pbits,kbits,k):
    p = getPrime(pbits)
    x = [getPrime(kbits + 1) for i in range(k)]
    y = prod(x)
    while 1:
        r = getPrime(pbits - kbits * k)
        q = 2 * y * r + 1
        if isPrime(q):
            return p*q, (p, q, r, x)

def encrypt(key, message):
    return pow(0x10001, message, key)

key = keygen(512, 24, 20)
flag = bytes_to_long(flag)
messages = [getPrime(flag.bit_length()) for i in range(47)] + [flag]
enc = [encrypt(key[0], message) for message in messages]

print(messages[:-1])
print(enc)

In that challenge, you could first recover the modulus key by computing GCDs of multiples of key which are obtained by finding small tit_i such that itimi=0\sum_i t_i m_i = 0 (and then noting that iciti1=0(modkey)\prod_i c_i^{t_i} - 1 = 0 \pmod{\mathrm{key}}). The rest of that challenge can be finished by factoring key and then solving the discrete logarithm problem to get the flag.

Recovery of the modulus in this challenge is slightly similar as it involves finding small linear combinations and then taking GCDs. The scenario in this challenge is slightly different however as we don't know the exponent exactly. Instead, we have 200 samples of random messages (159 bit) mim_i and are given gmif(modp)g^{m_i \oplus f} \pmod p where g=0x10001g = 0\mathrm{x}10001 and ff is the (159 bit) flag. For recovering pp, it helps to rewrite the XOR as sums using the identity

xy=x+y2i2ixiyix \oplus y = x + y - 2\sum_i 2^i x_i y_i

where xix_i and yiy_i denote the iith least significant bit of xx and yy respectively.

In our case, we write

mif=mi+f2j2jmi,jfjm_i \oplus f = m_i + f - 2 \sum_j 2^j m_{i,j} f_j

We want to find linear combinations of mifm_i \oplus f that equal 00, so that we can take the products of the corresponding ciphertexts to obtain multiples of the modulus in a similar way to the previous challenge. This gives us the idea to consider the bits of each mim_i.

In particular, we consider the following system of equations:

k0m0,0+k1m1,0++k199m199,0=0k0m0,1+k1m1,1++k199m199,1=0k0m0,158+k1m1,158++k199m199,158=0\begin{aligned} k_0 m_{0,0} + k_1 m_{1,0} + \cdots + k_{199} m_{199,0} &= 0 \\ k_0 m_{0,1} + k_1 m_{1,1} + \cdots + k_{199} m_{199,1} &= 0 \\ &\vdots \\ k_0 m_{0,158} + k_1 m_{1,158} + \cdots + k_{199} m_{199,158} &= 0 \\ \end{aligned}

as a matrix equation, this is:

[m0,0m1,0m199,0m0,1m1,1m199,1m0,158m1,158m199,158][k0k1k199]=[000]\begin{bmatrix} m_{0, 0} & m_{1,0} & \cdots & m_{199, 0} \\ m_{0, 1} & m_{1, 1} & \cdots & m_{199, 1} \\ \vdots \\ m_{0, 158} & m_{1, 158} & \cdots & m_{199, 158} \end{bmatrix} \begin{bmatrix} k_0 \\ k_1 \\ \vdots \\ k_{199} \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \\ \vdots \\ 0 \end{bmatrix}

This equation can be solved, and there are many solutions for the kik_i. Importantly, we also note that i=0199ki=0\sum_{i=0}^{199} k_i = 0, as this is enforced by the last row of the above matrix equation due to each of the mi,158m_{i,158} bits being 11. This then implies that for each solution to the above equation, we have

i=0199ki(mif)=i=0199ki(mi+f2j=01592jmi,jfj)=i=0199kimi+i=0199kif2i=0199kij=01592jmi,jfj=0\begin{aligned} \sum_{i=0}^{199} k_i (m_i \oplus f) &= \sum_{i=0}^{199} k_i \left ( m_i + f - 2\sum_{j=0}^{159} 2^j m_{i,j} f_j \right ) \\ &= \sum_{i=0}^{199} k_i m_i + \sum_{i=0}^{199} k_i f - 2 \sum_{i=0}^{199} k_i \sum_{j=0}^{159} 2^j m_{i, j} f_j \\ &= 0 \end{aligned}

This is perfect, because it means that for each solution, we also have

i=0199ciki=gi=0199ki(mif)=g0=1(modp)\prod_{i=0}^{199} c_i^{k_i} = g^{\sum_{i=0}^{199} k_i(m_i \oplus f)} = g^0 = 1 \pmod p

which means that

i=0199ciki1\prod_{i=0}^{199} c_i^{k_i} - 1

which is made up of values we know entirely, is a multiple of pp.

The only issue now is in computing these products. If we naively try to solve for the kik_i by looking at the kernel of the above matrix, we might get values that are too large to use as an exponent over the integers. To get around this, we use a technique which I first learnt from maple3142's no modulus challenge. The basic idea is to use LLL to find small solutions of kik_i, and then to deal with the negative values by just multiplying by the term with a positive exponent (since the resulting value will still be a multiple of pp). The lattice basis we use is simply the coefficient matrix (consisting of the bit values of the mim_i), augmented with the identity matrix. Because the basis is already quite short with entries consisting of only zeroes or ones, we can force the first 159 rows in the reduced basis to be 0 by scaling them appropriately. Performing the lattice reduction, taking GCDs, and removing some small factors eventually gives us the value of pp.

Now that we have pp, the next step is to recover the flag from the given gmifmodpg^{m_i \oplus f} \mod p values. At first glance, this doesn't seem easy; pp is a safe prime, and gg is of order (p1)/2(p-1)/2. However, we can reuse the above idea to find linear combinations of the messages which will help us to eliminate all but one bit of the flag, after which solving the discrete logarithm problem is easy (as we just check for one of two values). Consider the equation

[m0,0m1,0m199,0m0,1m1,1m199,1m0,158m1,158m199,158][k0k1k199]=[100]\begin{bmatrix} m_{0, 0} & m_{1,0} & \cdots & m_{199, 0} \\ m_{0, 1} & m_{1, 1} & \cdots & m_{199, 1} \\ \vdots \\ m_{0, 158} & m_{1, 158} & \cdots & m_{199, 158} \end{bmatrix} \begin{bmatrix} k_0 \\ k_1 \\ \vdots \\ k_{199} \end{bmatrix} = \begin{bmatrix} 1 \\ 0 \\ \vdots \\ 0 \end{bmatrix}

This is the same equation as above, except the target vector has a 11 in the first entry (corresponding to the least significant bit). As before i=0199ki=0\sum_{i=0}^{199} k_i = 0, and so a solution to this equation will also give us the following:

i=0199ki(mif)=i=0199ki(mi+f2j=01592jmi,jfj)=i=0199kimi+i=0199kif2i=0199kij=01592jmi,jfj=1+02f0=12f0\begin{aligned} \sum_{i=0}^{199} k_i (m_i \oplus f) &= \sum_{i=0}^{199} k_i \left ( m_i + f - 2\sum_{j=0}^{159} 2^j m_{i,j} f_j \right ) \\ &= \sum_{i=0}^{199} k_i m_i + \sum_{i=0}^{199} k_i f - 2 \sum_{i=0}^{199} k_i \sum_{j=0}^{159} 2^j m_{i, j} f_j \\ &= 1 + 0 - 2f_0 \\ &= 1 - 2f_0 \end{aligned}

Therefore, after computing

i=0199cikimodp\prod_{i=0}^{199} c_i^{k_i} \mod p

we simply check whether it equals to gg or g1modpg^{-1} \mod p to determine if f0f_0 is 00 or 11. This can be generalised to the other bits (all except for the most significant bit, which we know is 11 anyway), which allows us to satisfyingly recover the flag bit by bit.

import ast
from Crypto.Util.number import bytes_to_long
from binteger import Bin
from tqdm import tqdm
from lbc_toolkit import flatter

dat = open('./output.txt', 'r').read().splitlines()
messages = ast.literal_eval(dat[0].split('message = ')[1])
enc = ast.literal_eval(dat[1].split('enc = ')[1])

M = []
for m in messages:
    M.append(Bin(m, n=159).list)
M = Matrix(ZZ, M).augment(identity_matrix(200))
Q = diagonal_matrix([2^16] * 159 + [1] * 200)
print('performing lattice reduction...')
B = flatter(M * Q) / Q

print('recovering p...')
Gs = []
for r in tqdm(list(B)):
    if set(r[:159]) != {0}:
        continue
    K = r[159:]
    lhs = 1
    rhs = 1
    for i in range(200):
        if K[i] > 0:
            lhs *= pow(enc[i], K[i])
        else:
            rhs *= pow(enc[i], -K[i])
    Gs.append(lhs - rhs)
p = int(factor(gcd(Gs))[-1][0])
print(p, int(p).bit_length())

M = []
for m in messages:
    M.append(Bin(m, n=159).list)
M = Matrix(GF((p-1)//2), M).T
flag_bin = [1]
for i in range(1, 159):
    v = [0] * 159
    v[i] = 1
    K = M.solve_right(vector(v))
    s = prod(pow(enc[j], int(K[j]), p) for j in range(200)) % p
    if s == pow(0x10001, 1 << (158 - i), p):
        flag_bin.append(0)
    else:
        flag_bin.append(1)
    if len(flag_bin) % 8 == 1:
        print(Bin(flag_bin).bytes, ''.join(map(str, flag_bin)))

# n1ctf{s0o0_ezzz_dLLLp%$#@!}