当前位置:网站首页>【zer0pts CTF 2022】 Anti-Fermat
【zer0pts CTF 2022】 Anti-Fermat
2022-07-27 21:07:00 【[email protected]】
1.题目
from Crypto.Util.number import isPrime, getStrongPrime
from gmpy import next_prime
from secret import flag
# Anti-Fermat Key Generation
p = getStrongPrime(1024)
q = next_prime(p ^ ((1<<1024)-1))
n = p * q
e = 65537
# Encryption
m = int.from_bytes(flag, 'big')
assert m < n
c = pow(m, e, n)
print('n = {}'.format(hex(n)))
print('c = {}'.format(hex(c)))
#n = 0x1ffc7dc6b9667b0dcd00d6ae92fb34ed0f3d84285364c73fbf6a572c9081931be0b0610464152de7e0468ca7452c738611656f1f9217a944e64ca2b3a89d889ffc06e6503cfec3ccb491e9b6176ec468687bf4763c6591f89e750bf1e4f9d6855752c19de4289d1a7cea33b077bdcda3c84f6f3762dc9d96d2853f94cc688b3c9d8e67386a147524a2b23b1092f0be1aa286f2aa13aafba62604435acbaa79f4e53dea93ae8a22655287f4d2fa95269877991c57da6fdeeb3d46270cd69b6bfa537bfd14c926cf39b94d0f06228313d21ec6be2311f526e6515069dbb1b06fe3cf1f62c0962da2bc98fa4808c201e4efe7a252f9f823e710d6ad2fb974949751
#c = 0x60160bfed79384048d0d46b807322e65c037fa90fac9fd08b512a3931b6dca2a745443a9b90de2fa47aaf8a250287e34563e6b1a6761dc0ccb99cb9d67ae1c9f49699651eafb71a74b097fc0def77cf287010f1e7bd614dccfb411cdccbb84c60830e515c05481769bd95e656d839337d430db66abcd3a869c6348616b78d06eb903f8abd121c851696bd4cb2a1a40a07eea17c4e33c6a1beafb79d881d595472ab6ce3c61d6d62c4ef6fa8903149435c844a3fab9286d212da72b2548f087e37105f4657d5a946afd12b1822ceb99c3b407bb40e21163c1466d116d67c16a2a3a79e5cc9d1f6a1054d6be6731e3cd19abbd9e9b23309f87bfe51a822410a622.复现
重点看p,q的生成方式,p是随机的1024位素数,q = next_prime(p ^ ((1<<1024)-1))。
1<<1024-1=2**1024-1化为2进制就是1024个1,由于一个二进制数异或1就是取它的反,意思就是1变成0,0变成1。所以p^2**1024-1的结果x是把p的每一位取反,所以x+p=2**1024-1。
由于q是x的下一个素数所以q=x+r,带进去p+q=2**1024-1+r,所以p+q就约等于2**1024。
又由于
把p+q带进去
所以4n=2**2048-(p-q)**2,p-q=
,因为p+q=
,所以p=
然后再向后面爆破p就行了。
import gmpy2
import sympy
import libnum
n = int(0x1ffc7dc6b9667b0dcd00d6ae92fb34ed0f3d84285364c73fbf6a572c9081931be0b0610464152de7e0468ca7452c738611656f1f9217a944e64ca2b3a89d889ffc06e6503cfec3ccb491e9b6176ec468687bf4763c6591f89e750bf1e4f9d6855752c19de4289d1a7cea33b077bdcda3c84f6f3762dc9d96d2853f94cc688b3c9d8e67386a147524a2b23b1092f0be1aa286f2aa13aafba62604435acbaa79f4e53dea93ae8a22655287f4d2fa95269877991c57da6fdeeb3d46270cd69b6bfa537bfd14c926cf39b94d0f06228313d21ec6be2311f526e6515069dbb1b06fe3cf1f62c0962da2bc98fa4808c201e4efe7a252f9f823e710d6ad2fb974949751)
c = int(0x60160bfed79384048d0d46b807322e65c037fa90fac9fd08b512a3931b6dca2a745443a9b90de2fa47aaf8a250287e34563e6b1a6761dc0ccb99cb9d67ae1c9f49699651eafb71a74b097fc0def77cf287010f1e7bd614dccfb411cdccbb84c60830e515c05481769bd95e656d839337d430db66abcd3a869c6348616b78d06eb903f8abd121c851696bd4cb2a1a40a07eea17c4e33c6a1beafb79d881d595472ab6ce3c61d6d62c4ef6fa8903149435c844a3fab9286d212da72b2548f087e37105f4657d5a946afd12b1822ceb99c3b407bb40e21163c1466d116d67c16a2a3a79e5cc9d1f6a1054d6be6731e3cd19abbd9e9b23309f87bfe51a822410a62)
e = 65537
p=(gmpy2.iroot(pow(2,2048)-4*n,2)[0]+pow(2,1024))//2
# p=(2**1024+gmpy2.iroot((2**1024)**2-4*n,2)[0])//2
p=int(p)
while(1):
p=sympy.nextprime(p)
if(n%p==0):
print(p)
break
q=n//p
phi=(p-1)*(q-1)
d=gmpy2.invert(e,phi)
m=pow(c,d,n)
flag=libnum.n2s(int(m))
print(flag)
# b'Good job! Here is the flag:\n+-----------------------------------------------------------+\n| zer0pts{F3rm4t,y0ur_m3th0d_n0_l0ng3r_w0rks.y0u_4r3_f1r3d} |\n+-----------------------------------------------------------+'
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_61774705/article/details/124987392
边栏推荐
- 请求合并哪家强——接口请求合并的3种技巧,性能直接爆表
- This is the most concise guide to tcpdump in history. It's enough to read this one
- Can Siemens PLC collect analog data of multiple slave stations in real time and wirelessly?
- Common Taylor expansion
- Record the errors about formatc in R language
- Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
- 一加将在2020年释放ODM订单,发力中低端市场
- Date的使用
- 进制转换方法
- Yijia will release ODM orders in 2020 and make efforts in the middle and low-end market
猜你喜欢

JUC toolkit learning

一位软件投资者的独白:我为什么不追逐快速增长的公司

Key points of data management

Common Taylor expansion

2022 International Conference on civil, building and Environmental Engineering (iccaee 2022)

消息队列常见的几种使用场景介绍

Redis 哈希Hash底层数据结构

proteus仿真arduino中调用DHT11/22温湿度传感器

Master data management theory and Practice

【C语言】通讯录(动态版本)
随机推荐
新技术引领大中型企业营销新变革,用友BIP CRM重磅发布!
远程调试 idea配置remote debug、在远程服务器的程序中,添加JVM启动参数-Xdebug
Calling dht11/22 temperature and humidity sensor in Proteus simulation Arduino
Realize today's news website based on native JS
五子棋人机对战实现
Application of user portrait in precise push of wechat official account of scientific journals
Error:svn: E155010: ‘/Users/.../Desktop/wrokspace/xxx‘ is scheduled for addition, but is missing
Comparison between virtual memory and cache
BUUCTF-Baby RSA
sort排序
Redis 哈希Hash底层数据结构
File&递归14.1
2022 summer vacation daily question (5)
基于原生js实现今日新闻网站
[December Haikou] the 6th International Conference on ships, marine and Maritime Engineering in 2022 (naome 2022)
请求合并哪家强——接口请求合并的3种技巧,性能直接爆表
钉钉报警工具
Redis 哈希Hash底层数据结构
This is the most concise guide to tcpdump in history. It's enough to read this one
编辑复制粘贴判定问题(bug?),所见即所得显示符号问题反馈。
把p+q带进去
,因为p+q=
,所以p=