Lesson 9 of 15
ElGamal Encryption
ElGamal Encryption
The ElGamal cryptosystem (Taher ElGamal, 1985) is a public-key scheme based on the Discrete Logarithm Problem, like Diffie-Hellman. Unlike RSA, ElGamal encryption is probabilistic — the same plaintext encrypted twice produces different ciphertexts (due to the random ), which is a security advantage.
Key Generation
Choose a prime , generator , and private key . The public key is:
Encryption
To encrypt message with public key , choose a random session key :
The ciphertext is the pair .
Decryption
Using private key :
Since and :
So . ✓
Computing
By Fermat's Little Theorem, , so .
Example
, , private key , public key .
Encrypt with session key :
- ·
Decrypt : ✓
Your Task
Implement:
elgamal_encrypt(m, g, pub_key, k, p)→(c1, c2)elgamal_decrypt(c1, c2, priv_key, p)→m
Python runtime loading...
Loading...
Click "Run" to execute your code.