CTF-RSA_基于N分解的RSA题目
- 对N进行分解(只要知道p和q,就能解出任何rsa)
- N在有一般情况下不可分解的,如果p和q太接近,或相差过大,或pq很小等情况
1.在线查询分解网站
http://www.factordb.com/index.php
2.使用yafu工具分解
下载地址:https://sourceforge.net/projects/yafu/
#以分解49为例
yafu-x64.exe factor(49)
#导入文件进行分解,主要注意文本结尾要换行!!!不然要报错
yafu-x64.exe "factor(@)" -batchfile 1.txt
3.使用费马分解
网上找的脚本,p和q太接近
def isqrt(n):
x = n
y = (x + n // x) // 2
while y < x:
x = y
y = (x + n // x) // 2
return x
def fermat(n, verbose=True):
a = isqrt(n) # int(ceil(n**0.5))
b2 = a*a - n
b = isqrt(n) # int(b2**0.5)
count = 0
while b*b != b2:
# if verbose:
# print('Trying: a=%s b2=%s b=%s' % (a, b2, b))
a = a + 1
b2 = a*a - n
b = isqrt(b2) # int(b2**0.5)
count += 1
p=a+b
q=a-b
assert n == p * q
# print('a=',a)
# print('b=',b)
# print('p=',p)
# print('q=',q)
# print('pq=',p*q)
return p, q
fermat(n)
脚本2