RSA算法总结

Posted ying-hack

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RSA算法总结相关的知识,希望对你有一定的参考价值。

一直想写一个RSA算法的总结,但是一直没有写,直到今天。

RSA

RSA算法介绍

RSA算法使用了乘方运算。
在加密时,明文M经过加密运算得到密文C:C=Me mod n.
密文在经过解密得到明文M:Cd mod n = (Me mod n)d mod n = Med mod n = M
即必须存在e,d,n使得Med mod n = M成立,这里以n,e为公钥,私钥为d。
下面给出确定e,d,n的方法:
(1)确定n:独立的选取两大素数p和q。计算n=p x q。
(2)确定e:计算n的欧拉函数值 φ(n) = (p-1)(q-1),随机选择一整数e,使得1<=e<φ(n)和gcd(φ(n),e)=1成立
(3)确定d:计算e模φ(n)的乘法逆元,即为d:ed≡1 mod φ(n)

RSA流程

(1)选两个保密的大素数p和q
(2)计算n=pxq,φ(n)=(p-1)(q-1),其中φ(n)是n的欧拉函数值
(3)选一整数e,满足1<e<φ(n),且gcd(φ(n),e)=1.
(4)计算d,满足d * e ≡ 1 mod φ(n),即d是e在模φ(n)下的乘法逆元,因e与φ(n)互素,由模运算可知,它的乘法逆元一定存在。
(5)以{e,n}为公钥,以{d,n}为私钥

共模攻击(m,n相同;e,c不同,且e1 和 e2互质)

import gmpy2
from Crypto.Util.number import long_to_bytes

e1 = 17
e2 = 65537
n = 111381961169589927896512557754289420474877632607334685306667977794938824018345795836303161492076539375959731633270626091498843936401996648820451019811592594528673182109109991384472979198906744569181673282663323892346854520052840694924830064546269187849702880332522636682366270177489467478933966884097824069977
c1 = 54995751387258798791895413216172284653407054079765769704170763023830130981480272943338445245689293729308200574217959018462512790523622252479258419498858307898118907076773470253533344877959508766285730509067829684427375759345623701605997067135659404296663877453758701010726561824951602615501078818914410959610

c2 = 91290935267458356541959327381220067466104890455391103989639822855753797805354139741959957951983943146108552762756444475545250343766798220348240377590112854890482375744876016191773471853704014735936608436210153669829454288199838827646402742554134017280213707222338496271289894681312606239512924842845268366950

_, r, s = gmpy2.gcdext(e1, e2)

m = pow(c1, r, n) * pow(c2, s, n) % n
print long_to_bytes(m)


n's being,
Thou, from whose unseen presence the leaves dead
Are driven, like ghosts from an enchanter fleeing,
Yellow, a

p高位攻击

sage脚本

p = 7117286695925472918001071846973900342640107770214858928188419765628151478620236042882657992902
e = 65537
n = 113432930155033263769270712825121761080813952100666693606866355917116416984149165507231925180593860836255402950358327422447359200689537217528547623691586008952619063846801829802637448874451228957635707553980210685985215887107300416969549087293746310593988908287181025770739538992559714587375763131132963783147
c = 59213696442373765895948702611659756779813897653022080905635545636905434038306468935283962686059037461940227618715695875589055593696352594630107082714757036815875497138523738695066811985036315624927897081153190329636864005133757096991035607918106529151451834369442313673849563635248465014289409374291381429646
pbits = 512
kbits = pbits-p.nbits()
p=p<<kbits
print("upper %d bits (of %d bits) is given")% (pbits-kbits, pbits)
PR.<x> = PolynomialRing(Zmod(n))
f = x + p
x0 = f.small_roots(X=2^kbits, beta=0.4)[0]  # find root < 2^kbits with factor >= n^0.4
print(p+int(x0))

python脚本

e = 65537
n = 113432930155033263769270712825121761080813952100666693606866355917116416984149165507231925180593860836255402950358327422447359200689537217528547623691586008952619063846801829802637448874451228957635707553980210685985215887107300416969549087293746310593988908287181025770739538992559714587375763131132963783147
c = 59213696442373765895948702611659756779813897653022080905635545636905434038306468935283962686059037461940227618715695875589055593696352594630107082714757036815875497138523738695066811985036315624927897081153190329636864005133757096991035607918106529151451834369442313673849563635248465014289409374291381429646
p = 11437038763581010263116493983733546014403343859218003707512796706928880848035239990740428334091106443982769386517753703890002478698418549777553268906496423
q = n/p
print q
print q*p
phi = (p-1)*(q-1)
d = gmpy2.invert(e,phi)
m =pow(c,d,n)
print(m)
print pow(m,e,n)
print(long_to_bytes(m))


nd black, and pale, and hectic red,
Pestilence-stricken multitudes: O thou,
Who chariotest to their dark wintry bed

e等于3

import gmpy2
from Crypto.Util.number import long_to_bytes
e = 3
c = 19105765285510667553313898813498220212421177527647187802549913914263968945493144633390670605116251064550364704789358830072133349108808799075021540479815182657667763617178044110939458834654922540704196330451979349353031578518479199454480458137984734402248011464467312753683234543319955893
n = 123814470394550598363280518848914546938137731026777975885846733672494493975703069760053867471836249473290828799962586855892685902902050630018312939010564945676699712246249820341712155938398068732866646422826619477180434858148938235662092482058999079105450136181685141895955574548671667320167741641072330259009
res=0
for k in xrange(200000000):
    if gmpy2.iroot(c+n*k,3)[1]==1:
        res=gmpy2.iroot(c+n*k,3)[0]
        print long_to_bytes(res)
        # print time.asctime()
        break
        
O wild West Wind, thou breath of Autum

以上是关于RSA算法总结的主要内容,如果未能解决你的问题,请参考以下文章

信息安全技术RSA算法的研究及不同优化策略的比较

信息安全技术RSA算法的研究及不同优化策略的比较

RSA加密算法原理

RSA

PHP RSA和RSA2加密算法代码

PHP RSA和RSA2加密算法代码