参考文献:https://www.anquanke.com/post/id/84632
RSA的加密过程
选择两个大素数p和q,计算出模数N = p * q
计算φ = (p−1) * (q−1) 即N的欧拉函数,然后选择一个e(1<e<φ),且e和φ互质
取e的模反数为d,计算方法: e * d ≡ 1 (mod φ)
对明文A进行加密: B = pow(A,e,n),得到的B即为密文。
对密文B进行解密,A = pow(B,d,n),得到的A即为明文,这个pow()在python中的gmpy库中有,可以直接用
p 和 q :大整数N的两个因子
N:大整数N,我们称之为模数
e 和 d:互为模反数的两个指数
c 和 m:分别是密文和明文,这里一般指的是一个十进制的数,是16进制数的时候
RSA的算法涉及三个参数,n、e、d。
其中,n是两个大质数p、q的积,n以二进制表示时所占用的位数,就是所谓的密钥长度。
e和d是一对相关的值,e可以任意取,但要求e与(p-1)(q-1)互质;再选择d,要求(ed) ≡ 1(mod(p-1)×(q-1))。
令φ = (p-1)(q-1) 上式即 d*e = 1 mod φ 即:(d*e - 1)% φ = 0
(n,e),(n,d)就是密钥对。其中(n,e)为公钥,(n,d)为私钥。RSA加解密的算法完全相同,设A为明文,B为密文,则:A≡B^d( mod n);B≡A^e (mod n);
e和d可以互换使用,即:A≡B^e (mod n);B≡A^d( mod n);
上面是大佬发的RSA的入门,把在实验吧做过的RSA的题整理一下
1.RSA实践
嗯,用工具 RSA-Tool 2 by tE!(没有就自己下啊),至于E为啥子是11而不是题目上给的17,因为17出来的结果提交不对,公钥进制工具默认是hex不是十进制,需要将10进制17转换为16进制的11
2.RSAROLL
题目给的{920139713,19},则n是920139713,在http://www.atool.org/quality_factor.php或者http://factordb.com这个网址上可以分解出两个质数pq,然后用pqe求出d,再用密文nd求出每个明文,最后合并下就行了,直接python脚本跑下就行了
import gmpy2
N,p,q,e=920139713,18443,49891,19
d=gmpy2.invert(e,(p-1)*(q-1))
res=[]
with open("1.txt")as f:
f.readline()
for i in f:
res.append(chr(pow(int(i),d,N)))
print ("".join(res))
3.rsarsa
其实挺简单的,贴下代码
# -*- coding: UTF-8 -*-
import gmpy2
p =9648423029010515676590551740010426534945737639235739800643989352039852507298491399561035009163427050370107570733633350911691280297777160200625281665378483
q =11874843837980297032092405848653656852760910154543380907650040190704283358909208578251063047732443992230647903887510065547947313543299303261986053486569407
n = p*q
e =65537
c=83208298995174604174773590298203639360540024871256126892889661345742403314929861939100492666605647316646576486526217457006376842280869728581726746401583705899941768214138742259689334840735633553053887641847651173776251820293087212885670180367406807406765923638973161375817392737747832762751690104423869019034
d=gmpy2.invert(e,(p-1)*(q-1))
print ("d:",d)
key=pow(c, d, n)
print ("pwd:",key)
4.warmup
这道题真的是神烦,下载下来是个txt文件和pub文件,打开pub文件能看到是一个公钥文件,这是用“openssl”加密过的一段密码,直接kali解下
openssl rsa -pubin -text -modulus -in warmup.pub
然后解开之后有点蒙,完全不知道啥是啥,有Modulus:和Exponent:两个模块,后来看了下大佬的wp知道了前者是N,后者是E,知道了N和E就可以求d了,用github中https://github.com/pablocelayes/rsa-wiener-attack这个里面的RSAwienerHacker.py文件来解,但是需要改一下,前面的不用动,把测试函数后面的内容改成下面的内容
然后就能求出d了,求出d之后