ElGamal Encryption Calculator

Enter a prime p, generator g, private key x, ephemeral key k, and message m.

How it is calculated

All arithmetic is modular, using fast exponentiation. With prime p, generator g, private key x, ephemeral key k and message m: y = gx mod p, c1 = gk mod p, s = yk mod p, c2 = m × s mod p. The pair (c1, c2) is the ciphertext, y is the public key and s is the shared secret. Inputs are cut to whole numbers. The tool needs p of at least 3, g of at least 2, x and k of at least 1, and 0 ≤ m < p; p up to about 3.03 billion works. It does not check that p is prime or that g is a generator, so that is your responsibility.

Worked example

Take p = 23, g = 5, x = 6, k = 3, m = 10. Public key: 56 mod 23 = 8. First part: c1 = 53 mod 23 = 125 mod 23 = 10. Shared secret: s = 83 mod 23 = 512 mod 23 = 6. Second part: c2 = 10 × 6 mod 23 = 60 mod 23 = 14. The ciphertext is (10, 14), the public key is 8, and the shared secret is 6.

Try this example →

Common mistakes

  • Choosing a message m equal to or larger than p. The message must be a whole number below p, otherwise there is no result; split longer data into blocks.
  • Using a non-prime p such as 4 or 24. The tool still returns numbers, but they are not a secure or properly decryptable ElGamal setup.
  • Setting x or k to 0. Both must be at least 1, so the tool gives nothing for 0.
  • Typing a decimal message such as 10.7. It is silently cut to 10, which changes what you encrypt.
  • Reusing the same k for two messages. Real ElGamal needs a fresh k each time; this tool only shows one run.

About this calculator

The ElGamal cryptosystem, published by Taher Elgamal in 1985, is a public-key encryption scheme whose security rests on the discrete logarithm problem in a finite cyclic group — given a prime p and generator g, it's computationally hard to recover x from y = g^x mod p. Key generation picks a private key x and publishes y = g^x mod p as the public key; encryption then draws a fresh random ephemeral key k for every message and produces a ciphertext pair (c₁, c₂) = (g^k mod p, m·y^k mod p), so the same plaintext never encrypts to the same ciphertext twice.

This calculator walks through that math step by step: enter a small prime p, generator g, private key x, ephemeral key k, and an integer message m, and it derives the public key and the resulting ciphertext pair exactly as the algorithm would. Cryptography students, computer science instructors, and security engineers use toy examples like this to build intuition for asymmetric encryption and for the discrete-log basis that also underlies Diffie-Hellman key exchange and the DSA signature scheme — real deployments use primes hundreds of digits long, far beyond hand or calculator arithmetic.

Was this helpful?

You can pick up to 3 reactions.

Comments (1)

  • Editorial team

    To check the output by hand as the receiver would, raise c1 to the private key modulo p. With the default values, 10 to the power 6 mod 23 is 6, which matches the shared secret shown, and multiplying c2 by the inverse of 6 mod 23 returns the message 10. Treat all of this as a classroom toy; never encrypt real data with small numbers. Which step do you find hardest?

Popular calculators

All Calculators