Baidu Encyclopedia portal
Based on Duffy - Asymmetric encryption algorithm of Herman key exchange
EIGamal The public key cryptosystem is based on the intractability of the problem between discrete logarithms in a finite field . It is based on the principle that : It is difficult to solve the discrete logarithm , The inverse operation can be effectively calculated by square multiplication . In the corresponding group G in , An exponential function is a one-way function .
Operation error reporting 、 Please see the instructions for timeout
import prime
import random
import math
p=prime.safe_length(150)
g=prime.safe_root(p)
a=random.randint(2, p)
ga=pow(g,a,p)
print(' Public key :('+str(p)+',\n'+str(g)+',\n'+str(ga)+')')
#print(' Private key :'+str(a))
mb=prime.length(149)#int(input('m:'))
print('\nB\'s input:\n'+str(mb))
k=random.randint(1, p-1)
while math.gcd(p-1, k)!=1:
k=random.randint(1, p-1)
c1=pow(g, k,p)
c2=((mb%p)*pow(ga,k,p))%p
print('\ncyphertext:('+str(c1)+',\n'+str(c2)+')')
v=pow(c1,a,p)
ma=(c2*prime.modinv(v,p))%p
print('\nA\'s output:\n'+str(ma))
print(ma==mb)# Plaintext comparison