CTF入门Crypto之RSA(直接分解模数N)
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

一、RSA基本原理
(1)独立地选择两个大的素数p和q(100~200位十进制数)。
(2)计算出模数 N = p * q,计算欧拉函数值 φ = (p-1) * (q-1)。
(3)然后选择一个e(1<e<φ),(φ,e)=1(即e和φ互质)(互质:公约数只有1的两个整数)。
(4)取e的模反数d,计算方法为:e * d ≡ 1 (mod φ) (模反元素:如果两个正整数e和n互质,那么一定可以找到整数d,使得 e * d – 1 被n整除,或者说e * d被n除的余数是1。这时,d就叫做e的“模反元素”。欧拉定理可以用来证明模反元素必然存在。两个整数a,b,它们除以整数M所得的余数相等:a ≡ b(mod m),比如说5除3余数为2,11除3余数也为2,于是可写成11 ≡ 5(mod 3)。)。
(5)得到公钥(n,e)和私钥(n,d)。
(6)对明文m进行加密:c = pow(m, e, N),可以得到密文c。
(7)对密文c进行解密:m = pow(c, d, N),可以得到明文m。
二、CTF中的RSA
1、直接分解模数N
(1)原理
由RSA的基本原理可知,通过分解N,得到p和q是最直接的攻击方法,但也是最难的攻击方法,RSA在设计时就已通过数学方法的证明,只要p和q选取的当,N不可能被分解(这里不能被分解指的是在有限的时间内,一般来说破解所花的时间要在该信息存在价值的时间范围内),当然目前量子计算机是破解RSA的一种有力工具了。 在CTF中,如果是考察分解N来解题,则意味着题目中的N是简单的,通过工具就可以分解的出来。常用的工具是windows平台的RSATool和在线分解(http://factordb.com),在线分解的原理是该网站存储了一些N及N的分解值p和q
(2)例题,直接给出n和e
import gmpy2n = 920139713# p 和 q通过在线网站http://factordb.com/index.php分解p = gmpy2.mpz(18443)q = gmpy2.mpz(49891)e = gmpy2.mpz(19)phi_n = (p-1)*(q-1)d = gmpy2.invert(e, phi_n)c = """7047967927522111522747041641841402236827083548329523526307290545978847648329523545978847666355179247520680445978847642831337447520680445978847642539213770479679245826567734152224652483295235534149509425392137428313374425392137341524652458265677263072905483295235828509797341524652425392137475206804428313374483295235475206804459788476306220148"""result = ""c_list = c.split()#print(c_list)for i in c_list: result += chr(pow(int(i),d,n))print(result)import gmpy2n = 920139713# p 和 q通过在线网站http://factordb.com/index.php分解p = gmpy2.mpz(18443)q = gmpy2.mpz(49891)e = gmpy2.mpz(19)phi_n = (p-1)*(q-1)d = gmpy2.invert(e, phi_n)c = """7047967927522111522747041641841402236827083548329523526307290545978847648329523545978847666355179247520680445978847642831337447520680445978847642539213770479679245826567734152224652483295235534149509425392137428313374425392137341524652458265677263072905483295235828509797341524652425392137475206804428313374483295235475206804459788476306220148"""result = ""c_list = c.split()#print(c_list)for i in c_list: result += chr(pow(int(i),d,n))print(result)import gmpy2n = 920139713# p 和 q通过在线网站http://factordb.com/index.php分解p = gmpy2.mpz(18443)q = gmpy2.mpz(49891)e = gmpy2.mpz(19)phi_n = (p-1)*(q-1)d = gmpy2.invert(e, phi_n)c = """7047967927522111522747041641841402236827083548329523526307290545978847648329523545978847666355179247520680445978847642831337447520680445978847642539213770479679245826567734152224652483295235534149509425392137428313374425392137341524652458265677263072905483295235828509797341524652425392137475206804428313374483295235475206804459788476306220148"""result = ""c_list = c.split()#print(c_list)for i in c_list: result += chr(pow(int(i),d,n))print(result)(3)例题,给出key和flan.enc
import gmpy2from Crypto.PublicKey import RSAimport rsapublic_key = "./file/pub.key"cipher_file = "./file/flag.enc"#读入公钥with open(public_key, "r") as f: key = RSA.importKey(f) n = key.n e = key.e print(key.n) #86934482296048119190666062003494800588905656017203025617216654058378322103517 print(key.e) #65537#大数分解得到p = 285960468890451637935629440372639283459q = 304008741604601924494328155975272418463phin = (q-1)*(p-1)d = gmpy2.invert(e, phin)key = rsa.PrivateKey(n, e, int(d), p, q)with open("./file/flag.enc", "rb+") as f: f = f.read() print(rsa.decrypt(f, key))import gmpy2from Crypto.PublicKey import RSAimport rsapublic_key = "./file/pub.key"cipher_file = "./file/flag.enc"#读入公钥with open(public_key, "r") as f: key = RSA.importKey(f) n = key.n e = key.e print(key.n) #86934482296048119190666062003494800588905656017203025617216654058378322103517 print(key.e) #65537#大数分解得到p = 285960468890451637935629440372639283459q = 304008741604601924494328155975272418463phin = (q-1)*(p-1)d = gmpy2.invert(e, phin)key = rsa.PrivateKey(n, e, int(d), p, q)with open("./file/flag.enc", "rb+") as f: f = f.read() print(rsa.decrypt(f, key))import gmpy2from Crypto.PublicKey import RSAimport rsapublic_key = "./file/pub.key"cipher_file = "./file/flag.enc"#读入公钥with open(public_key, "r") as f: key = RSA.importKey(f) n = key.n e = key.e print(key.n) #86934482296048119190666062003494800588905656017203025617216654058378322103517 print(key.e) #65537#大数分解得到p = 285960468890451637935629440372639283459q = 304008741604601924494328155975272418463phin = (q-1)*(p-1)d = gmpy2.invert(e, phin)key = rsa.PrivateKey(n, e, int(d), p, q)with open("./file/flag.enc", "rb+") as f: f = f.read() print(rsa.decrypt(f, key))猜你可能想看的VPS
- azvds→$1.3 月 1GB 内存 10GB NVME 空间 不限流虚拟空间(主机)
- PacificRack→$9.99 年 KVM-1GB 13GB 2TB全球[VPS测评]
- 真实测评 六六云→洛杉矶三网双程 GIA1 核 1G 15G SSD 1全球[VPS测评]
- 新商家慎重-HostUp→7 美金 年 1 核 1G 1T 流量 荷兰服全球[VPS测评]
- 生日优惠 OLVPS→国内外机房多重神秘优惠 洛杉矶安畅机房 CN2 G全球[VPS测评]
- 优惠 Digital-VM→7 折优惠 日本机房 10Gbps 大带宽不日本VPS[主机]
- 抗投诉 VPS €14.6 半年 1G 内存 1T 月流量 罗马尼亚机房全球[VPS测评]
- 疯狂猜成语 图猜成语一个人在说话后面还站着两个人是什么成语?全球[VPS测评]
- 在博客中如何合理展示AdSense 广告全球[VPS测评]
- 咖啡主机 双十二 洛杉矶512M内存 40G硬盘 自带20Gbps高防 全球[VPS测评]
- 谷歌浏览器Google Chrome版本大全全球[VPS测评]
- 宝塔面板免费版如何开启 waf 防火墙全球[VPS测评]
- 如何申请和配置俄罗斯搜索引擎 Yandex 收录推送 API 教程全球[VPS测评]
- 通过 IndexNow 将 WordPress 网站内容更新快速提交给搜全球[VPS测评]
- WikiHost香港BGP VPS 九一折促销,500元起/年,三网回程香港VPS[主机]
- Kencengsolusindo → 印尼vps 7刀 月 1Gbps 全球[VPS测评]
- GreenCloudVPS → 新加坡存储机型 → 2G 500G 全球[VPS测评]
- 6种样式社交论坛网站HTML5模板 - Social全球[VPS测评]
- 20个温暖心灵的问题,抖音,朋友圈都可以拿去发。全球[VPS测评]
- 自适应bootstrap左侧导航可关闭全球[VPS测评]
- 快速云:科普云服务器服务器的概念和作用2022-08-2313:26来源全球[VPS测评]
- earidc怎么样?香港三网cn2vps带宽1M月付29元香港VPS[主机]
- 桔子数据怎么样?美国、香港CN2云服务器2核2G10M带宽仅480元/年美国VPS[主机]
- edgenat:韩国cn2+香港cn2,VPS八折优惠,8核8G/50gWINDOWS
- Aoyohost:1核1GB/20GB/600GB流量/60Mbps端口香港VPS[主机]
- 华为S2300 系列交换机如何修改密码?全球[VPS测评]
- 麻花云:2021新春采购节,香港云主机2折起,安徽BGP高防云机,移动大香港VPS[主机]
- 阿里云免备案服务器怎么样?阿里云香港服务器价格多少钱一年?香港VPS[主机]
- 1分钟快速读懂云计算全球[VPS测评]
- Pia云:美国三网CN2 GIA线路,15元/月起;香港云主机1核/2G美国VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-49953.html











