ElGamal ExampleElGamal is a public key method that is used in both encryption and digital signing. It is used in many applications and uses discrete logarithms. At the root is the generation of P which is a prime number and G (which is a value between 1 and P-1) [Theory]. Try example (P=71, G=33, x=62, M=15 and y=31) Try! Check
First Bob generates a prime number (p) and a number (g) which is between 1 and (p-1):
Bob select a random number (x) which will be his private key:
He then calculates Y:
Bob public key is now [P,G,Y] and sends g, p and Y to Alice. The private key is x Alice then creates a message:
and then selects a random value (k), and calculates two new values (a and b):
Next Bob decrypts the message:
The original message and the decrypted version match ... success! |
Theory
Picking the G value in ElGamal
Code used
[HttpPost] public ActionResult elgamal(string button1) { if (button1 != null) { int x = Global.random(200) + 10; int P = getPrime(); int G = Global.random(P - 1); int k = Global.random(200) + 10; BigInteger g = new BigInteger(Convert.ToString(G)); BigInteger p = new BigInteger(Convert.ToString(P)); BigInteger y = g.Pow(x).Mod(p); int mess = Global.random(200) + 10; BigInteger message = new BigInteger(Convert.ToString(mess)); BigInteger a = g.Pow(k).Mod(p); BigInteger b = y.Pow(k).Multiply(message).Mod(p); BigInteger po = p.Subtract(new BigInteger("1")).Subtract(new BigInteger(Convert.ToString(x))); BigInteger m1 = a.Pow(po.IntValue).Mod(p); BigInteger res = m1.Multiply(b).Mod(p); ViewData["XValue"] = x.ToString(); ViewData["GValue"] = g.ToString(); ViewData["Pvalue"] = p.ToString(); ViewData["Yvalue"] = y.ToString(); ViewData["AValue"] = Convert.ToString(a); ViewData["BValue"] = Convert.ToString(b); ViewData["res"] = Convert.ToString(res); ViewData["message"] = Convert.ToString(message); if (Convert.ToString(message) == Convert.ToString(res)) ViewData["messagematch"] = "The original message and the decrypted version match ... success!"; else ViewData["messagematch"] = "The messages do not match ... failure!"; return PartialView("elgamal_partial"); } return View(); }