defphi(n): if n <= 1: return n for i inrange(2, n + 1): if n % i == 0: ans = 1 while n % i == 0: n //= i ans *= i ans //= i ans *= i - 1 return ans * phi(n)
defcommon(x, y): if x > y: x, y = y, x i = 2 ans = 1 while i <= x: while x % i == 0and y % i == 0: x //= i y //= i ans *= i i += 1 return ans
n = 67108934 total = 0 phin = 33554466 for i inrange(1, phin): if gcd(i, phin) == 1: total += pow(7, i, n) print("Flag is Aegis{%d}" % total)
Kinoko的代码 III
源代码:
1 2 3 4 5 6 7 8 9
P = 0x7fffffff Q = 0x100 n = 0x100 a = [(i ** (i ** i)) % P for i inrange(n)] res = 0 for x in product(a, repeat=8): if reduce(add, x) % Q == 0: res += reduce(mul, x) print("Flag is Aegis{%d}" % (res % (P * Q)))
尝试用快速幂计算出了a,后面的部分就没有思路了,感觉跟 $GF(2^m)$ 有关。
Kinoko的代码 IV
源代码:
1 2 3 4 5 6 7 8 9 10 11
from Crypto.Util.number import *
msg = b'......' g = 23 p = 335215034881592512312398694238485179340610060759881511231472142277527176340784432381542726029524727833039074808456839870641607412102746854257629226877248337002993023452385472058106944014653401647033456174126976474875859099023703472904735779212010820524934972736276889281087909166017427905825553503050645575935980580803899122224368875197728677516907272452047278523846912786938173456942568602502013001099009776563388736434564541041529106817380347284002060811645842312648498340150736573246893588079033524476111268686138924892091575797329915240849862827621736832883215569687974368499436632617425922744658912248644475097139485785819369867604176912652851123185884810544172785948158330991257118563772736929105360124222843930130347670027236797458715653361366862282591170630650344062377644570729478796795124594909835004189813214758026703689710017334501371279295621820181402191463184275851324378938021156631501330660825566054528793444353 x = bytes_to_long(msg) h = pow(g,x,p) print(h) ''' 179956689939799978686596162623009071834749620910632881129570185702597885675366053746660084250822996742213156358846254579536681385720261821941496018561712101559148720253943504890341017743734982584598207516433225345830605843249917382020797858837178839235576965252932584506546764681169682067451758822339982991607788445472451090172187831874941209496749224595861824389402308122683468407022055642183997505151642262281735659513608920324853368172095765041692780083964849712368477052130574239545836322863774186581648844719904538237553913580382934907373884521762228204915991345935021715751152215665694954681931730050491031883519789246541180580522272095192525557554130071875996891173839573092163026966683532806284169788149085982141065261043806380173094680150058915109078775560035635889930619074748416389716690011996098383845646464047453083163083103550738288711630170975733999017234569638947005463854359023927470291784650805324521819096823 '''
c = 179956689939799978686596162623009071834749620910632881129570185702597885675366053746660084250822996742213156358846254579536681385720261821941496018561712101559148720253943504890341017743734982584598207516433225345830605843249917382020797858837178839235576965252932584506546764681169682067451758822339982991607788445472451090172187831874941209496749224595861824389402308122683468407022055642183997505151642262281735659513608920324853368172095765041692780083964849712368477052130574239545836322863774186581648844719904538237553913580382934907373884521762228204915991345935021715751152215665694954681931730050491031883519789246541180580522272095192525557554130071875996891173839573092163026966683532806284169788149085982141065261043806380173094680150058915109078775560035635889930619074748416389716690011996098383845646464047453083163083103550738288711630170975733999017234569638947005463854359023927470291784650805324521819096823 m = 23 n = 335215034881592512312398694238485179340610060759881511231472142277527176340784432381542726029524727833039074808456839870641607412102746854257629226877248337002993023452385472058106944014653401647033456174126976474875859099023703472904735779212010820524934972736276889281087909166017427905825553503050645575935980580803899122224368875197728677516907272452047278523846912786938173456942568602502013001099009776563388736434564541041529106817380347284002060811645842312648498340150736573246893588079033524476111268686138924892091575797329915240849862827621736832883215569687974368499436632617425922744658912248644475097139485785819369867604176912652851123185884810544172785948158330991257118563772736929105360124222843930130347670027236797458715653361366862282591170630650344062377644570729478796795124594909835004189813214758026703689710017334501371279295621820181402191463184275851324378938021156631501330660825566054528793444353 d=discrete_log(c,mod(m,n)) msg = long_to_bytes(d) print(msg)
notRSA
源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from Crypto.Util.number import getPrime, bytes_to_long
p = getPrime(512) q = getPrime(512) n = p * q flag = b'Aegis{......}' m = bytes_to_long(flag) c = pow(m, 2, n) print(p**3+q**3) print(p*q-p-q) print(c)
import sympy as sp a = 2683207403901766067825485634185601766409570143649318199140952968037512600470579376068383383524815864039841298998044483348310172352574406995610031363899276749157640097193160488032526253335175497216620520711500711141110234147693948098688349034232723537263489946840300271580577412796742296691684971450119145954769829625063379511509190333209177419150804570514746916533943674944751821667853376532291018641849933876951962902976903371221223111423718945634451076584248030 b = 114716649959013852657615268840231124447472690710601678501437134218521741792245535933871147569266364478277891322010833300443127314646898434178369690377612566380887447356076682596113975161733897245200003941284379586690945264751249879210669330319947878473291341185035678293547495739763970613403135670992913681859 p = [1, -3, -3*b, -a]
x = sp.Symbol('x') f = x**3 + p[1]*x**2 + p[2]*x + p[3] s = sp.solve(f) # print(s[0])
from Crypto.Util.number import long_to_bytes import gmpy2
e = 2 c = 23588520303183909632881265716945062770267850520083979450971468612710595466903076931312046174728593508895307445955963477450821781428035995913285067064035796132808196500567866710084350514597397699053074465810242481186367241 p = 9294245038014495018370025503838233837842445450717112861530019994089208464544256432365138864858019427603469395634623982915723003321692974199306043896395471 q = 12342761514228429158263998414220938515337038241757334937761557332062702646597784278541920751268963691194708568191829119140194036616936954532285327408748239 n = p*q u = pow(c, (p+1)//4, p) v = pow(c, (q+1)//4, q) # sp+tq=1 s = gmpy2.invert(p, q) # (p^-1) mod q t = gmpy2.invert(q, p) # (q^-1) mod p x = (t*q*u+s*p*v) % n y = (t*q*u-s*p*v) % n
from Crypto.Cipher import AES from Crypto.Util.number import inverse from Crypto.Util.Padding import pad, unpad from collections import namedtuple from random import randint import hashlib import os
Point = namedtuple("Point", "x y")
O = 'Origin'
FLAG = b'Aegis{......}'
defcheck_point(P: tuple): if P == O: returnTrue else: return (P.y**2 - (P.x**3 + a*P.x + b)) % p == 0and0 <= P.x < p and0 <= P.y < p
defpoint_inverse(P: tuple): if P == O: return P return Point(P.x, -P.y % p)
defpoint_addition(P: tuple, Q: tuple): if P == O: return Q elif Q == O: return P elif Q == point_inverse(P): return O else: if P == Q: lam = (3*P.x**2 + a)*inverse(2*P.y, p) lam %= p else: lam = (Q.y - P.y) * inverse((Q.x - P.x), p) lam %= p Rx = (lam**2 - P.x - Q.x) % p Ry = (lam*(P.x - Rx) - P.y) % p R = Point(Rx, Ry) assert check_point(R) return R
defdouble_and_add(P: tuple, n: int): Q = P R = O while n > 0: if n % 2 == 1: R = point_addition(R, Q) Q = point_addition(Q, Q) n = n // 2 assert check_point(R) return R
defgen_shared_secret(Q: tuple, n: int): S = double_and_add(Q, n) return S.x
p = 310717010502520989590157367261876774703 a = 2 b = 3 E = EllipticCurve(GF(p), [a, b]) K = (260053855577519367767956057258891222403, 179596564645843970846404853116641598439) G = (179210853392303317793440285562762725654, 105268671499942631758568591033409611165) K = E.point(K) G = E.point(G)
# primes中为点G的阶的质因数分解 primes = [2, 3**7, 139, 165229, 31850531, 270778799, 179317983307] dlogs = [] for fac in primes: t = int(G.order()) // int(fac) dlog = discrete_log(t*K, t*G, operation='+') dlogs += [dlog] print(str(fac) + ',' + str(dlog)) n = crt(dlogs, primes) print('n = ' + str(n))
from Crypto.Cipher import AES from Crypto.Util.number import inverse from Crypto.Util.Padding import pad, unpad from collections import namedtuple from random import randint import hashlib import os
Point = namedtuple("Point", "x y")
O = 'Origin'
def check_point(P: tuple): if P == O: return True else: return (P.y**2 - (P.x**3 + a*P.x + b)) % p == 0 and 0 <= P.x < p and 0 <= P.y < p
def point_inverse(P: tuple): if P == O: return P return Point(P.x, -P.y % p)
def point_addition(P: tuple, Q: tuple): if P == O: return Q elif Q == O: return P elif Q == point_inverse(P): return O else: if P == Q: lam = (3*P.x**2 + a)*inverse(2*P.y, p) lam %= p else: lam = (Q.y - P.y) * inverse((Q.x - P.x), p) lam %= p Rx = (lam**2 - P.x - Q.x) % p Ry = (lam*(P.x - Rx) - P.y) % p R = Point(Rx, Ry) assert check_point(R) return R
def double_and_add(P: tuple, n: int): Q = P R = O while n > 0: if n % 2 == 1: R = point_addition(R, Q) Q = point_addition(Q, Q) n = n // 2 assert check_point(R) return R
p = 310717010502520989590157367261876774703 a = 2 b = 3 E = EllipticCurve(GF(p), [a, b]) K = (260053855577519367767956057258891222403, 179596564645843970846404853116641598439) G = (179210853392303317793440285562762725654, 105268671499942631758568591033409611165) K = E.point(K) G = E.point(G)
primes = [2, 3**7, 139, 165229, 31850531, 270778799, 179317983307] dlogs = [] for fac in primes: t = int(G.order()) // int(fac) dlog = discrete_log(t*K, t*G, operation='+') dlogs += [dlog] print(str(fac) + ',' + str(dlog)) n = crt(dlogs, primes)