from Crypto.Cipher import AES from Crypto.Util.number import long_to_bytes as l2b, bytes_to_long as b2l from secret import FLAG from os import urandom from base64 import b64encode, b64decode
# These modes of Block Cipher are just like Stream Cipher. Do you know them? AES_enc = AES.new(urandom(16), AES.MODE_ECB).encrypt defAES_CFB (iv, pt): ct = b"" for i inrange(0, len(pt), 16): _ct = XOR(AES_enc(iv), pt[i : i + 16]) iv = _ct ct += _ct return ct
defAES_OFB (iv, pt): ct = b"" for i inrange(0, len(pt), 16): iv = AES_enc(iv) ct += XOR(iv, pt[i : i + 16]) return ct
defAES_CTR (iv, pt): ct = b"" for i inrange(0, len(pt), 16): ct += XOR(AES_enc(iv), pt[i : i + 16]) iv = counter_add(iv) return ct
from sage.allimport * from Crypto.Util.number import * from secret import p, q, flag
assert isPrime(p) and isPrime(q) n = p * q a, b = matrix(ZZ, [[p, 1], [q, 1]]).solve_right( vector([p**2 - p**3, q**2 - q**3]) ) E = EllipticCurve(Zmod(n), [a, b]) G = E(p, p) + E(q, q) C = bytes_to_long(flag) * G
a = -1049512290645561483277399447040672259507710914145558231422452159145941450861058912834056552784840698307176425328594627265181382568207073595223799102540059103656850409121714215271402071402990265653829990643814289333297114436290307127182601793045470624406368512814269833830187545236393724608995894644699923989 b = 330613225413866308562655832653992432640737790102976283577689980446254238304479688134993945656361409867735093176372274589048066502491030816811279723518019832240148759433890104257541015694288688653084062998961288644429744942281764740765767448933787468732728303440425139427370295303413074746846731173227818565326124721081874768870022303341674817123171380954083010086565749443463586594318218908360567200188035652004143989131725183710453256926775457844063169319469 C = (267111004965766851197295766324872918045366663010386225569683352174816889947153426038920564765985480495027226192426180920889404710714177330839745847098719253913784748674975923941362014119630482337688147428774108074983765342590544406095084448586154162465772821665560547734890209137946668822330843444608421730, 121650402415930828032211963598144915558171373162163617470672182014503810780161804477407976153593724606514937006081912867636117646613415693478218365932212102557909492288758305287088882502760464160705341780505639501694329242419144215441270993917308948949819834611451749926628000691771375822508363489298910865)
首先,利用給定的a和b算p和q 直接z3XD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
>>> from z3 import * >>> s=Solver() >>> x, y=Ints("x, y") >>> s.add(x>0) >>> s.add(y>0) >>> s.add(x!=y) >>> a = -1049512290645561483277399447040672259507710914145558231422452159145941450861058912834056552784840698307176425328594627265181382568207073595223799102540059103656850409121714215271402071402990265653829990643814289333297114436290307127182601793045470624406368512814269833830187545236393724608995894644699923989 >>> b = 330613225413866308562655832653992432640737790102976283577689980446254238304479688134993945656361409867735093176372274589048066502491030816811279723518019832240148759433890104257541015694288688653084062998961288644429744942281764740765767448933787468732728303440425139427370295303413074746846731173227818565326124721081874768870022303341674817123171380954083010086565749443463586594318218908360567200188035652004143989131725183710453256926775457844063169319469 >>> s.add(x**2-x**3-a*x==b) >>> s.add(y**2-y**3-a*y==b) >>> s.check() sat >>> s.model() [x, = 796516347571618382842409566670391818297833481271542059034954968631944200751696685241692453026124019466008822177825139658601579493035399097176001286756611, y = 359160846099444348290305694779134753321907709661985769865266028792407078112888527565129439985352509538424954784005539823019894001632619107445650921758147]
好啦,有p和q之後,可以算出G並拆order分別算它們的DLP: (以q為例,p一樣)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
sage: a=-104951229064556148327739944704067225950771091414555823142245215914591450861058912834056552784840698307176425328594627265181382568207073595237991025400591036568504091217142152714020714029902656538299906438142893329711443629030712718260179304547062440636851281426983383018754523639374608995894644699923989 sage: b=330613225413866308562655832653992432640737790102976283577689980446254383044796881349939456563614098677350931763722745890480665024910308168117972351801983224014875943389010425754101569428868865308406299896128864429744942281764740765767448933787468732728303440425139427370295303413074468467311732278185653261247210818747688700223033416748171231713809540831008656574944346358659431821890836056720018803565200414398913172518371053256926775457844063169319469 sage: q=359160846099444348290305694779134753321907709661985769865266028792407078112888527565129439985352509538424954784005539823019894001632619107445650921758147 sage: E = EllipticCurve(GF(q), [a, b]) sage: G=E(28607748532586155305766590971512659203413099194155217832119487667929904063769085772752568246866731921408737399300889149334418551010959577073796486388937480663378183502384364325599822278159619696537650011902619010103876865119734678532259458237178383683768508566703123494722154129671015135020887186845060 , 11556771936710627311327152614495265716197411903352782890022099742435127886458521280682189301147652900443377696183067981621473494668018204621652208514757) sage: C=E(26711100496576685119729576632487291804536666301038622556968335217486889947153426038920564765985480495027226192426180920889404710714177330897458470987192539137847486749759239413620141196304823376881474287741080498376534259054440609508444858615416246577282166556054773489020913794668822330843444608421730, 12165040241593082803221196359814491555817137316163617470672182014503810780161804477407976153593724606514937006081912866361176466134156934782183659322121025579094922887583052870888825027604616070534178050563950169432924241914421544127099391730894894981983461145749926628000691771375822508363489298910865) sage: factors, exponents = zip(*factor(E.order())) ....: primes = [factors[i] ^ exponents[i] for i inrange(len(factors))] ....: dlogs = [] ....: for fac in primes: ....: t = int(G.order()) // int(fac) ....: dlog = discrete_log(t*C,t*G,operation="+") ....: dlogs += [dlog] ....: print("factor: "+str(fac)+", Discrete Log: "+str(dlog)) #calculates discrete logarithm for each prime order ....: ....: l = crt(dlogs,primes) ....: print(l)
#! /usr/bin/python3 from Crypto.Util.number import bytes_to_long, long_to_bytes, getPrime import os from secret import FLAG defencrypt(m, e, n): enc = pow(bytes_to_long(m), e, n) return enc
defdecrypt(c, d, n): dec = pow(c, d, n) return long_to_bytes(dec)
if __name__ == "__main__": whileTrue: p = getPrime(1024) q = getPrime(1024) n = p * q phi = (p - 1) * (q - 1) e = 3 if phi % e != 0 : d = pow(e, -1, phi) break print(f"{n=}, {e=}") print("FLAG: ", encrypt(FLAG, e, n)) for _ inrange(3): try: c = int(input("Any message for me?")) m = decrypt(c, d, n) print("How beautiful the message is, it makes me want to destroy it .w.") rah=bytes_to_long(os.urandom(8))#added my self print(f"{rah=}")#added my self new_m = long_to_bytes(bytes_to_long(m) ^ rah) print( "New Message: ", encrypt(new_m, e, n) ) except: print("?") exit()
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler from urllib.parse import urlparse, parse_qs import re, os
if os.path.exists("/flag"): withopen("/flag") as f: FLAG = f.read().strip() else: FLAG = os.environ.get("FLAG", "flag{this_is_a_fake_flag}") URL_REGEX = re.compile(r"https?://[a-zA-Z0-9.]+(/[a-zA-Z0-9./?#]*)?")
if __name__ == "__main__": server = ThreadingHTTPServer(("", 7777), RequestHandler) server.allow_reuse_address = True print("Starting server, use <Ctrl-C> to stop") server.serve_forever()
default.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14
server { listen 7778; listen [::]:7778; server_name localhost;