1.题目:
from Crypto.Util.number import getPrime, bytes_to_long
FLAG = b"flag{}"
def enc(m):
return pow(m, e, N)
if __name__ == "__main__":
l = 256
p = getPrime(1024)
N = p * getPrime(1024)
e = 65537
a = (p >> l) << l
print("N:", N)
print("Known part of p:", hex(a))
print("Length of the unknown part:", l)
print("enc:", enc(bytes_to_long(FLAG)))
N: 13139369168613206469808493070119137888363636548621629780897948879328793540933675072448361493321304924953815474270401406259487525517560528123707016104942485164559271692275987380567766009184969340122208041180122234792566147648471202470677782205185423853314467362074540818483729953544353584322270414479260852672948012862257167187569701381652931473637503302338392147780573148724508117699531886205586281824118899931516823621049590863613262210219765105389989391065557707559113268724368695051264276619633555407916385088611885715568165460641318205321508100969473959719364829756492542217470309748646183210141490634293731384313
Known part of p: 0xce2f93251a3a97404a11c1fe88cf15c7aaf26ffd508ff006933bff2e9ea0c6197a98f1188f03b74b16d564e958a84c877fc0e21faf00f0ae42f26bde226ebf7c9732f17d860b81d139799832d510b91001967fc33ff2d9fbd4c4767fa2438e480000000000000000000000000000000000000000000000000000000000000000
Length of the unknown part: 256
enc: 10994040238462701583659972453101990790105813765012990749608185072718600854440721383239626137413394180017327728436373282101414910961527694291269438429124480262436140242346606299693570528165786128342639697068827177018329599990851910019879374047005450085901527704075107943071874274180276690059744636245418370546246757239666384320024728428669095536636012185174919921512858446990367609436305909104080544807406108497002947094974486271586520410674562299953931051491793506395053049529401503138822284281986730639405804678960905130223082038659899687410133538282139577181626031301078543719687909557541703207389570582210505090095
2.使用软件sagemath notbook,利用sage 计算p,q ,打开其网页版:
生成一个以x为符号的一元多项式环。
PR.<x> = PolynomialRing(Zmod(n))
定义求解的函数
f = x + p4
多项式小值根求解及因子分解
X:表示求解根的上界
roots = f.small_roots(X=2^kbits, beta=0.4)
脚本1:
def phase3(high_p, n):
R.<x> = PolynomialRing(Zmod(n), implementation='NTL')
p = high_p + x
x0 = p.small_roots(X = 2^256, beta = 0.1)[0]
P = int(p(x0))
Q = n // P
print(P)
assert n == P*Q
n = 13139369168613206469808493070119137888363636548621629780897948879328793540933675072448361493321304924953815474270401406259487525517560528123707016104942485164559271692275987380567766009184969340122208041180122234792566147648471202470677782205185423853314467362074540818483729953544353584322270414479260852672948012862257167187569701381652931473637503302338392147780573148724508117699531886205586281824118899931516823621049590863613262210219765105389989391065557707559113268724368695051264276619633555407916385088611885715568165460641318205321508100969473959719364829756492542217470309748646183210141490634293731384313
p4= 0xce2f93251a3a97404a11c1fe88cf15c7aaf26ffd508ff006933bff2e9ea0c6197a98f1188f03b74b16d564e958a84c877fc0e21faf00f0ae42f26bde226ebf7c9732f17d860b81d139799832d510b91001967fc33ff2d9fbd4c4767fa2438e480000000000000000000000000000000000000000000000000000000000000000
e = 0x10001
phase3(p4, n)
脚本2:
from sage.all import *
n = 13139369168613206469808493070119137888363636548621629780897948879328793540933675072448361493321304924953815474270401406259487525517560528123707016104942485164559271692275987380567766009184969340122208041180122234792566147648471202470677782205185423853314467362074540818483729953544353584322270414479260852672948012862257167187569701381652931473637503302338392147780573148724508117699531886205586281824118899931516823621049590863613262210219765105389989391065557707559113268724368695051264276619633555407916385088611885715568165460641318205321508100969473959719364829756492542217470309748646183210141490634293731384313
p4=0xce2f93251a3a97404a11c1fe88cf15c7aaf26ffd508ff006933bff2e9ea0c6197a98f1188f03b74b16d564e958a84c877fc0e21faf00f0ae42f26bde226ebf7c9732f17d860b81d139799832d510b91001967fc33ff2d9fbd4c4767fa2438e48
e = 0x10001
pbits = 1024
kbits = pbits - p4.nbits()
print(p4.nbits())
p4 = p4 << kbits
PR.<x> = PolynomialRing(Zmod(n))
f = x + p4
roots = f.small_roots(X=2^kbits, beta=0.4)
if roots:
p = p4+int(roots[0])
print ("n: ", n)
print ("p: ", p)
print ("q: ", n/p)
3. 求出p之后,flag=pow(c,d,n)
import gmpy2
from Crypto.Util.number import *
n=13139369168613206469808493070119137888363636548621629780897948879328793540933675072448361493321304924953815474270401406259487525517560528123707016104942485164559271692275987380567766009184969340122208041180122234792566147648471202470677782205185423853314467362074540818483729953544353584322270414479260852672948012862257167187569701381652931473637503302338392147780573148724508117699531886205586281824118899931516823621049590863613262210219765105389989391065557707559113268724368695051264276619633555407916385088611885715568165460641318205321508100969473959719364829756492542217470309748646183210141490634293731384313
p=144788620032174382850620144740051190004909189981823947687939533751214863574416128745504241848556196436424873523723598270584585967337289044432722639171175692346268666992200205255923809991550657146510500317550922775903638618780099941385823936837216968003763133275736822717054686219559882653840804217844349691379
q=90748631803338033382719802077109017154157229824571563567405846296920929137783882558055978191525135923468682426958334699526354800278355091313071807697974640522544224693325158583809586604573129139663109910278125191334731044785721145359018060504935418728787028099072072663992579146767368168659319905098993800547
c=10994040238462701583659972453101990790105813765012990749608185072718600854440721383239626137413394180017327728436373282101414910961527694291269438429124480262436140242346606299693570528165786128342639697068827177018329599990851910019879374047005450085901527704075107943071874274180276690059744636245418370546246757239666384320024728428669095536636012185174919921512858446990367609436305909104080544807406108497002947094974486271586520410674562299953931051491793506395053049529401503138822284281986730639405804678960905130223082038659899687410133538282139577181626031301078543719687909557541703207389570582210505090095
e=65537
d=gmpy2.invert(e,(p-1)*(q-1))
flag=pow(c,d,n)
print(long_to_bytes(flag))
最后得出flag:flag{b082411e0d6e296d4598b69825fc0cc5}