就当为CSDN这方面的python开源做点贡献哈哈哈
RSA
- RSA介绍
- 算法原理
- python代码
#RSA算法的计算机实现
from random import randint
class RSA:
def __init__(self,p:int,q:int):#取两个素数生成公私钥从而创建相应的RSA
if PrimalityTest(p)==False:
print(p,'is not prime!Please choose another one and input again')
elif PrimalityTest(q)==False:
print(q,'is not prime!Please choose another one and input again')
else:
self.n=p*q
fain=(p-1)*(q-1)
e=randint(2,fain-1)
while Eulid(e,fain)!=1:e=randint(2,fain-1)
self.e=e
d=ExtendedEulid(fain,self.e)
self.d=d
print('Public keys:n=',self.n,',e=',self.e)
def encrypt(self,x:int):
y=(x**self.e)%self.n
print('The ciphertext is:',y)
def decrypt(self,y:int):
x=(y**self.d)%self.n
print('The plaintext is:',x)
- 实现过程
①生成RSA密码器
取两个素数创建RSA密码类对象并生成相应的公钥和私钥:
(ps:这里的公钥e是随机生成的,详见原理部分)
②加密
对‘2020’加密:
③解密
对得到的密文解密:
④查看私钥
怎么样?RSA是不是很神奇?!