import math
import random
#生成素数数组
def primeArray():
arraya = []
for i in range(2,100): #生成前100中的素数,从2开始因为2是最小的素数
x = prime(i,2) #i为素数是返回True,则将x加入arraya数组中;2为测试值
if x:
arraya.append(i)
return arraya
#判断素数
def prime(p):
if p <= 1:
return False
for i in range(2, int(math.sqrt(p)) + 1):
if p % i == 0:
return False
return True
#找出与(p-1)*(q-1)互质的数e
def coPrime(s):
while True:
e = random.choice(range(100))
x = gcd(e,s)
if x==1: #如果最大公约数为1,则退出循环返回e
break
return e
#求两个数的最大公约数
def gcd(a,b):
if b==0:
return a
else:
return gcd(b, a%b)
#根据e*d mod s = 1,找出d
def find_d(e,s):
for d in range(100000000):
x = (e*d) % s
if x==1:
return d
#生成公钥和私钥
def main():
a= primeArray()
print("前100个素数:",a)
p = random.choice(a)
q = random.choice(a)
print("随机生成两个素数p和q. p=",p," q=",q)
n = p * q
s = (p-1)*(q-1)
#print("The p is ", p)
#print("The q is ", q)
#print("The n(p*q) is ",n)
e = coPrime(s)
print("根据e和(p-1)*(q-1))互质得到: e=", e)
d = find_d(e,s)
print("根据(e*d) 模 ((p-1)*(q-1)) 等于 1 得到 d=", d)
print("公钥: n=",n," e=",e)
print("私钥: n=",n," d=",d)
pbvk=(n,e,d)
return pbvk
#生成public key公钥或private key私钥
def createKeys(a,zx):
pbk = (a[0],a[1]) #public key公钥
pvk = (a[0],a[2]) #private key私钥
#print("公钥: n=",pbk[0]," e=",pbk[1])
#print("私钥: n=",pvk[0]," d=",pvk[1])
if zx==0:
return pbk
if zx==1:
return pvk
#加密
def encryption(mw, ned):
B = pow(mw,ned[1]) % ned[0]
return B
#解密
def decode(mw, ned):
C = pow(mw,ned[1]) % ned[0]
return C
if __name__=='__main__':
pbvk = main()
pbk = createKeys(pbvk, 0) if 0 return pbk if 1 return pvk
A = int(input("请输入明文: "))
print("")
B = encryption(A,pbk) #加密
print("生成的密文是: ", B)
pvk = createKeys(pbvk, 1) #私钥
print("")
C = decode(B,pvk) #解密
print("解密后的明文是: ", C)