下载task.py看到内容
from Crypto.Util.number import *
from secret import flag
m = bytes_to_long(flag)
p = getPrime(512)
q = getPrime(512)
e = 65537
n = p*q
c = pow(m,e,n)
print(f'p = {p}')
print(f'q = {q}')
print(f'c = {c}')
'''
p = 12567387145159119014524309071236701639759988903138784984758783651292440613056150667165602473478042486784826835732833001151645545259394365039352263846276073
q = 12716692565364681652614824033831497167911028027478195947187437474380470205859949692107216740030921664273595734808349540612759651241456765149114895216695451
c = 108691165922055382844520116328228845767222921196922506468663428855093343772017986225285637996980678749662049989519029385165514816621011058462841314243727826941569954125384522233795629521155389745713798246071907492365062512521474965012924607857440577856404307124237116387085337087671914959900909379028727767057
'''
认识RSA:
https://blog.csdn.net/dbs1215/article/details/48953589
先关函数:
gmpy2.inverse(x, y): 计算模逆元,即找到一个整数z,使得x * z % y == 1。
gcd(x, y): 计算两个数的最大公约数
bytes_to_long(s): 将字节串转换为长整数
pow(x,y,n) 函数:求(x 的 y 次方) 的值。
result1 = pow(2, 3) # 计算 2 的 3 次方
print(result1) # 输出: 8
result2 = pow(2, 3, 5) # 计算 2 的 3 次方再对 5 取模
print(result2) # 输出: 3
getPrime(x):Python自带的一种方法,通过调用该方法可以生成指定位数(x)的素数
素数:又称质数(一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数)
自然数:自然数是指表示物体个数的数,即由0开始,0,1,2,3,4,……一个接一个,组成一个无穷的集体,即指非负整数。
exp:
from Crypto.Util.number import * # Crypto.Util.number模块提供了一组用于处理大整数的实用函数,这些函数在加密和数学运算中经常用到
import gmpy2 # gmpy2是一个Python库,旨在提供高精度计算功能,并且支持整数、有理数、浮点数等多种数据类型
p = 12567387145159119014524309071236701639759988903138784984758783651292440613056150667165602473478042486784826835732833001151645545259394365039352263846276073
q = 12716692565364681652614824033831497167911028027478195947187437474380470205859949692107216740030921664273595734808349540612759651241456765149114895216695451
c = 108691165922055382844520116328228845767222921196922506468663428855093343772017986225285637996980678749662049989519029385165514816621011058462841314243727826941569954125384522233795629521155389745713798246071907492365062512521474965012924607857440577856404307124237116387085337087671914959900909379028727767057
e = 65537
L = (p-1) * (q-1)
n = q * p
d = gmpy2.invert(e,L)
m = pow(c,d,n)
print(m)
print(long_to_bytes(m))
NSSCTF{it_is_easy_to_solve_question_when_you_know_p_and_q}
标签:gmpy2,签到,pow,自然数,RSA,LitCTF,整数,print,import
From: https://blog.csdn.net/2301_80216972/article/details/137526483