RSA题目刷题记录(持续更

1.RSA
在一次RSA密钥对生成中,假设p=473398607161,q=4511491,e=17
求解出d作为flga提交

思路:
N=pq
L=(p-1)(q-1)

17*D mod L=1
p = 473398607161
q = 4511491
e = 17

N = 2135733555619387051
L = 2135733082216268400

脚本:
import gmpy2
p = 473398607161
q = 4511491
e = 17

N = 2135733555619387051
L = 2135733082216268400

D=gmpy2.invert(e,L)
print(D)

本题重点:gmpy2.invert函数能够求出D,e与D相乘再取L为余得到结果为1,invert能够实现这个算法反向求出D

2.rsarsa
Math is cool! Use the RSA algorithm to decode the secret message, c, p, q, and e are parameters for the RSA algorithm.

p = 9648423029010515676590551740010426534945737639235739800643989352039852507298491399561035009163427050370107570733633350911691280297777160200625281665378483
q = 11874843837980297032092405848653656852760910154543380907650040190704283358909208578251063047732443992230647903887510065547947313543299303261986053486569407
e = 65537
c = 83208298995174604174773590298203639360540024871256126892889661345742403314929861939100492666605647316646576486526217457006376842280869728581726746401583705899941768214138742259689334840735633553053887641847651173776251820293087212885670180367406807406765923638973161375817392737747832762751690104423869019034

Use RSA to find the secret message

本题思路
1.N=pq,求得N
2.L=(p-1)
(q-1),求得L
e已经给了
可以直接求d,用invert
求出来之后需要注意题目给了密文
而明文=密文的D次方再取N为模求余
此处用到第二个函数:pow
m=pow(c,d,N)
意思就是c的d次方再以N为模取余

脚本:
import gmpy2

p=9648423029010515676590551740010426534945737639235739800643989352039852507298491399561035009163427050370107570733633350911691280297777160200625281665378483
q=11874843837980297032092405848653656852760910154543380907650040190704283358909208578251063047732443992230647903887510065547947313543299303261986053486569407
e=65537
c=83208298995174604174773590298203639360540024871256126892889661345742403314929861939100492666605647316646576486526217457006376842280869728581726746401583705899941768214138742259689334840735633553053887641847651173776251820293087212885670180367406807406765923638973161375817392737747832762751690104423869019034

N=pq
D=gmpy2.invert(e,(p-1)
(q-1))
print(D)
m=pow(c,D,p*q)
print(m)

本题重点在于密文明文对应算法,这里做题的时候忘了,还有就是引进了新的函数pow(a,b,c)
密文=明文E次方 mod N
明文=密文D次方 mod N
速记:明文encrypted=密文,密文decrypted=明文

posted @ 2021-03-09 12:47  ChristopherWu  阅读(406)  评论(0编辑  收藏  举报