RSA

参考文献: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


img_7018e37dbfa76ff9cba0b247760da72c.png


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文件来解,但是需要改一下,前面的不用动,把测试函数后面的内容改成下面的内容


img_7d9e1ec3b418fdc95e7e9e2cb8f38ec4.png

然后就能求出d了,求出d之后


img_a5d06f4560f5761a72abe4f98f3c65f3.png
RSA-Tool 2 Copyright ?2000-2002 by tE! [TMG] Introduction Please read this text carefully. This utility has been made for those who want to use the RSA public key algorithm in their own programs. It offers creation of strong keypairs and a nice integer factorization feature which makes use of several differnt factoring methods including the MPQS. It's possible to factor integers +256 bits in size but please keep in mind that this can take a *lot* of memory and time ! Thus it's not recommended to try factoring bigger numbers on slow machines with a few MB of physical Memory. Don't even think of trying to factor 512 bit numbers for example.. RSA-Tool 2 Features: - Secure keypair generation - Key test dialog - Support of multiple number bases - Auto base-conversion on select - Support of numbers up to 4096 Bits 1. About RSA RSA is a Public Key Cryptosystem developed in 1977 by Ronald Rivest, Adi Shamir and Leonard Adleman. Since 09-20-2000 the U.S. Patent #4,405,829 on this Algorithm EXPIRED! That means that the Algorithm is Public Domain now and can be used by everyone for free, even in commercial software. 2. Parameters P = 1st large prime number Q = 2nd large prime number (sizes of P and Q should not differ too much!) E = Public Exponent (a random number which must fulfil: GCD(E, (P-1)*(Q-1))==1) N = Public Modulus, the product of P and Q: N=P*Q D = Private Exponent: D=E^(-1) mod ((P-1)*(Q-1)) Parameters N and E are public whereas D is -private- and must NEVER be published! P and Q are not longer needed after keygeneration and should be destroyed. To obtain D from the public key (N, E) one needs to try splitting N in its both prime factors P and Q. For a large Modulus N (512 bit and more) with carefully chosen primefactors P and Q this is a very difficult problem. All the security of the RSA encryption scheme relies on that integer factorization problem (tough there's no mathematical proof for it). To fin
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值