0x00 dp
题目给出(e,n,dp,c),这里的dp为:d mod(p-1) =dp
现在需要分解n得到p和q ,计算私钥d就得到答案了。
推导过程:
已知
dp = d mod(p-1)
两边同乘 e 得到
dp*e = e*d mod (p-1)
设未知数k1,上式变形为
dp*e + k1(p-1) = e*d
又因为 e*d = 1 mod (ϕ(n)),ϕ(n)=(p-1)*(q-1)
dp*e + k1(p-1) =1 mod (ϕ(n))
设未知数k2,上式变形为
dp*e + k1(p-1) - 1 = k2(p-1)(q-1)
(p-1)[k2(q-1) - k1] + 1 = dp*e
因为 dp = d mod (p-1)
所以 dp < p-1,
e>[k2(q-1) - k1]
设未知数x = [k2(q-1) - k1],带入上述方程
(p-1) *x + 1 = dp*e
通过上述推导,(p-1) *x + 1 = dp * e 并且x∈(0,e),遍历1-65537,找到p,q= n/p, 从而得到d,计算得到flag.
#-*- coding:utf-8 -*-
import libnum
import gmpy2
e=65537
n=9637571466652899741848142654451413405801976834328667418509217149503238513830870985353918314633160277580591819016181785300521866901536670666234046521697590230079161867282389124998093526637796571100147052430445089605759722456767679930869250538932528092292071024877213105462554819256136145385237821098127348787416199401770954567019811050508888349297579329222552491826770225583983899834347983888473219771888063393354348613119521862989609112706536794212028369088219375364362615622092005578099889045473175051574207130932430162265994221914833343534531743589037146933738549770365029230545884239551015472122598634133661853901
dp=81339405704902517676022188908547543689627829453799865550091494842725439570571310071337729038516525539158092247771184675844795891671744082925462138427070614848951224652874430072917346702280925974595608822751382808802457160317381440319175601623719969138918927272712366710634393379149593082774688540571485214097
c=5971372776574706905158546698157178098706187597204981662036310534369575915776950962893790809274833462545672702278129839887482283641996814437707885716134279091994238891294614019371247451378504745748882207694219990495603397913371579808848136183106703158532870472345648247817132700604598385677497138485776569096958910782582696229046024695529762572289705021673895852985396416704278321332667281973074372362761992335826576550161390158761314769544548809326036026461123102509831887999493584436939086255411387879202594399181211724444617225689922628790388129032022982596393215038044861544602046137258904612792518629229736324827
flag = ''
for i in range(1,e):
if (dp*e+1)%i==0:
if n%((dp*e+1)/i+1)==0:
p = (dp*e+1)/i+1
q = n/p
phi = (p-1)*(q-1)
d = gmpy2.invert(e,phi)
print libnum.n2s(pow(c,d,n))
#flag{c3009b61-f9ed-4b20-8855-edab53e89530}
0x01 sm4
将key和密文c转16进制。在使用sm4解密,在转字符得到flag.工具下载 提取码:6s6q
key = [13, 204, 99, 177, 254, 41, 198, 163, 201, 226, 56, 214, 192, 194, 98, 104]
c = [46, 48, 220, 156, 184, 218, 57, 13, 246, 91, 1, 63, 60, 67, 105, 64, 149, 240, 217, 77, 107, 49, 222, 61, 155, 225, 231, 196, 167, 121, 9, 16, 60, 182, 65, 101, 39, 253, 250, 224, 9, 204, 154, 122, 206, 43, 97, 59]
for i in key:
print(hex(i)[2:].zfill(2),end='')
print('')
for i in c:
print(hex(i)[2:].zfill(2),end='')
print('')
#key:0dcc63b1fe29c6a3c9e238d6c0c26268
#c:2e30dc9cb8da390df65b013f3c43694095f0d94d6b31de3d9be1e7c4a77909103cb6416527fdfae009cc9a7ace2b613b
#使用工具解密sm4得到:534D343A2020666C61677B31636161393662652D343236362D346138652D626432632D6563653937373439353439377D
import libnum
m =0x534D343A2020666C61677B31636161393662652D343236362D346138652D626432632D6563653937373439353439377D
print(libnum.n2s(m))
'''
output:
0dcc63b1fe29c6a3c9e238d6c0c26268
2e30dc9cb8da390df65b013f3c43694095f0d94d6b31de3d9be1e7c4a77909103cb6416527fdfae009cc9a7ace2b613b
SM4: flag{1caa96be-4266-4a8e-bd2c-ece977495497}
'''