ElGamal encryption algorithm

 

ElGamal encryption algorithm

ElGamal encryption is a public key cryptography system. It uses asymmetric key encryption communications between the parties and the encrypted message.
The password system is based is difficult to find a cyclic group of discrete logarithm difficult, even though we know G  A and G  k , it is difficult to calculate G  AK .

The idea ElGamal cryptosystem
suppose Alice wants to communicate with Bob.

  1. Bob generates public and private keys:
    • Bob choose a very large number of q and a cyclic group q .
    • From the cyclic group ˚F  Q , he chose any element g and
      a member a , so as to satisfy gcd (A, Q) = 1 .
    • He then calculate G = H  A .
    • Bob released F , H = g  a , q , and g as his public key, and retain a as a private key.
  2. Alice uses the public key to encrypt data Bob:
    • Alice from the cyclic group F, select an element K ,  so gcd (k, q) = 1 .
    • She then calculate G = P  K and H = S  K  = G  AK  .
    • She and M multiplied by s.
    • She then sent (P, M * S) = (G  K , M * S).
  3. Bob decrypts the message:
    • Bob calculate S  '  = P  A  = G  AK .
    • He will be divided by M * s S  ' to give M, i.e., = S S  ' .

The following is the realization ElGamal cryptosystem in Python

# Python program to illustrate ElGamal encryption   
    
import random    
from math import pow  
    
a = random.randint(2, 10)   
    
def gcd(a, b):   
    if a < b:   
        return gcd(b, a)   
    elif a % b == 0:   
        return b;   
    else:   
        return gcd(b, a % b)   
    
# Generating large random numbers   
def gen_key(q):   
    
    key = random.randint(pow(10, 20), q)   
    while gcd(q, key) != 1:   
        key = random.randint(pow(10, 20), q)   
    
    return key   
    
# Modular exponentiation   
def power(a, b, c):   
    x = 1  
    y = a   
    
    while b > 0:   
        if b % 2 == 0:   
            x = (x * y) % c;   
        y = (y * y) % c   
        b = int(b / 2)   
    
    return x % c   
    
# Asymmetric encryption   
def encrypt(msg, q, h, g):   
    
    en_msg = []   
    
    k = gen_key(q)# Private key for sender   
    s = power(h, k, q)   
    p = power(g, k, q)   
        
    for i in range(0, len(msg)):   
        en_msg.append(msg[i])   
    
    print("g^k used : ", p)   
    print("g^ak used : ", s)   
    for i in range(0, len(en_msg)):   
        en_msg[i] = s * ord(en_msg[i])   
    
    return en_msg, p   
    
def decrypt(en_msg, p, key, q):   
    
    dr_msg = []   
    h = power(p, key, q)   
    for i in range(0, len(en_msg)):   
        dr_msg.append(chr(int(en_msg[i]/h)))   
            
    return dr_msg   
    
# Driver code   
def main():   
    
    msg = 'encryption'  
    print("Original Message :", msg)   
    
    q = random.randint(pow(10, 20), pow(10, 50))   
    g = random.randint(2, q)   
    
    key = gen_key(q)# Private key for receiver   
    h = power(g, key, q)   
    print("g used : ", g)   
    print("g^a used : ", h)   
    
    en_msg, p = encrypt(msg, q, h, g)   
    dr_msg = decrypt(en_msg, p, key, q)   
    dmsg = ''.join(dr_msg)   
    print("Decrypted Message :", dmsg);   
    
    
if __name__ == '__main__':   
    main()   

Sample output:

Original Message: encryption 
g Use: 5860696954522417707188952371547944035333315907890 
used g ^ a: 4711309755639364289552454834506215144653958055252 
used g ^ k: 12475188089503227615789015740709091911412567126782 
used g ^ ak: 39448787632167136161153337226654906357756740068295 
decrypts the message: Encryption

In the cryptographic system, the original message M by AK multiplying it to hide . In order to remove the mask to K is given in the form of clues . Unless someone knows a , he will not be able to retrieve the number . It is difficult to find loop group discrete logarithm, and only knows G  A and G  K sufficient to calculate G  AK .

 

 

Guess you like

Origin www.cnblogs.com/ye1031/p/11999980.html