# -*- coding: utf-8 -*-
"""
Created on Fri Oct 15 13:40:51 2021
@author: 16381
"""
import secrets
import math
#素性检验
n=5515596313
d=2674607171
e=1757316971
data=dict(a='01',b='02',c='03',d='04',e='05',f='06',g='07',h='08',i='09',j='10',k='11',
l='12',m='13',n='14',o='15',p='16',q='17',r='18',s='19',t='20',u='21',v='22',w='23',x='24',y='25',z='26',_='00')
def isshu(m):
a=int(math.sqrt(m))
for i in range(2,a):
if m%i==0:
return 0
return 1
def creatshusu():
a=secrets.randbelow(1000000)
while( a<100000 or isshu(a)==0 ):
a=secrets.randbelow(1000000)
return a
def gcd1(m,n):
if m<n:
t=n
n=m
m=t
while(n!=0):
t=m%n
m=n
n=t
return m
#求乘法逆元
def chengfares(n,d):
t=0
v=1
a=n
b=d
r=a%b
while r!=0:
q=int(a/b)
r=a%b
if r==0:
break
a=b
b=r
w=v
v=t-q*v
t=w
if(b!=1):
print(f'{d}的逆元不存在')
if(v<0):
v=n+v
print(f'{d}的逆元为{v}')
return v
#创建RSA密码
def creatkey():
global n,d,e
q=creatshusu()
p=creatshusu()
while(p==q):
p=creatshusu()
n=p*q
n_1=(p-1)*(q-1)
e=secrets.randbelow(n_1)
while(gcd1(n_1,e)!=1):
e=secrets.randbelow(n_1)
d=chengfares(n,e)
print(f'密钥对已生成,公钥为:{e},{n},私钥为{d},{n}')
#二进制转化函数
def change(m):
b=[]
while(m!=0):
b.append(m%2)
m=int(m/2)
b.reverse()
return b
#快速指数
def quickzhishu(a,m,n):
b=change(m)
c=0
d=1
for i in range(0,len(b)):
c=int(c*2)
d=(d*d)%n
if (b[i]==1):
c=c+1
d=(d*a)%n
return d
def jiami(c):
# c=[1612050119,500230109,2000061518,13050000]
m=[]
for i in range(0,len(c)):
m.append(quickzhishu(c[i],e,n))
print(m)
return m
def jiemi(m):
c=[]
zi=['']
s=[]
for i in range(0,len(m)):
c.append(quickzhishu(m[i],d,n))
print('解密')
print(c)
for j in range(0,len(c)):
while c[j]!=0:
# print(c[j])
temp=c[j]%100
# print(temp)
zi.append(chr(temp+96))
# print(chr(temp+96),end=(''))
c[j]=int(c[j]/100)
# z=zi.reverse()
# print(z,end='')
zi.reverse()
# print(zi)
for l in range(0,len(zi)):
s.append(zi[l])
zi=[]
# s.append(zi)
for i in range(0,len(s)):
print(s[i],end=(''))
# print(s)
return s
def changenum(c):
print(c)
n=[]
num=[]
for i in range(0,len(c)):
n.append(data[c[i]])
# print(data[c[i]],end=(''))
# print(' ')
# print(len(n))
# print(n)
# m=int(len(n)*2/4/2)
# if(len(n)*2/4/2>int(len(n)*2/4/2)):
# m=m+1
# print(m)
m=5
for i in range(0,len(n),m):
temp=int(n[i])
for j in range(1,m):
if i+j>=len(n):
for k in range(j,m):
temp=temp*100
break
temp=(temp)*100+int(n[i+j])
num.append(temp)
# print(temp)
# print(n[i]+n[i+1])
return num
# creatkey()
c=input('请输入明文:')
# c='hello_word'
# c='hello_word'
# for i in range(0,len(c),2):
# print()
# print(changenum(c))
jiemi(jiami(changenum(c)))
# print(len(changenum(c)))
# jiemi(jiami())
# b=[]
# a='b'
# print(ord(a[0])-97)
# c=[1612050119,500230109,2000061518,13050000]
# print(len(c))
# print(type(c[3])\
def main():
return 0
# print(creatshusu())
# print(isshu(17))
# print(quickzhishu(1612050119, 1757316971, 5515596313))
# chefares(26,7)
# b=[]
# print(len(b))
|