首页 时政热点 科技头条 智能AI 安全攻防 数码硬件 开发者生态 归档 标签 关于

破解apbq-rsa系列CTF题目

2026-11-24 1 阅读 约10分钟阅读 先知社区
分享:
字号:
在标准 RSA 中我们通常只知道模数 n = pq。一些问题会额外给出一些线性提示: hi = a i p + b i q , i = 1 , 2 , … h_i = a_i\, p + b_i\, q, \qquad i = 1,2,\dots h i ​ = a i ​ p + b i ​ q , i = 1 , 2 , … apbq-rsa系列的题目就是给出了一些这样的提示,随着 ( a i , b i ) (a_i,b_i) ( a i , b i ) 的比例逐渐增加,我们恢复 p,q 的注意力也随之增加。此类的核心思路是利用 p, q 的线性关系与模 n 的整除/同余性质消元,再结合暴力枚举、LLL 格基约或题目多元小根方法 恢复 p, q。 apbq-rsa-i from Crypto.Util.number import getPrime, bytes_to_long from random import randint ​ p = getPrime(1024) q = getPrime(1024) n = p * q e = 0x10001 ​hints = [] for _ in range(2): a, b = randint(0, 2**12), randint(0, 2**312)hints.append(a * p + b * q) ​ FLAG = open('flag.txt', 'rb').read().strip() c = pow(bytes_to_long(FLAG), e, n) print(f'{n = }') print(f'{c = }') print(f'{hints = }') 这个简单的数学题 h i n t 1 = a 1 p + b 1 q h i n t 2 = a 2 p + b 2 qhint_1=a_1p + b_1 q\\hint_2=a_2p + b_2qhint1​=a1​p+b1​qhint2​=a2​p+b2​q构造消元, h i n t 1 * a 2 , h i n t 2 * a 1hint_1*a_2,hint_2*a_1 hint 1 ​ * a 2 ​ , hin t 2 ​ * a 1 ​ 会发现 h i n t 1 a 2 = a 2 a 1 p + a 2 b 1 q h i n t 2 a 1 = a 1 a 2 p + a 1 b 2 q h i n t 1 a 2 − h i n t 2 a 1 = ( a 2 b 1 − a 1 b 2 ) q h i n t 1 a 2 − h i n t 2 a 1 ≠ 0 ( mod q )hint_1a_2=a_2a_1 p + a_2b_1 q\\ hint_2a_1=a_1a_2 p + a_1b_2 q\\hint_1a_2-hint_2a_1=(a_2b_1-a_1b_2)q\\hint_1a_2-hint_2a_1\equiv0\pmod{q}hint1​a2​=a2​a1​p+a2​b1​qhin t 2​a 1​= a 1​a 2​p + a 1​b 2​q hin t 1​a 2​−hin t 2​a 1​=(a 2​b 1​−a 1​b 2​)q hin t 1​a 2​−hin t 2​a 1​== 0 ( mod q ) h i n t 1hint_1hint1​ 与 hint2hint_2hint2​ 构造可以出 q q q 的整数倍。即 q = g c d ( h i n t 1 a 2 − h i n t 2 a 1 , n ) q= gcd(hint_1a_2-hint_2a_1,n) q = g c d ( hin t 1 ​ a 2 ​ − hin t 2 ​ a 1 ​ , n ) p , q p, q p , q 都是1024位素数,n 是2048位。a是12位,b是312位。 h i n t = a p + b q hint=a p + b q hin t = a p + b q 约1336位。 h i n t 1 * a 2 − h i n t 2 * a 1hint_1*a_2-hint_2*a_1hint 1​*a 2​−hint2​*a1​约1348位。所以这个gcd大概率就是q本身。爆破(a 1,a 2)(a_{1},a_{2})(a 1​、a 2​) import itertools from Crypto.Util.number import long_to_bytes from math import gcd from multiprocessing import Pool import os n = 14660571735908952489002993827903664741357457851760908649720903490242781200599 27920094155562537748032230076143421478424335586553147688577893304920503563585 75117244456361894120102322888839979623343749769670724735692681049382421623741 46303713780734432405002257811743659454802783820254267629648005198676228493220 09091057065198172095678243657956338321052188140869789557498899816607308818118 24607816255834498951700694162969110800371082422009438191903295927222102403724 78763827674403639468179156438805357971476738826284179397702996711111034575635 088841241354137431691608708586537071546432546205216081312804647066737570894023 c = 30142284658390242225319131788767559972025790665719465681179576209926261840750 72645592929274030475721860516337363549964023809640935927422141596369521625373 03288799217154498796305364922226883936229614029825456755724724395963187364265 36287680565902512887178769672388443636682166693451111992353667824910996284421 27257864721021039424850656858549634126388057122624827577450574032676846584
这篇文章对您有帮助吗?

订阅66必读

每日精选科技资讯,直达你的邮箱